ZSH alias with parameter
Solution 1
You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the []
.
gitall() {
git add .
if [ "$1" != "" ] # or better, if [ -n "$1" ]
then
git commit -m "$1"
else
git commit -m update
fi
git push
}
*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.
Solution 2
If you really need to use an alias with a parameter for some reason, you can hack it by embedding a function in your alias and immediately executing it:
alias example='f() { echo Your arg was $1. };f'
I see this approach used a lot in .gitconfig aliases.
Solution 3
I used this function in .zshrc file:
function gitall() {
git add .
if [ "$1" != "" ]
then
git commit -m "$1"
else
git commit -m update # default commit message is `update`
fi # closing statement of if-else block
git push origin HEAD
}
Here git push origin HEAD
is responsible to push your current branch on remote.
From command prompt run this command: gitall "commit message goes here"
If we just run gitall
without any commit message then the commit message will be update
as the function said.
Solution 4
"git add ."
and the other commands between "
are just strings for bash, remove the "
s.
You might want to use [ -n "$1" ]
instead in your if body.
Solution 5
I tried the accepted answer (Kevin's) but was getting the following error
defining function based on alias `gitall'
parse error near `()'
Hence changed the syntax to this, based on the git issue and it worked.
function gitall {
git add .
if [ "$1" != "" ]
then
git commit -m "$1"
else
git commit -m update
fi
git push
}
albttx
Gratuated student in 42 School. Gopher and DevOps while (42) learn_everything_as_possible(); From France
Updated on February 21, 2022Comments
-
albttx about 2 years
I am trying to make an alias with parameter for my simple git add/commit/push.
I've seen that a function could be used as an alias, so I tried but I didn't make it.
Before I had:
alias gitall="git add . ; git commit -m 'update' ; git push"
But I want to be able to modify my commits:
function gitall() { "git add ." if [$1 != ""] "git commit -m $1" else "git commit -m 'update'" fi "git push" }
-
chepner over 8 years
csh
does, but it doesn't have functions at all. (I don't know if there are no functions because aliases can take parameters, or if aliases take parameters because there are no functions, or what.) -
archae0pteryx over 6 yearsSo you would call it (from the shell) like
gitall "my commit message"
? or would you call itgitall('my commit message')
-
Kevin over 6 years@archae0pteryx functions are called exactly like any other command, so
gitall "my commit message"
. -
rococo over 6 yearsSo hacky and yet so beautiful
-
tripleee over 6 yearsWhy make an alias at all? Just call the function
example
. -
Charles Duffy over 5 yearsI'd suggest
getall() {
without the precedingfunction
-- sure, it's legal either way in zsh, but that sole change will make this compatible with all POSIX-compliant shells. -
Kevin over 5 years@CharlesDuffy makes sense, sure.
-
Charles Duffy over 5 yearsBTW, if you used
git commit -m "${1:-update}"
(a parameter expansion with a default provided), then you wouldn't need theif
statement at all. -
tripleee over 5 yearsAlso, belatedly, you need a semicolon before the closing brace.
-
MayTheSForceBeWithYou over 4 yearsThis was beautiful. With this I was able to make an alias that adds an alias to an rc file, then reloads said rc file. ❤️ alias addalias='f() { echo "alias" $1 >> ~/.zshrc && . ~/.zshrc };f'
-
maoizm about 4 yearsno need to add any names into global scope, just use anonymous function:
alias example='(){ echo Your arg was $1. ;}'
-
gihanchanuka over 3 yearsTried using the function. Works, but autocomplete is not working as expected. In my case, I want an alias for Git, and after adding function, zsh auto-suggest folder names rather branch names.
-
Jimmy Adaro almost 3 years@maoizm Great piece of code! I'm using it like this:
alias dexec='(){docker exec -it $1 /bin/bash;}'
to access a Docker container in 1 command. Thanks! -
maoizm almost 3 years@triplee Quite surprisingly for me Zsh tolerates missing
;
before}
-
tripleee almost 3 years
-
maoizm almost 3 years@tripleee I have removed
bash
tag as it leads to misunderstanding + zsh definitely treats aliases differently from bash -
Chris Perry almost 3 yearsNote that this must be written with single quotes (as it is in the original example). I was using double quotes at first and it was breaking functionality. Not sure why tho ¯\_(ツ)_/¯
-
Good Pen over 2 yearsWhat a clear answer!
-
Good Pen over 2 years+1: Why make an alias at all? Just call the function example. example_func() { echo Your arg was $1 ; }
-
nicolas over 2 yearsis there any benefit of having an alias, or is it just more syntax ?
-
jan almost 2 years@maoizm Your example works, but I don't understand why that anonymous function gets called at all
-
maoizm almost 2 years@jan this is by design: anonymous function is invoked immediately at its lexical scope. and there is no sense to store them in shell’s lookup table as they can’t be referred to later. they get discarded immediately after invocation