Wikia

Vim Tips Wiki

Changes: Open the folder containing the currently open file

Edit

Back to page

(Remove html character entities)
(clean up and merge comment about editing current directory, adding reference to netrw commands)
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
{{Duplicate|493|1336}}
 
{{Duplicate|493|1336}}
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=311
 
|id=311
Line 8: Line 7:
 
|complexity=basic
 
|complexity=basic
 
|author=Thomas R. Kimpton
 
|author=Thomas R. Kimpton
|version=5.7
+
|version=7.4
 
|rating=87/27
 
|rating=87/27
|category1=
+
|category1=File Handling
 
|category2=
 
|category2=
 
}}
 
}}
Occasionally, on Windows, I have files open in gvim, that the folder for that file is not open. This key map opens the folder that contains the currently open file. The expand() is so that we don't try to open the folder of an anonymous buffer, we would get an explorer error dialog in that case.
+
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:
  +
  +
* {{help|:Explore}}
  +
* {{help|netrw-:Explore}}
  +
* {{help|netrw-:Sexplore}}
  +
* {{help|netrw-:Vexplore}}
  +
* {{help|netrw-:Hexplore}}
  +
* {{help|netrw-:Texplore}}
  +
  +
Netrw or some of its replacements (like {{script|text=NERDtree|id=1658}}) also allow simply editing a directory name to bring up a directory browser, for example with <code>:new %:p:h</code>.
  +
  +
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.
   
 
<pre>
 
<pre>
if has("gui_running")
+
if has("win32")
if has("win32")
+
" Open the folder containing the currently open file. Escape properly for Windows cmd shell.
" Open the folder containing the currently open file. Double <CR> at end
+
nnoremap <silent> <C-F5> :if expand("%:p:h") != "" \| exec "!start explorer.exe" shellescape(expand("%:p:h")) \| endif<CR>
" is so you don't have to hit return after command. Double quotes are
 
" not necessary in the 'explorer.exe %:p:h' section.
 
:map <silent> <C-F5> :if expand("%:p:h") != ""<CR>:!start explorer.exe %:p:h<CR>:endif<CR><CR>
 
endif
 
 
endif
 
endif
 
</pre>
 
</pre>
   
==Comments==
+
If you have Vim 7.4.191 or higher, the mapping can be shortened to:
Why have it inside a gui_running block? (Does it make a difference?)
 
   
----
+
<pre>
  +
nnoremap <silent> <C-F5> :if expand("%:p:h") != "" \| exec "!start explorer.exe" expand("%:p:h:S") \| endif<CR>
  +
</pre>
  +
  +
Note, if you are using the {{help|prefix=no|'shellslash'}} option, you will need to temporarily toggle it off in your mapping before using <code>shellescape()</code>.
  +
  +
==Comments==
 
If you prefer to use full Explorer windows (with folder pane etc), modify as follows:
 
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>
 
!start explorer.exe %:p:h,/e<CR>:endif<CR><CR>
 
----
 

Latest revision as of 14:30, April 23, 2014

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