Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

Server and client mode in Vim

Posted by ajay on October 21, 2009

Here is another interesting and useful feature of vim. We can start vim as a server which will send and receive commands and execute them.

The first use-case of the above feature is to give vim a one-instance functionality, just like Firefox. We click on a link anywhere in the system, it doesn’t open a new Firefox window. Just creates a new tab in the existing Firefox instance.  To achieve similar functionality in vim, we need to start vim as a server –

[command_promot]$  vim –servername SAMPLESERVER file1.txt

Done. Now you can go to any shell in the system, and to open a file in the existing vim instance, just do –

[command_prompt]$ vim –servername SAMPLESERVER –remote-tab file2.txt file3.txt

The above command will open file2.txt and file3.txt as new tabs in the existing vim instance running :).

If the above typing is very long and cumbersome, you can just set a couple of aliases [one for command to start the server and another for opening the file in existing server]. You can also choose any server-name and you can also run multiple servers in your system [A possible use-case when you’re working on multiple projects and run one vim instance for every project and while opening the file you decide which vim instance to use for opening this file]. The servers are recognized by the –servername option which you give while starting the server.

The server has additional functionality in which it can receive a command and execute it. This can be quite handy when you use multiple computers and login to a server machine for working. Example – You were using machine A and had a vim instance running.  You left this machine and started using machine B and would like to continue editing where you left on machine A. If your vim instance was started as a server (assuming server name to be SAMPLESERVER), then from machine B you can just login to machine A and execute the following command –

[command_prompt_on_machine_A]$ vim –servername SAMPLESERVER –remote-send ‘<Esc>:mksession ~/sessionFile.vim<CR>:wqa<CR>’

Essentially this command will send the existing server running by name SAMPLESERVER, to store all the progress in ~/sessionFile.vim, save all files and exit :). [If you dont know what mksession does, please read my earlier post about sessions in vim]. Now to continue with the same settings, you start your vim instance by picking up the settings from the session file –

[command_prompt_on_machine_A]$ vim –servername SAMPLESERVER -S ~/sessionFile.vim

Done. This will allow you to edit files in one single environment, even if you work on different machines :). Happy Vimming.

PS: vim needs to be compiled with +clientserver in order for you to be able to use this feature.


16 Responses to “Server and client mode in Vim”

  1. maruti borker said

    nice !

  2. Rajesh S R said

    Great post! Learning a lot of vim from ur blog. Keep it going!

  3. […] like to hear that!) I’ve heard the same of Emacs programmers. (Did you know you can setup a VIM SERVER?) I, myself, would be “absocrippled” were I to lack bash (thus: cygwin). But presently, […]

  4. […] above commands and see what would happen! For more options of vim, please access this and ajay’s blog for […]

  5. Anna said

    Hi there colleagues, how is everything, and what you
    want to say concerning this article, in my view its in fact remarkable designed for me.

  6. Mike said

    WordPress converted “–” to &emdash, so your command lines are broken.

    • Mike said

      WordPress converts “hyphen hyphen” to embdash in comments, too. 😦

    • Lilly said

      It is especially rewarding to see you continuing to practice what you preach in a simple and unauoivqcel way. Thanks for your willingness to spend your time in the lab testing what really works and then freely sharing the results with the rest of us.

  7. Wow, awesome blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your website is wonderful, let alone the content!

  8. i am ur big fan keiry i need ur picturs and some vedios pls to me Click https://twitter.com/moooker1

    • Lenna said

      Meraviglioso, lo stesso vale in qualche modo anche per noi lettori, sempre considerati eterni bambini, sempre sbeffegiati e considerati lettori di sen7id&#821o;ordcne. Ma forse è proprio questo che ci rende così forti

  9. sam said

    Can You show how to set aliases. instead of long typing that would be greatly helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: