Vim logo vim online Vim Book Ad

 Tip #309: close vim you left open remotely

 tip karma   Rating 21/12, Viewed by 925 

created:   August 12, 2002 22:27      complexity:   intermediate
author:   noah      as of Vim:   6.0

Vim 6 has this cool client-server protocol. I use it all the time to edit a file in an existing gvim, like so
$ gvim --remote [filename]

Today I left myself logged in at the console at work, and when I got home I realized I had left vim running with files  unsaved. I think I even left it in insert mode. I wanted to edit these files at home. So I ssh'd to the machine and started playing with the --remote commands.

:help was a bit cryptic
   --remote-send {keys}         Send {keys} to server and exit.

After a lot of failed attempts, I finally succeeded in getting the remote vim to save its buffers and quit.

$ DISPLAY=:0 vim --servername GVIM --remote-send '<ESC>:wqa<CR>'

A couple of notable things. Then environment variable DISPLAY has to be the display of the remote vim, and you have to be able to open that display. The client-server stuff is done through X.

The <CR> is important. This part eluded me for a long time. The {keys} are just like keys you would press if you were editing at the console, and you have to press enter, or vim won't do anything.

Check your .swp files to make sure vim really closed the files it was editing. Vim provides little feedback as to the success or failure of what you're trying to do remotely. Nonetheless, it's clearly a useful feature to have available.

 rate this tip  Life Changing Helpful Unfulfilling 

<<Move through wrapped lines. | showing ascii value of the current character in decimal, hex, and octal >>

Additional Notes

[email protected], August 13, 2002 11:09
Yeah. Its pretty cool, and provides an edge over other editors.
Anonymous, September 28, 2002 10:54
To get better feedback you can use the :redir command to redirect messages to a file, like this:
vim --servername GVIM --remote-send ':redir >> outputfile<cr>'

You can also evaluate remote expressions, ie:
vim --servername GVIM --remote-expr 'mode()'
this will tell you what mode vim is currently in and
vim --servername GVIM --remote-expr 'getcwd()'
will tell you what directory vim is working in
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 [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo