sponsor Vim development Vim logo Vim Book Ad

basic Tip #493: Open the directory for the current file in Windows

 tip karma   Rating 29/16, Viewed by 1661 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   June 24, 2003 9:10      complexity:   basic
author:   Adam Wolff      as of Vim:   6.0

It's often handy to open the folder that corresponds to a file I'm editing (as much as I hate to leave Vim!) I've added this mapping:
   map <C-e>  :silent !explorer %:p:h:gs?\/?\\\\\\?<CR>

So that typing ctrl-e in any buffer opens the folder that the file lives in in Windows.

See :help filename-modifiers for more.

 rate this tip  Life Changing Helpful Unfulfilling 

<< jump to file from :CVSDiff output | maximize or restore window >>

Additional Notes

rpilkey at magma.ca, June 24, 2003 9:42
Good tip, I changed it to this:

map <C-e>  :!start explorer %:p:h:gs?\/?\\\\\\?<CR>

This stops the console window from flashing up on the screen. See  :help !start

Rog
Anonymous, June 24, 2003 12:59
Very good!

I call instead the windows explorer the explorer plugin with:

map <C-e>  :Exp %:p:h:gs?\/?\\\\\\?<CR>

But this works only on Windows. Anybody knows how it should looks for unix?


[email protected], June 24, 2003 15:10

Well, I found I can reuse iexplore.exe, as in the following:

map <silent> <C-E> :sil!!start C:/PROGRA~1/INTERN~1/iexplore -nohome -e <C-R>=getcwd()<CR><CR>

If cygwin is installed, we can have a simple one:

map <silent> <C-E> :sil!r!cygstart -x .<CR>


[email protected], June 24, 2003 15:12
On Unix, try using a free program called Worker:
http://www.boomerangsworld.de/worker/woverview.php3?lang=en

It is an X-based file explorer, similar to Windows explorer.
I had to modify the mapping to:
map <C-e>  :silent !worker %:p:h<CR>
[email protected], June 26, 2003 7:14
For quite a while now I've been doing it this way:

func! OpenCWD()
    if has("gui_running")
        if has("win32")
            let s:stored_shellslash = &shellslash;
            set noshellslash
            !start explorer.exe %:p:h
            let &shellslash; = s:stored_shellslash
        elseif has("gui_kde")
            !konqueror %:p:h &
        elseif has("gui_gtk") " TODO: test!
            !nautilus %:p:h &
        elseif has("mac") && has("unix")
            !open %:p:h
        endif
    endif
endfunc

command! OpenCWD call OpenCWD() " or whatever you like

I'm not sure how flawless it is, but it has worked for me on a couple of different platforms.
[email protected], June 26, 2003 19:07
A solution that I like is to use the vi file manager (http://vifm.sourceforge.net).  You can use it with or without being in vim (a vim plugin comes with the distribution). Unfortunately, I have not been able to get it to compile in cygwin on windows yet, and I am on a windows machine right now, so I can't access it to give more of the specifics.  I think the plugin comes with a command like :Vifm, but, that could be mapped.
sgovindachar at yahoo, June 27, 2003 8:38
Since I perfer to stay within Vim, I use the following:
  :sf %:p:h
This splits the window and lists the contents of the
directory of the file.  Putting the cursor on an item from
the list in that window and typing o opens up a new window
with the contents of the item.
Anonymous, June 4, 2005 19:26
How is this different from ":Sexplore" or the shorter ":Sex"?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo