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?

  • 1
    You could create a symlink so that some/directory/.zshrc points out to ~/.zshrc.
    – thiagowfx
    Jun 12, 2021 at 1:15
  • 3
    – muru
    Jun 12, 2021 at 2:46
  • @muru Thanks, this was what I was looking for.
    – Josh Cho
    Jun 13, 2021 at 3:33

3 Answers 3


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.

  • 5
    This is a much safer way to do it.
    – user10489
    Jun 12, 2021 at 5:15
  • 1
    I prefer alias zshrc="vim ~/.zshrc" Jun 12, 2021 at 21:46
  • @MateenUlhaq I almost suggested that! It's a great choice for the alias, since it does not collide with anything executable (and is also not an identical string to the .zshrc filename). In fact, I might change my own alias to that (and update my answer to match)!
    – Matt Obert
    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
  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.

  • 1
    This is a very smart approach which answers the OP's question without causing undesirable side effects. The only thing I don't like is that it reinforces bad habits.
    – Matt Obert
    Jun 12, 2021 at 19:56

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

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.

  • 4
    "Global aliases are infamously problematic ..." Truer words were seldom spoken. There is a better answer coming soon. (I'm on mobile and typing relatively slowly.)
    – Matt Obert
    Jun 12, 2021 at 3:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .