How to make cut/copy/paste in GVim on Ubuntu work with Ctrl+X,Ctrl+C,Ctrl+V?

54

31

By default, the cut/copy/paste short-cuts in GVim on Ubuntu are:

 Cut    "+x
 Copy   "+y
 Paste  "+gP

I would like to use control key combos in GVim, like I use in Firefox and othe gnome applications. How do I configure GVim to work like other Gnome apps?

pcapademic

Posted 2009-07-22T05:59:03.067

Reputation: 3 283

To avoid conflicts with default vim keys, opt for Ctrl+Shift+Key instead of Ctrl+Key – Sheharyar – 2016-09-21T08:48:23.400

Answers

51

Add the following lines to your _vimrc or .vimrc

source $VIMRUNTIME/mswin.vim
behave mswin

But beware, visual mode is then CTRL-Q instead of CTRL-V.

For an overview what mswin.vim does see the mswin.vim sourcode. It is commented very well and if some command is unclear you can easily look it up in vim's help.

Here is a quick overview compiled from the source:

  • backspace and cursor keys wrap to previous/next line
  • CTRL-X and SHIFT-Del are Cut
  • CTRL-C and CTRL-Insert are Copy
  • CTRL-V and SHIFT-Insert are Paste
  • Use CTRL-Q to do what CTRL-V used to do
  • Use CTRL-S for saving, also in Insert mode
  • CTRL-Z is Undo; not in cmdline though
  • CTRL-Y is Redo (although not repeat); not in cmdline though
  • Alt-Space is System menu
  • CTRL-A is Select all
  • CTRL-Tab is Next window
  • CTRL-F4 is Close window

At Nippysaurus' request: I put following in my .gvimrc to show Ctrl-V besides Paste in the menu:

unmenu! Edit.Paste
aunmenu Edit.Paste
nnoremenu 20.360 &Edit.&Paste<Tab>Ctrl-V        "+gP
cnoremenu    &Edit.&Paste<Tab>Ctrl-V        <C-R>+

I didn't test it thoroughly, just a quick check if it did what I expected. Works for me, hope it works for you;-)

Ludwig Weinzierl

Posted 2009-07-22T05:59:03.067

Reputation: 7 695

With this, VIM deserves a second chance! – user1068352 – 2015-03-31T20:16:52.177

But this doesn't work for GVim, only for Vim! – Suncatcher – 2016-10-19T16:09:41.270

2It looks like mswin.vim already has a behave mswin call. Is it necessary to call this again in my own _vimrc? – Nathan Friend – 2017-02-06T13:25:24.420

1@NathanFriend Ditto. It works even without behave mswin, at least on my system with Vim 8.0. – Mikel – 2017-07-13T17:39:55.357

2Is there a way to change the menu shortcut text to match the new shortcut keys? – Nippysaurus – 2009-11-30T00:09:21.063

@Nippysaurus: I'd tried it with menutrans in .gvimrc. Didn't get it to work, will have another look at it in the evening... – Ludwig Weinzierl – 2009-11-30T05:54:23.133

@Nippysaurus: Added a possible solution to my answer, see above. – Ludwig Weinzierl – 2009-11-30T20:39:01.990

1I understand and appreciate the points that Manni and Peter Thorin made in the comments of Peter Thorin's answer. My take is that I learned GVim on Windows, as a notepad replacement, and I want less friction. Thanks Ludwig – pcapademic – 2009-07-22T07:07:07.770

46

If you want Cut/Copy/Paste to work using the "standard" hotkeys, but you don't want to change any of the other configuration options in gvim, try do add the following to ~/.vimrc.

vmap <C-c> "+yi
vmap <C-x> "+c
vmap <C-v> c<ESC>"+p
imap <C-v> <C-r><C-o>+

Paste only works in Visual and insert mode, so you don't have to worry about the conflict with Ctrl-V and blockwise Visual Mode. This isn't a problem, because Copy and Cut put you into insert mode, so you can immediately paste afterwards. If you try it out you'll find that it feels completely natural.

I came up with this configuration after several iterations of tweaking, and I think it's "perfect" now. If you're even a little bit dissatisfied with your current copy/paste configuration, try this out and I bet you'll love it.

Cogan

Posted 2009-07-22T05:59:03.067

Reputation: 561

This is exactly what I was looking for. Thanks. – Sean McCleary – 2011-05-05T19:57:52.793

2After some experiments and consultation of :help i_ctrl-r_ctrl-o, I decided to change imap <C-v> <ESC>"+pa to imap <C-v> <C-r><C-o>+; this works as expected when the cursor is to the left of the first character of a line, and it most closely imitates the "standard" behavior. – Holger – 2012-10-11T20:05:14.640

I'm thinking about changing <C-v>to shift+insert that seems to be alot faster on my machine. But in the middle of a project and don't feel like restarting vim right now. Just sharing – Will – 2012-10-16T05:29:09.637

You are awesome =) – Yugal Jindle – 2013-11-14T07:03:03.393

9

If you want to maintain the normal vim behavior but also allow for less cumbersome use of the system clipboard, see Accessing the system clipboard. If you would like gvim to use the system clipboard as its default buffer (so any x, y, p, etc. command uses the clipboard) then add the following line to your vimrc:

set clipboard=unnamed

I personally use the buffers far more within vim than between vim and the system; so I'd rather have a slightly more cumbersome shortcut than have my system clipboard constantly clobbered. But it's nice that the option is there for those who would prefer it.

jtb

Posted 2009-07-22T05:59:03.067

Reputation: 2 115

2

I would think you can add this with the :imap command (tried it just with Ctrl+X in Windows which worked, pressing ctrl+c seems to cancel the command though so you might have to do it in vimrc).

:imap <C-X> "+x
:imap <C-C> "+y 
:imap <C-V> "+gP

If you add it to your ~/.vimrc you just need to remove the : in front of imap.

imap only adds the bindings in insert mode, so you might want to change it to just map or something else. Look into :help mapmode to learn more about remapping and unmapping stuff.

Good luck!

Peter Thorin

Posted 2009-07-22T05:59:03.067

Reputation: 351

1This should do a bit of the trick. But it is a bad idea to do that. Ctrl-V should go into visual mode, Ctrl-C will cancel any commands. And the trick will only work on your system; on a different machine, you will be lost. – innaM – 2009-07-22T06:18:38.730

1I agree in part with that it is a bad idea, I wouldn't want to use it myself, but if you are a casual gvim user only, then you would probably not miss the bindings you remove anyway. – Peter Thorin – 2009-07-22T06:22:10.857

1The why use gvim in the first place? How long will you stay a casual user? – innaM – 2009-07-22T06:30:27.360

2I don't think it's such a bad idea. People are used to Ctrl+X etc. and there are billions other reasons to use vim. – Ludwig Weinzierl – 2009-07-22T07:43:24.000