23

I'm seeing something very odd, and honestly I'm stumped.

The version of vim that comes with mac is outdated (7.3 instead of 7.4). I'm trying to install vim from homebrew, and I want to use that one instead of the default apple version.

I ran "brew install vim". It installed correctly in /usr/local/bin/vim. All good.

When I run "which vim", it prints "/usr/local/bin/vim". The Apple version of vim is installed at /usr/bin/vim. So the which command is telling me that I'm using the homebrew version of vim.

However, when I actually run vim, it still runs the Apple version

$ vim --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Jul  9 2015 23:58:42)
Compiled by root@apple.com
...

If I run the homebrew version explicitly, I see this:

$ /usr/local/bin/vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Oct 23 2015 18:16:35)
MacOS X (unix) version
Included patches: 1-898
Compiled by Homebrew
...

I even tried moving /usr/bin/vim to /usr/bin/vim73 to try to force using the homebrew version. However, when I did this, here is what I see when I try to run vim:

$ vim --version
-bash: /usr/bin/vim: No such file or directory
$

What is going on? How can I get it to run the homebrew version of vim?

bfontaine
  • 18,169
  • 13
  • 73
  • 107
kgreenek
  • 4,986
  • 3
  • 19
  • 30
  • Do you have an alias? Use `type` instead of `which` to check. In bash `which` is not a shell builtin, but `type` is. – janm Oct 24 '15 at 01:56
  • The other obvious problem is the hash table if you haven't restarted your shell: `hash -l` shows the contents of the hash table in bash. – janm Oct 24 '15 at 02:00
  • Ah thanks! When I type "type vim", I see "vim is hashed (/usr/bin/vim)". When I type "hash -l", I do see vim in the list. – kgreenek Oct 24 '15 at 02:03
  • To fix it the hacky way, I added 'alias vim="/usr/bin/local/vim"' to my .bash_profile. That seems to work. Is there a nicer way? – kgreenek Oct 24 '15 at 02:04
  • 1
    Don't add an alias, no need to modify .bash_profile. Let the path do it for you. A fresh shell will get a fresh hash table, or just do `hash -r` to clear the hash table and let it get built again. – janm Oct 24 '15 at 02:08
  • I've tried quitting the terminal, and I've definitely resett the computer, but it didn't fix it. – kgreenek Oct 24 '15 at 03:08
  • please also add the output of `alias vim`. – cel Oct 24 '15 at 04:34

2 Answers2

55

Start a new shell session and it’ll work.

Bash caches executables’ paths so when you run vim it looks at your PATH to find the first executable with this name. It caches it and the second time you run vim it remembers vim is actually /usr/bin/vim and runs that.

Since you haven’t restarted your Bash session its cache is still the same; hence the error you’re seeing. It has nothing to do with the way you installed vim.

If you don’t want to start a new shell session, you can run hash -r to tell Bash to clear its executables memory.

bfontaine
  • 18,169
  • 13
  • 73
  • 107
3

You forgot an argument:

$ brew install vim --override-system-vi
romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    This option doesn’t exist. – bfontaine Oct 24 '15 at 09:20
  • @bfontaine, yes it does: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/vim.rb#L14. Sorry for the typo. – romainl Oct 24 '15 at 11:10
  • I don’t think it doesn’t do what you think. `--override-system-vi` make the formula installs a `vi` binary that’s a symlink to `vim`. Technically it doesn’t override anything; we should probably rename it: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/vim.rb#L99 – bfontaine Oct 24 '15 at 11:12
  • @bfontaine is right; it installs a `vi` symlink to `vim`, nothing more. updated link to code: https://github.com/Homebrew/homebrew-core/blob/master/Formula/vim.rb – Adnan Nov 03 '17 at 20:04
  • 3
    `--override-system-vi` was deprecated; use `--with-override-system-vi` instead – serghei Mar 28 '18 at 05:01