I have recently upgraded to Vim 8 using Homebrew, but my
vi commands still load the OS-provided version 7.4 of Vim. I could add an alias to
vi, but is there another way?
This is happening because the OS's
vi is ahead of Homebrew
vi in the
While you could fix it by putting
/usr/local/bin ahead of
/usr/bin in the
PATH, that would be a security hole since Homebrew gives ownership of that directory to your user. That permission change from the macOS default means that even an extremely unsophisticated malware could use this hole to get root privileges. All they'd have to do is add some other common command here like
ls, then pass the commands through to
/bin/ls until they see you've run it through
sudo, then they take over.
This is a general principle, not specific to Vi, Macs, or Homebrew at all: never place user-writeable
PATH elements ahead of those that can only be modified by
There are several ways to solve this problem:
Replace the OS's
vimbinaries with symlinks to the Homebrew versions:
$ cd /bin $ sudo mv vi vi-7.4 $ sudo ln -s /usr/local/bin/vi . $ cd /usr/bin $ sudo mv vim vim-7.4 $ sudo ln -s /usr/local/bin/vim .
I do not recommend this solution, but it is what the original version of the question asked for, so there it is.
Notice that this doesn't delete the OS-provided versions, just renames them so you still have access to them by appending
-7.4if you need to later.
Since all three of the directories in the prior solution are in the default user
PATHon macOS, you don't actually need the symlinks unless you have local software that has hardcoded
vim. Just move the system versions shadowing Homebrew's versions out of the way:
$ sudo mv /bin/vi /bin/vi-7.4 $ sudo mv /usr/bin/vim /usr/bin/vim-7.4
vimcommands to fall through to the Homebrew versions in
Both solutions above will break the next time you upgrade macOS because the OS installer will put the Apple-provided versions back in
/usr/bin, so the solution I actually recommend is to add aliases to your
alias vi=/usr/local/bin/vim alias vim=/usr/local/bin/vim
This lets you switch back to the OS-provided version of Vim easily by quoting the alias:
$ 'vi' somefile.txt # forces use of /bin/vi
You might also want to add this to
Several commands use
$EDITORinstead of hard-coding a call to
Another option you have is to remove the Homebrew build of Vim and install MacVim instead. It also installs to
/usr/local/bin, but unlike Homebrew Vim, it installs a command that isn't shadowed by the OS version,
mvim. By default, that command launches the GUI MacVim editor, but with this alias, you can have your
viand your Terminal editor, too:
alias vi='mvim -v'
-vflag tells it not to launch the GUI, but to edit in the Terminal instead.
If you choose this option, make a similar change to the
Among the many nice features you get with MacVim is integration with the OS clipboard: pasting from the OS clipboard into Terminal.app running stock or Homebrew Vim you already have, but I find it nice to go the other direction as well: "yank" commands in
mvim -vunder Terminal.app also sends the yanked text to the OS clipboard so I can paste it into some other program.