2

In vim I frequently end up undoing more than I want to after a messed up paste from the system clipboard. Is there a better way?

To reproduce:

Open iTerm      # or terminal
vi              # open vim
i               # enter insert mode
type some stuff
Cmd-V           # paste the contents of the OS X clipboard

The paste is a mess because I haven't :set paste. So

Esc # enter command mode
u   # undo

but that undoes my typing as well as my paste.

Is there a different undo I can use? Apart from remembering to :set paste before pasting is there a better way to do this in general?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Thomas David Baker
  • 1,037
  • 10
  • 24
  • 1
    Depending on the terminal, you use bracketed paste mode to auto set paste. http://stackoverflow.com/a/7053522/1890567 – FDinoff Nov 11 '15 at 05:26
  • 1
    What I do when that happens to me is to select the pasted text and format it (hit '=' in escape mode) – Diego Pino Nov 11 '15 at 07:46

5 Answers5

4

This is not the answer you're looking for, but you really should stop using vim as a non-modal editor. If you want to paste then return to normal mode and use vim's paste commands. Assuming that you have a vim version with clipboard access this should do the trick "+p. You can also facilitate this by setting your unnamed register to be the + (or *) register, such that pasting from clipboard becomes a simple p.

The following question has a great answer that includes all this information How to make vim paste from (and copy to) system's clipboard?.

Community
  • 1
  • 1
Vitor
  • 1,936
  • 12
  • 20
1

I'm not sure there's anything you can do after the fact, but one solution is to hit Ctrl-G u in insert mode just before you paste. This breaks the undo block into two separate blocks - so the paste will be remembered as a separate undoable action.

Granted if you have to remember to do this, you might as well just use :set paste instead - but on the upside it's fewer keystrokes and you don't have to go to command mode first.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
1

Take a look at this plugin, it can auto run :set paste! when you use Cmd-V(or Ctrl-V) to paste some text. And leave paste mode when you finished.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
1

The following mappings create an additional undo point (via :help i_CTRL-G_u) before pasting in insert mode. This way, you can undo the paste separately.

inoremap <C-r> <C-g>u<C-r>
inoremap <C-v> <C-g>u<C-v>
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

Actually the solution is you have to go to command mode(e.g. Esc) first and re-enter the insert mode, but it only works if I type manually but it seems "randomly" stop working if I test it in ~/.vimrc. Google doesn't help at all.

I spends a lot of time try to fix this issue and I just figure out the reason in my case:

Don't map the paste key same with the terminal existing paste key

e.g. Ctrl+Shift+V will paste in my Konsole terminal, but if I assign this key <C-S-v> in ~/.vimrc, the "undo for only single paste instead of multiple pastes" will not working.

In my case, I have to use <C-v> instead of <C-S-v>:

inoremap <C-v> <Esc>"+pi<Esc>i<Right><Right>

Your case may difference, but the point is same: don't assign the same paste key conflicts with existing terminal emulator key.

I've 100% proved this conclusion by set my terminal paste key to Ctrl+V and now <C-v> stop working but <C-S-v> working.

Note also that the vim is too sensitive and strange. I figure out I have to use i and then 2 Right keys manually to make it works in the correct cursor position, that's means I have to put i and 2 Right keys in the ~/.vimrc too. Your case might difference, but the point is same, ensure the keys+order in ~/.vimrc 100% match with what you type manually.

林果皞
  • 7,539
  • 3
  • 55
  • 70