Wikia

Vim Tips Wiki

Open the folder containing the currently open file

Talk0
1,612pages on
this wiki

Redirected from VimTip311

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 311 Printable Monobook Previous Next

created August 13, 2002 · complexity basic · author Thomas R. Kimpton · version 7.4


Occasionally, on Windows, I have files open in gvim, that the folder for that file is not open for browsing.

The easy way to browse a file's directory without leaving Vim, is to use one of the built-in Netrw commands to open the current file's directory:

Netrw or some of its replacements (like NERDtree) also allow simply editing a directory name to bring up a directory browser, for example with :new %:p:h.

If you prefer the Windows Explorer GUI, this key map opens the folder that contains the currently open file, checking first to prevent trying to open the folder of an unnamed buffer.

if has("win32")
  " Open the folder containing the currently open file. Escape properly for Windows cmd shell.
  nnoremap <silent> <C-F5> :if expand("%:p:h") != "" \| exec "!start explorer.exe" shellescape(expand("%:p:h")) \| endif<CR>
endif

If you have Vim 7.4.191 or higher, the mapping can be shortened to:

nnoremap <silent> <C-F5> :if expand("%:p:h") != "" \| exec "!start explorer.exe" expand("%:p:h:S") \| endif<CR>

Note, if you are using the 'shellslash' option, you will need to temporarily toggle it off in your mapping before using shellescape().

CommentsEdit

If you prefer to use full Explorer windows (with folder pane etc), modify as follows:

!start explorer.exe %:p:h,/e<CR>:endif<CR><CR>

Around Wikia's network

Random Wiki