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.
maruti borker said
nice !
Rajesh S R said
Great post! Learning a lot of vim from ur blog. Keep it going!
Tisha said
Your post is a timely cotroibutinn to the debate
Diffed and Merged…and Ported « Bitratchet said
[…] 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, […]
Indy said
I’m not easily imspersed but you’ve done it with that posting.
An introduction about vim server | Forest Wang said
[…] above commands and see what would happen! For more options of vim, please access this and ajay’s blog for […]
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.
Vina said
That’s a sharp way of thniking about it.
car and home owners nsurance quotes in Texas said
#26 JC Thanks for the balanced and honest appraisal of your town. It sounds like what I am looking for. I really do appreciate your insights. Refreshing from the typical cheer leading so many engage in when it comes to their town. I will keep you posted.
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.
natachasteven.com said
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!
hillcasey62296 said
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̵o;ordcne. Ma forse è proprio questo che ci rende così forti
sam said
Can You show how to set aliases. instead of long typing that would be greatly helpful.