Is there a way to set some kind of an alias so that when I do:
cd some/directory
vim .zshrc
It does vim ~/.zshrc
?
The answer is much simpler than you think, because you are asking the wrong question.
If what you really mean is "how can I create an alias that allows me to edit my ~/.zshrc
regardless of my current working directory?" then the answer is simply:
alias zshconfig="vim ~/.zshrc"
This example is from my own ~/.zshrc
and I use it often.
An even better option, suggested by @mateen-ulhaq in the comments below, would be:
alias zshrc="vim ~/.zshrc"
In other words, do not attempt to alias the filename, or the path to the filename. Both of these things are possible, but could create unpredictable side-effects that cause problems for you later, as @michael-homer has pointed out.
But since you know that your preferred $EDITOR
is vim
, simply alias the vim
invocation of that particular file and you should be in business. (It is much safer to alias a specific combination of commands and arguments to an unusual string which is less likely to be used out of context.)
More generally, however, my advice is that you should not be worried about optimizing the two-character sequence ~/
, but instead you should get used to typing it. Go on ... stretch out your left pinky finger like you're Eddie Van Halen practicing scales. (This will help you to master vim
while you are at it, since you probably should get used to hitting the Esc
key with that pinky finger!)
As your typing skills improve, you'll gain muscle memory for the ~/
sequence, which will help you to reliably type that character sequence when referring to files in your home directory. You'll type it quickly on purpose, and you won't type it by accident.
The downside to relying on aliases is that no matter how much they may speed you up on your home computer, they will slow you down when you need to ssh
into some remote Docker container in a client's private subnet and find all your aliases are gone.
.zshrc
filename). In fact, I might change my own alias to that (and update my answer to match)!
Commented
Jun 13, 2021 at 1:06
I don't really see the point, but yes. What you're doing is more complicated than just giving a command a different name or some default arguments, so you need a function, not an alias.
function vim {
if [[ $# -eq 1 && $1 == .zshrc && ! -e .zshrc ]]; then
set -- ~/.zshrc
fi
command vim "$@"
}
Explanation: when you run vim
, it will invoke the function. In the last line, command vim …
invokes the external command, and "$@"
passes it the arguments that were passed to the function. In between, if there is exactly one argument and that argument is the exact string .zshrc
and there is no file called .zshrc
in the current directory, the command line is replaced by ~/.zshrc
. Adjust these conditions if they aren't exactly what you want.
Presumably you are using zsh as your shell, which does have a "global alias" feature that lets you do this:
% alias -g .zshrc='~/.zshrc'
% echo .zshrc
/home/mwh/.zshrc
That is, anywhere that the word .zshrc
appears in the command line, it's replaced with ~/.zshrc
, which is then subject to tilde expansion as usual. It has to be a shell "word" of its own, so it won't be expanded if it's only a substring of a longer name. This works with any command.
Global aliases are infamously problematic because they really do expand anywhere, silently, and so you could for example run rm .zshrc
in another directory and it will take aim at your own dotfiles. They also apply inside shell functions, even those already defined. In this case it seems relatively unlikely to cause an issue, but be aware of the potential for it. You can specifically name a file in the current directory with ./.zshrc
, or get the literal string with \.zshrc
or by surrounding it with quotes.
some/directory/.zshrc
points out to~/.zshrc
.