Wikia

Vim Tips Wiki

Edit file in existing vim automatically

Talk0
1,610pages on
this wiki
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'
        return
      endif
    endif
  endfor
  echo "not found"
  let v:swapchoice = ''
endfunction
auto SwapExists * call AskVims()

CommentsEdit

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

Advertisement | Your ad here

Around Wikia's network

Random Wiki