English 中文(简体)
zsh alias -> function?
原标题:

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!

最佳回答
gg() { git grep "$*"; }
问题回答

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.





相关问题
zsh alias -> function?

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 ...

zsh handling of arguments

In the zsh programming language, how do I get the entire list of arguments as a string in "" ? i.e. in myzshcommand 1 2 3 foo bar I want something to match me "1 2 3 foo bar" Thanks!

Tell if a user has SUed in a shell script?

I have a script which executes a git-pull when I log in. The problem is, if I su to a different user and preserve my environment with an su -lp, the script gets run again and usually gets messed up ...

How do I require a minimum version of zsh?

I want to use a feature only present in newer versions of zsh: [[ "$foo" =~ "regexp" ]] where regexp is a regular expression. Is it possible to do this check? Something like: if [[ $ZSH_VERSION &...

Rename files based on sorted creation date?

I have a directory filled with files with random names. I d like to be able to rename them file 1 file 2 etc based on chronological order, ie file creation date. I could be writing a short Python ...

ZSH Prompt/RPrompt Conflict

I currently have my ZSH PROMPT variable set up as: PROMPT=$ [%{e[0;32m%}%n@%m:%~%{e[0m%}]> and my RPROMPT variable as: RPROMPT=$ [%{e[0;32m%}%Te[0m%}] The effect I m working for is to make ...

热门标签