Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM Ctrl-V Conflict with Windows Paste

Tags:

vim

windows

People also ask

How to CTRL-v on Vim?

Press v to select characters, or uppercase V to select whole lines, or Ctrl-v to select rectangular blocks (use Ctrl-q if Ctrl-v is mapped to paste). Move the cursor to the end of what you want to cut. Press d to cut (or y to copy).

How do I copy and paste text in Vim?

You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.


From the VIM documentation:

Since CTRLV is used to paste, you can't use it to start a blockwise Visual selection. You can use CTRLQ instead. You can also use CTRLQ in Insert mode and Command-line mode to get the old meaning of CTRLV. But CTRLQ doesn't work for terminals when it's used for control flow.


Check your _vimrc file and see if it sources mswin.vim. That script maps the ^v to the paste. You can either remove that line on your _vimrc file or disable the mapping commands directly on mswin.vim.

Do a :help behave on vim for more info.


Visual mode (and other stuff) working like in Unix requires both JOP's and Windows Programmer's suggestions.

In GVim on Windows, go to the edit menu, click on startup settings, and comment out the windows-specific garbage (using the vimrc comment character, which is a double-quote). The mswin.vim file is where the ctrl-v override is specified, and the behave mswin option makes it so that the arrow keys don't just apply motion like you'd expect (it also changes the mouse selection behavior).

"source $VIMRUNTIME/mswin.vim
"behave mswin

I like to add a black-background colorscheme in there as well, so it looks more like what I'd see in a terminal (and because a light background is great on paper, but awful on a backlit screen): colorscheme koehler


If this line in your _vimrc troubles you:

behave mswin

then delete that line.


I prefer the same keystrokes everywhere so I use this in my .vimrc to override mswin.vim:

if has('win32')
  " Avoid mswin.vim making Ctrl-v act as paste
  noremap <C-V> <C-V>
endif

I'm not sure there is a lot you can do about that. You can use CtrlQ instead though.


Here's a modern day solution to this problem. It applies to the terminal version of Vim/Neovim, not the GUI version. If you use Microsoft's new-ish Windows Terminal (I highly recommend it.), you can redefine its key bindings to your advantage. The following section of the settings file is initially NOT commented out. If you comment it out, as I've shown, Ctrl+V becomes the rectangular visual select key in Vim we all know and love.

// Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
// These two lines additionally bind them to Ctrl+C and Ctrl+V.
// To learn more about selection, visit https://aka.ms/terminal-selection
//{
//    "command": {
//        "action": "copy",
//        "singleLine": false
//    },
//    "keys": "ctrl+c"
//},
//{
//    "command": "paste",
//    "keys": "ctrl+v"
//}

Now here's the weird part. I'd expected this to change the behavior of Ctrl+V outside of Vim in the Terminal, so I checked. It still does a paste, but it's different than the Ctrl+Shift+V paste. Inside Vim, however, all is good: Ctrl+V for rectangular select; and "*P, Ctrl+Shift+V, or Right Mouse Button for pasting from the clipboard.