1

I'm new to Vim and install that in windows 10.

I write this instructions in Vim then in normal mode pressed u and all the instructions disappeared.

console.log(899) hello you can write some simple Javascript instructions here....

But when I use VsCode and press Ctrl-z all the instructions will not disappear. VsCode deletes word by word where Vim deletes all the things.

How can I set this behavior in Vim?

1
  • u is to undo the last action in a whole. Here, the action was to put a full line (not a word.) The undo will work as you expect if you insert one word a time… However, you can delete a word the cursor is on with diw
    – gildux
    Jan 3, 2023 at 12:19

2 Answers 2

1

The undo action is per-line if you write a bunch of lines in insert-mode. This is natural as most languages use 1 statement per line (usally).

To override this behavior while in insert mode use Ctrl-g u to manually add an undo point.

6
  • Actually, if you type i<multiple lines of text><esc>, then u removes all text, since the action was a single insert.
    – D. Ben Knoble
    Jan 3, 2023 at 14:30
  • I don't get that behavior if there are line-breaks in the insert action. I can see each line in my undoTree. Jan 4, 2023 at 14:21
  • 2
    You might have a mapping on <enter> in insert-mode; try vim --clean
    – D. Ben Knoble
    Jan 4, 2023 at 21:02
  • @D.BenKnoble You are right. vim --clean gives the behavior as you said. Must be something in my config/plugin that changed that behavior. Jan 26, 2023 at 12:26
  • Thanks, It works for me. Feb 20, 2023 at 5:42
1

One way to get a behavior to want you want is to have the following mapping:

inoremap <space> <C-g>u<space>

It create a new undo operation each time you insert a space.

3
  • can I have both of this behaviors in vim? default behavior and your solution together? Oct 6, 2023 at 5:33
  • I'm not sure to understand what you mean but I suppose the answer is no :-) If you mean to have to kinds of undo one with the standard behavior and one with the 'word' undo behavior I suppose it is not possible since the there is only one undo stack and I don't see ways to make the distinction between the kind of undo state. Oct 6, 2023 at 6:37
  • 1
    You understood what I mean. Oct 6, 2023 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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