Sharing the Vim clipboard along different Vim instances, even over the network.
It is well known by Vim users that is very easy to yank and paste text from any buffer to another one meanwhile you are in the same Vim instance. That means that if you have vimInstance1, vimInstance2 … vimInstanceN you can’t yank text from a buffer in vimInstanceN and paste it to vimInstanceM.
There are some aproachs to achieve this using terminal multiplexers like Screen or Tmux, but what happens when you have this?:
That is how I work. For each project one vagrant machine as I wrote before here. For each project one vim instance in the virtual machine with all my plugins, a ssh connection to control the virtual machine and then a terminal in the host to control the Git repo.
If you want to copy some text from the editor window, remember, vim is running in the virtual machine where you don’t have X server, to paste it somewhere else inside the same machine, or inside the host machine to paste it in the browser because you want to post it in a help forum, or perhaps you want to copy a code fragment inside another related project, then you just can’t do it in an easy way because you run into this issue:
If you want to copy the green selection, you only have access to the orange (Tmux), or red (X server mouse) selections. There is several plugins that try to solve this like vim-slime, but what they do is to send Vim selection to a specific Tmux window and pane. Anyway the following schema shows how I work:
How the hell can I copy from one editor to another if each one is even in one different virtual machine with no X at all, I cannot even know what is going to be the Tmux target window and pane, well, I will know the pane number, but not the window number because it will depend each time. But even if I could determine both targets, Tmux is running in the host, and Vim is running in the virtual machine. How are they going to communicate?
Finding the solution
One day at work I told myself that should be out there a solution for this. I took a paper and start drawing those thoughts: what I have here are several computers connected in a private network, so perhaps I could have a vim server in my host master machine, and connect to that vim remote session from the virtual machines:
I already knew that Vim has a client-server mode, I thought it uses the system sockets and buses, but after some testing I realized I was wrong. For instance, in the virtual machines after executing vim — version the option clientserver wasn’t available. I thought it was a problem with the Ubuntu’s distributed package, so I downloaded sources and compiled it with explicit clientserver support, but it didn’t compile with that option. After some research I found that Vim’s clientserver support relays on the X clipboard, so without X there is not clientserver support.
After that I gave it up, but next day surfing Stackoverflow I read that Vim was not designed to have session sharing support, in response to someone who was asking exactly the same I was looking for. I got in shock, but I didn’t gave it up.
Further reading in Stackoverflow took me to the right clue, once again someone asking the same, the winning answer tells about using tabs, not bad solution, but in my case to use this approach should have just one virtual machine with all the projects there, messing everything up… I don’t like that solution, but suddenly, further reading in that topic I found a good answer.
Why not to use a file for the yanked text?
This guy gave the solution: You select text in visual mode, and save it to a file in your system, then in another vim instance you can read that file pasting its content inside the current buffer. You map those actions in your .vimrc and then suddenly you can copy paste along different Vim instances. Cool!
So right now we have a solution for yank/paste along different Vim instances, but one moment, this is only usefull meanwhile you are working in the same machine with different Vim instances, but this will not work for my workflow where each project editor is running in different virtual machines, but then the bulb lights up: Network.
If the master file with the Vim shared copy buffer is located in the host, then I could access that file from all over the world through the network with a proper and secured setup. But in this case even security is not a must have because I won’t access the host from outside my desktop. So what I did is to install sshfs in the virtual machines, in the host I created ~/.vimsharedbuffer/ directory, and the on the virtual machines I mounted the remote directory in the same location: ~/.vimsharedbuffer/ then, I changed the mappings in my .vimrc in my dotfiles repo:
vmap <C-c> :w! ~/.vimsharedbuffer/.vimbuffer<CR>
nmap <C-c> :.w! ~/.vimsharedbuffer/.vimbuffer<CR>
“ paste from buffer
map <C-y> :r ~/.vimsharedbuffer/.vimbuffer<CR>
Then I have this approach:
So at this point I can select text in any vim instance wherever it is, virtual machine, remote computer, host system… press Ctrl + c and it will be wrote in the shared file, then from another Vim instance in the same computer, in a virtual machine, or even in a remote machine, you can press Ctrl + y (in my case) and the file is inserted below the cursor in the current vim instance buffer.
What can you do with this? The limit is your imagination.