Vim Tips Wiki

Edit file in existing vim automatically

Redirected from VimTip1260

1,625pages on
this wiki
Add New Page
Talk0 Share
Tip 1260 Printable Monobook Previous Next

created June 15, 2006 · complexity intermediate · author John Little · version n/a

Here's another variation on swapping to a file in another instance of Vim.

In Vim 7, this is what the SwapExists event is for. I use Vim on Windows accessing files via a samba mount onto the development server, and rely on the file type association to start Vim, and typically have half a dozen Vim instances, fighting for space with terminal sessions and applications, and so the swap file warning is a nuisance. Cured with:

function! AskVims()
  let full_name = escape(expand("<afile>:p"), ' ')
  for i in split(serverlist())
    if i != v:servername
      if remote_expr(i, 'bufexists("' . full_name . '")')
        echo 'found in ' . i
        call remote_foreground(i)
        call remote_expr(i, 'foreground()')
        let v:swapchoice = 'q'
  echo "not found"
  let v:swapchoice = ''
auto SwapExists * call AskVims()


Vim 7's distribution contains a file in the runtime/macros directory called editExisting.vim that does this already.

You could just copy it into one of your plugin directories or create a small editExisting.vim in your plugin directory that does this:

runtime! macros/editExisting.vim

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.