简化这个bash命令

如何消除在这一行( search for something )两次inputsearch查询?

 git grep "search for something" $(git log -g --pretty=format:%h -S"search for something") 

我的解决scheme

创buildbash脚本/usr/local/bin/git-search

 search_string="$1" git grep $search_string $(git log -g --pretty=format:%h -S$search_string) 

然后我可以使用:

 git search 'search for something' 

你可以把以下内容放在一个文件中,然后在~/.bashrc这个文件:

 function gitgrep { git grep "search for something" $(git log -g --pretty=format:%h -S"$1") } 

为了获得它,你只需要周期文件名. file_with_function . file_with_function

那么你会:

gitgrep "foo bar baz" (需要引号,如果你不想使用引号,可以用$@而不是$1来玩)

你可以把它作为别名:

 alias youralias="git grep \"$0\" $(git log -g --pretty=format:%h -S\"$0\")" 

然后从shell中调用它

 youralias "search for something" 

那对你有用吗?

另一个select是使用bash历史扩展:

 git grep "search for something" $(git log -g --pretty=format:%h -S!#:2) 

虽然所有这些都可以工作,但如果你也想缩短你的git的基本命令,只需在~/.gitconfig创build一个文件并添加一些别名即可。 例如,这里有一些我的:

 $ cat ~/.gitconfig [alias] ci = commit co = checkout f = fetch s = status b = branch d = diff a = add l = log g = grep 

所以现在,你可以发出以下内容:

git g "search for something" $(git l -g --pretty=format:%h -S"search for something")

或者你仍然可以在.gitconfig文件中拥有git别名,但是也可以包含本主题中提出的其他人bash函数,别名等。