zsh alias -> function?

心已入冬 提交于 2019-12-20 09:12:03

问题


Suppose I have:

alias gg="git grep"

then stuff like:

gg "int x"

works, but

gg int x

gets complaints. Is there a way to rewrite gg as a function in zsh so that it takes all the arguments after gg, and stuffs them into a string?

Thanks!


回答1:


gg() { git grep "$*"; }



回答2:


For your particular use case, this is a bad idea. git-grep is expecting a single-arg pattern. You're trying to get the shell to treat your space (between int and x) as part of the pattern. This will break quickly when you try something like: gg foo.*bar or various other things that the shell might interpret. So anything after gg really should be quoted. This is why git grep int x also doesn't work: fatal: ambiguous argument 'x': unknown revision or path not in the working tree...

If you think gg is a worthwhile keystroke saver, you should keep your arg to it consistent with what git-grep expects. Your original alias is fine for this. And continue to single- or double-quote your pattern, just like you do with any other regex-accepting command.



来源:https://stackoverflow.com/questions/2174298/zsh-alias-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!