DEV Community

loading...
Cover image for From Vim to the system clipboard in one command

From Vim to the system clipboard in one command

aminnairi profile image Amin ・4 min read
vnoremap <C-y> :'<,'>w !xclip -selection clipboard<Cr><Cr>

This command, there. It's a life savior! If you are a Vim user and you don't know what this does (or merely) you may want to stick with me to find out.

Configuration

Vim allows you to customize your editing experience by adding some configuration by using the standard Vim configuration file. It is generally located under the /home/$USER/.vimrc file.

$ vim /home/$USER/.vimrc

In Mac OS X and Windows operating systems, the path to this configuration file may differ. I'll let you grab this information on the interwebs.

For GNU/Linux users, this is the typical path to this configuration file. $USER will contain your current username, but you can change the path according to your needs.

Commands

Vim is extremely powerful, and I have yet to learn all the things it hides under the hood. For now, I'll try to teach you more about commands if you are not at ease with this concept.

Commands are a way to add more shortcuts to enhance your editing experience.

map <C-y>:echo "Hello"<Cr>

This will print "Hello" on the status line (the bar at the bottom) whenever you hit the Control + y keys. The <Cr> will just send the enter key, because commands are like macros that automate some keystrokes.

Modes

But this macro will be effective on all modes like the normal, visual or block mode. This may not be what you want so we can limit this command to the visual mode for instance.

vmap <C-y>:echo "Hello"<Cr>

Now, we can use this command only in the visual mode, and nothing else. Generally you'll want to customize the experience based on the mode you are.

And you are probably wondering what is the difference between what I wrote at the beginning of the article and this command. Well, commands can call other commands. You start to see the problem?

Yes! Commands can be called recursively. And while in some rare cases this can be useful, this could hang our Vim program. To prevent this, we simply use vnoremap instead of vmap, and this is the case for all other modes like nmap and nnoremap for the normal mode.

Range Marks

We need to see one more thing before we understand fully what is going on in the final command.

In visual mode, you have access to some special marks, which you can use for referencing the range of your selection when hitting v for instance.

:'<,'>

This little piece of code will get the entire selection. Now we can use it to send its content with the w key to whatever we want. For our needs, we need to send it to the clipboard.

:'<,'>w !xclip -selection clipboard

Actually, if you type this while in the visual selection, this will properly copy your selection to the clipboard if you are on GNU/Linux and that xclip is installed. Xclip is a little utility in GNU/Linux that helps to handle the system clipboard in the command line. There might be other alternatives for your own operating system. I'll let you again check the internets for that.

One more thing: everything that is after ! can be any commands for your operating system. That means the possibility are endless. You could for instance have a command <C-m> that send an email with the selection. Or even <C-c> that will open Google Chrome with the URL of the highlighted visual selection.

Let your imagination do the rest!

The final command for reminder:

vnoremap <C-y> :'<,'>w !xclip -selection clipboard<Cr><Cr>

Why?

Of course, you can do what I did in Vim. Actually, there is a documentation for that if you type :help clipboard. Unfortunately, if your Vim version does not support the clipboard module, you won't be able to use this feature.

$ vim --version | grep clipboard

If you see -clipboard, then you will probably need to use this command. Otherwise, you are good to use the standard Vim idiomatic way of dealing with the system clipboard.

Conclusion

Once you add this little line to your .vimrc file, you'll be able to hit Control + y and copy the visual selection to your system clipboard. Pretty cool, huh?

Shortly after writing this article, after some discussions with the community, and some search, I found that on Arch (the distribution I'm currently running), there is a package called gvim which adds support for the clipboard register. This now enables shortcuts like these ones.

" Leader + C to copy the visual selection to the system clipboard
vnoremap <Leader>c "+y

" Leader + V to paste the content of the system clipboard
nnoremap <Leader>p "+p

Now, my shortcuts are way easier than before. And I got inspired by this video from Luke Smith which makes awesome videos about GNU/Linux and some other stuff. You should definitely check them out!

I'm still pretty new to Vim commands so please, if you see any mistake, feel free to mention them in the comments so that I can improve this article!

Discussion (11)

pic
Editor guide
Collapse
sirseanofloxley profile image
Sean Allin Newell • Edited

Most of the time using the * register for me works pretty well "*yy for example to copy the current line to my clipboard. I'll have to remember this xclip when ny whacko linux setup renders the * register useless for me though!

Thanks!

Collapse
aminnairi profile image
Amin Author

You're welcome Sean!

Indeed, for most Vim users, the * register should work properly. I recently learned that there is a GTK version of Vim which has this feature. Unfortunately mine don't. I'm on Arch so maybe I should try neovim or vim-gtk.

I'll try to remember that if I ever find a vim version on Arch that supports that feature.

Thanks for your comment!

Collapse
talha131 profile image
Talha Mansoor

Unfortunately mine don't. I'm on Arch so maybe I should try neovim or vim-gtk.

@aminnairi , does + register work for you without xclip? I have been copying Vim selection system clipboard using + since ages on MacBook without any additional configuration.

Also it is possible that your Vim version is not compiled with X11 clipboard integration. You can check with :version command. I suspect this is the reason * register did not work for you.

Thread Thread
aminnairi profile image
Amin Author

Thanks for your reply @talha131 !

Yes, we arrived at the same conclusion. I've previously been running this command

$ vim --version | grep clipboard

And the output is

-clipboard         +keymap            +postscript        +vartabs
+eval              -mouse_jsbterm     -sun_workshop      -xterm_clipboard

From what I understood, all minuses mean that there is no support for that feature and unfortunately the clipboard feature is not on the Vim installation I have.

I tried the * and the + registers without any luck...

Thread Thread
talha131 profile image
Talha Mansoor

Right. You will either have to look for a Vim package for your Linux distribution that has clipboard support or compile VIm yourself with all the feature set. Been there, done that!

I migrated to NeoVim a couple of years ago to avoid precisely such issues, and haven't looked back to Vim since then. NeoVim provides a consistent experience and features across all the platforms.

Thread Thread
aminnairi profile image
Amin Author

I waited for an event like this and someone like you to convince me to climb the step and use NeoVim. I guess now is the time!

Collapse
2kabhishek profile image
Abhishek Keshri

I use this in my vimrc

" Copy Paste from X11 Clipboard
vmap yy :!xclip -f -sel clip
map pp mz:-1r !xclip -o -sel clip`z

P.S : Leader is mapped to ","

Here's the full .vimrc

Collapse
aminnairi profile image
Amin Author

Haha! You mapped pp. Got it? ... Okay bye. #jk

Thank you so much for sharing your .vimrc. It's like sharing our grandma recipes... I love it!

Collapse
2kabhishek profile image
Abhishek Keshri

I see what you did there 🤣

Collapse
voyeg3r profile image
Sérgio Luiz Araújo Silva

I have the plugin 'machakann/vim-highlightedyank' that allows me to see what I have just copied.

Collapse
aminnairi profile image
Amin Author

Looks cool! I'll add it to my collection. Thanks!