Use Vim as a daemon

I discovered recently the --remote argument elinks takes. It proved useful to me. In general I like the concept of daemon for almost any app. This way, I practically can have my non-X11 web browser running in the background as a daemon, and open tabs on it from anywhere else, for example, reading my email or a news feed, and I want to open a link from there.

For torrents I use deluge, which I use without an interface: no X11, no ncurses, but no command line either. I just send commands to it. For music, I use Cmus, and its remote feature as well. I treat Cmus as a daemon.

So the next step was using my favorite text editor as a daemon. A duckduckgo search revealed that there is a remote feature for Vim, only that ... it is available only on a GUI. That is a feature for gVim, or for Vim if it is running in a GUI terminal emulator, or without a GUI but on a computer that has X11 installed. Now, as elinks, cmus, deluge, and many other pieces of software demonstrate, X11 is not needed in order to use an app as a daemon. The remote feature of Vim was written specifically to work with X11, on purpose. This is an inconvenience for me, just as using an X11 web browser to connect to a Wi-Fi network in a hotel is (you can issue a command from a TTY on a Linux laptop without X11 to get Wi-Fi, but some implementations require a web browser, which actually amounts to requiring X11).

I try to reduce firing up X11 to a minimum. All my computers boot into TTYs. The startx command is an option to me, and should remain so. This is my personal liking and choice, this is an arrangement that works for me. So using gVim and overload my RAM and CPU with X11, not to mention all the distracting and usually annoying visual that comes with it, only to use a version of Vim as a daemon... thanks but no thanks.

GNU Screen comes to the rescue. With GNU Screen you can practically turn just any application into a daemon. I'm sure something similar could be arranged for tmux or other terminal multiplexers. GNU Screen is not the solution, it is a solution that works to my liking.

Fire up screen then have a windows called "vim". Then, from anywhere else in your system, for example from a different screen window, or from the plain shell after dettaching screen, issue this command:

screen -p "vim" -X stuff ":edit! /file/to/edit $(printf \\r)"

This command effectively sends the file to be opened by the Vim instance running in the background. You get the prompt back on your shell immediately.

There are two downsides. One, Vim has to be in normal mode when receiving this command. But it is not really necessary for it to, since you can simply add a printf <Esc> code before :edit. You can duckduckgo for that <Esc> code. I always leave my Vim in normal mode anyway, so that line works great for me without the <Esc> inserted there. Note that if Vim is in a mode other than normal mode, <Esc> will put it in the normal mode, and if Vim was already in normal mode, <Esc> is simply superfluous. You can't damage anything.

Two, you have to provide the full path to the file. Big whoop.

Good news, you can also skip having to provide the full path. Integrate this command with Midnight Commander. The combo Vim, GNU Screen and Midnight Commander is a great way of daemonizing Vim. Great indeed.

In Midnight Commander, add these lines to the user menu:

v       Send to Vim through Screen
        screen -p "vim" -X stuff ":edit! %d/%p $(printf \\r)"

The %d/%p will handle the full path for you. Problem solved. Now, when browsing files with mc you can press <F2>+v to send those files to Vim for them to be opened there, in the background. You have a comfortably usable Vim daemon right there.

This behavior kind of replicates what the normal is in a, sigh, visually overloaded environment, like using a GUI. You open Thunar, Nautilus, Dolphin or any major file manager for Linux, double click on a .odt file, and those are opened with the same instance of LibreOffice (or whatever), in diferent tabs, and LibreOffice can be running in the background (the window can have no focus and be minimized, that is).

Arguably, some may point out that this solution I propose makes Vim dependent on screen. I take that. But then again, if you are seeking to use Vim as a daemon, it is very likely that the need arises from precisely having Vim running in the background in a different window of screen or another terminal multiplexer.

Overall, it is a pity that Vim does not have the --remote argument on for computers without X11 installed.

Hope you people find this useful. Send feedback to .