2012-04-16 86 views
8

我想創建Bash一個別名,這樣在bash中創建多字別名?

git diff somefile 

成爲

git diff --color somefile 

但我不想這樣定義

alias gitd = "git diff --color" 

自己的自定義別名,因爲如果我習慣了這些自定義別名,然後我放棄了在沒有這些映射的機器上工作的能力。

編輯:看來bash不允許使用多字別名。除了創建別名之外,還有其他解決方案嗎?圍繞分配標誌

+0

我不認爲這是可能的... – 2012-04-16 06:34:48

回答

8

更好的回答(對於這個特定的情況)。

git-config手冊頁:

color.diff 
     When set to always, always use colors in patch. When false (or 
     never), never. When set to true or auto, use colors only when the 
     output is to the terminal. Defaults to false. 

無功能或需要的別名。但是函數包裝方法對於任何命令都是通用的;把那張卡片貼在你的袖子上。

+0

再次感謝您指出這一點。我喜歡你的答案。但是這個對我的用例非常具體。所以選擇這個作爲接受的答案。 – Sudar 2012-04-16 07:17:23

1

避免坯在bash:

alias gitd="git diff --color" 
16

要爲命令創建一個更聰明的別名,你必須寫一個具有相同的名稱作爲命令的封裝功能,並分析論證,轉換它們,然後用轉換後的參數調用真實命令。

例如,您的git函數可以識別diff正在被調用,並在那裏插入--color參數。

代碼:

# in your ~/.bash_profile 

git() 
{ 
    if [ $# -gt 0 ] && [ "$1" == "diff" ] ; then 
    shift 
    command git diff --color "[email protected]" 
    else 
    command git "[email protected]" 
    fi 
} 

如果你想diff之前支持任何選項,仍然有它添加--color,你必須讓這個聰明的分析,很明顯。

+0

完美的作品,解決了我的問題。謝謝! – Sudar 2012-04-16 07:06:41

+0

您可能想使用'command git'而不是硬編碼路徑。 – 2012-04-16 15:38:15

+0

我知道有一種方法可以引用原始的外部命令,但對於我來說我記不起來了。 :) – Kaz 2012-04-16 17:49:23

1

你在吠叫錯誤的樹。將color.diff配置選項設置爲auto