4

I'm a pro user of vim, however, somehow I don't seem to have definitively figured out the use of time based undo, or maybe I don't really like it. Is there a way I can undo a deletion caused by Ctrl-w W3I will be really grateful for the help.

Thanks.

1
  • If you're referring to undoing ^W deletes made as part of an insert operation, I'm with you in wanting to know. I guess you could remap it to something that saves the value in a script- or buffer-local variable, but I don't think there's a way to do this built into vim. But I'm confused by your mention of time-based undo, ie g- and g+, which AFAIK is not related to the use of i_^W.
    – intuited
    Commented May 29, 2010 at 22:56

2 Answers 2

5
:inoremap <C-w> <C-g>u<C-w>

<C-g>u sets undo point to the current point. And then <C-w> does the deletion work.

4

The tip title refers to Ctrl+U which is similar but not quite the same as Ctrl+W. However the same method can be used for both.

Recover from accidental Ctrl+U.

You can't undo these deletions. However, what you've typed is still in the . register. You can confirm that (after pressing Esc to return to normal mode) with the command :reg which will list all registers (or just :reg . to display the . register). You may be able to copy the missing text from the register display, for example, with the mouse.

Unfortunately, simply pasting the . register won't help because it will repeat the Ctrl-u or Ctrl-w and will delete the text again. However, you can use another register (register a in the following):

:let @a = @.
"aP

The above will paste all the text you last inserted, including what was accidentally deleted.

1
  • Would it be possible to use "setline()" and "substitute()" in a function to automate this action? Commented Jul 11, 2019 at 0:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .