A common task I find myself repeating when editing files is opening the enclosing folder of the current file in Finder. Many IDEs and Mac apps call this feature “Reveal in Finder”.

Now that I’m trying to do more of my text editing in vim, I wanted to setup a shortcut to do this. Here’s what I came up with:

" ~/.vimrc
command! -nargs=1 Silent execute ':silent !'.<q-args> | execute ':redraw!'
nmap <Leader>o :Silent open %:h<cr>

The first line sets up a Silent command that executes a CLI command silently, without changing what’s currently being displayed or showing the stdout of the command1.

The important bit is on the second line: open <PATH> opens the provided path in Finder2. In vim, % refers to the path of the current buffer. Adding :h tells vim to use the “head” of the path – that is, the enclosing directory. So, %:h expands to the directory of the current file. You can try this out with :echo expand("%:h").

Put together, I can now type ,o (I have , as my leader key, but the default is \), and the folder of the current file is opened in a new Finder window.


  1. To see what the default behavior is, try running :!echo foo. vim pops you over to a separate window (?) that shows the stdout of the command. Since I don’t care about the output of open, this trick allows us to circumvent the transition to that other view. ↩︎

  2. open is actually a pretty general commend – it can be used to “open” any file. For example, open somevideo.mov will open that video file in whatever your default media player is. On Linux, the equivalent is xdg-open↩︎