2

I know about u and Ctrl-r for undo and redo. But u takes me back to the last saved state. I want to know how to undo a paste cmd-v.

For example,

I am typing here bla bla bla, [link to website](here I paste a wrong http link 
with cmd-v)

Then I want to go back to,

I am typing here bla bla bla, [link to website](

Is there any way I can do it in vim?

Thanks in advance.

1

Disclaimer: I'm wholly ignorant of how Apple keyboards with cmd rather than ctrl interact with vim, so you may need to change or fine-tune some of this.

The correct way to paste in vim is to use the p command. To paste from the system clipboard, you would use "*p. So, the standard way of doing things would be to leave insert mode and "*pa (the a, of course, re-enters insert mode).

If you want to have a keyboard shortcut to use within insert mode, it would seem natural to do something like:

inoremap <c-v> <esc>"*pa

(That is: actually type what is there rather than pressing ctrl+v and escape).

At first glance it would seem like this should provide the behaviour you want -- it's just automating the standard pasting process I described above. But it does not. Vim treats all parts of a command as part of the same undo block, and to vim insert mode itself is a sort of command.

However, you can manually start a new undo block while in insert mode by using ctrl+g followed by u (yes, I know, this key chording seems fairly un-vim-like, but there you go). So, you can amend the key remapping like so:

inoremap <c-v> <c-g>u<esc>"*pa

All of this will work with ctrl+v (I've just tested it on Windows and it works there, at least), you may need to use something different (other than <c-v>) for cmd-based shortcuts within vim. If you're using an Apple keyboard, that implies OSX -- if cmd+v is a system shortcut, I'm not sure what would take priority. Some superficial googling suggests you may need to fiddle with some OS-level configuration to get cmd working properly with vim.

0

In MacVim, you need to unmap the Edit > Paste menu item in order to override the shortcut, then you can remap Cmd-V (D-v in MacVim parlance) to the appropriate command in each mode. I added this to my ~/.gvimrc and now everything works wonderfully:

macmenu Edit.Paste key=<nop>
noremap <D-v> "*P
cnoremap <D-v> <C-r><C-o>*
inoremap <D-v> <C-g>u<C-r><C-o>*

The <C-g>u is necessary only in insert mode to fix the undo history; the noremap and cnoremap are simply to restore the default paste behavior in the normal and command modes.

Thanks to evilsoup for pointing me in the right direction here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.