Today I learned how to create aliases in PowerShell. I’m switching from Mac to Windows, and I want the terminal in VS Code to do what I want.
No terminal will work for me until it interprets gs
as git status
. I type that compulsively.
In bash, setting that up looks like this:
alias gs='git status'
But in PowerShell, aliases can only refer to single words. No parameters. Wat.
You can make a function with the whole command in it, and then set an alias to that function.
Function GitStatus { git status } Set-Alias gs GitStatus
The first time I did this it felt kinda silly. But then the second time …
Function CommitDangit { git add . git commit -m "temp" } Set-Alias c CommitDangit
This alias c
makes a crappy commit as quickly as possible. I use it when live coding, to make insta-savepoints when stuff works. (I’m a bit compulsive about committing, too. Just commit, dangit!)
The PowerShell syntax requires a long name for my command before I give it a short one. This is more expressive than the bash:
alias c='git add . && git commit -m "temp"'
My CommitDangit
function is named for readability, plus a tiny alias for fast typing.
This is a win. I like it more than the bash syntax. PowerShell is a more modern scripting language, and it shows.
Bonus: in bash I put those aliases in a file like .bashrc
or .bash_profile
or sometimes another one, it depends. In PowerShell, I put the aliases in a file referenced by $profile
. Edit it with: code $profile
, no figuring out which file it is.
Next: reload the $profile in an existing window with . $profile