Open file in Vim in Explorer

I’ve been using Vim for a while, and I’m also a hardcore Directory Opus (Dopus) fan. I find that both of them complement my work well, allowing me to greatly speed up the way I handle files and edit them. However, I’ve always wanted a way to move from the very keyboard-centric Vim to a full-fledged file manager. That may be Dopus, or plain vanilla Windows Explorer, or whatever your favorite file manager utility happens to be.

This is what Dopus looks like, with my preferences configured.



But I digress.

My gripe is that extracting the file name and path in Vim is a tad inconvenient, not to mention that after extracting it to, say, the clipboard, I then have to open up the Windows Explorer, paste in the full path, remove the file name, hit enter, before I can finally browse the directory in a usual manner.

Certainly, Vim has many options, and for those who may be thinking: “Well, Vim can return you just the file path”, yes, I’m aware. My point is really just to illustrate that moving from Vim’s view of editing a given file, to browsing the directory that the file lives in, is not exactly the most intuitive process in the world. Also, while Vim certainly has some options for dealing with directories, and some of them are not bad at all, such as the venerable NERDTree, it’s really not what I need. I don’t just need to browse a directory structure, but I often need to shuffle files around, rename them, map network shares, and so on. That is firmly in the realm of file managers, not an editor – Vim. Hence, I want my file manager.

As such, here’s a little Vim function (it’ll sit nicely in your vimrc file, or anywhere else you prefer to put it), that does all that for you. I like -based keystrokes, so for me, hitting -e (natural and similar to Win-E to me) will pop the file in Dopus, if Dopus exists on the system. If Dopus isn’t there, then it falls back to the traditional Windows Explorer. Such a fallback is for reliability, and also because I use Vim on multiple VMs that will not have Dopus installed. Finally, it also highlights the file so you can see it easily.

" Open current file in Explorer (Dopus version)
function OpenPathInExplorer()
    let filepath=expand('%:p')
    let dopuspath="C:Program FilesGPSoftwareDirectory Opusdopusrt.exe"

    if filereadable(dopuspath)
      :exe '!start "'. dopuspath .'" /cmd Go "' . filepath . '" NEW | redraw'
      :exe '!start explorer.exe /select,"' . filepath . '" | redraw'
nmap <leader>e :call OpenPathInExplorer()

If you don’t like the Dopus bits, feel free to strip them. Alternatively, here’s a plain, simple, vanilla, works-on-all-Windows-systems version:

" Open current file in Explorer (simple version)
function OpenPathInExplorer()
    let filepath=expand(&#039;%:p&#039;)
    :exe &#039;!start explorer.exe /select,"&#039; . filepath . &#039;" | redraw&#039;
nmap <leader>e :call OpenPathInExplorer()

There we go. The command can be seen at status area of Vim. I can’t show the keystrokes, though. ;)


Hope you find this useful. :)