0

i have just tried something of which i don't know how it worked out but thanks it did. i have been trying to undo a few operations of dl which deleted a few letters. it didn't work as it kept on redoing so i tried something else. i then kept on pressing random numbers with a "u" and those letters came back. e.g 9u

how does this work?

2
  • 1
    Welcome to Vi and Vim! Are you asking how the underlying undo mechanism works, how to undo (press u), or how to undo multiple operations? Please edit your question to clarify, and see the formatting guide
    – D. Ben Knoble
    Jun 23, 2020 at 12:07
  • the original vi editor only had one single undo operation. Then there were a few vi clones, that allowed . to repeat going further undo IIRC. Jun 23, 2020 at 13:19

1 Answer 1

4

Original vi only supports single-level undo: the first time you press u it performs an undo, and the second time you press it it undoes the undo, going back to the state before the undo.

Nvi (and possibly other implementations of vi) allow you to perform more undoes first by pressing u, and then by pressing . to repeat the undo operation.

Vim augments undo with two different undo behaviours.

In “vi-compatible” undo, the u key behaves as above. If, however, you want to perform multiple undo operations, you first press u to undo, and then press Ctrl-R for further undoes.

In the Vim-style undo, pressing u will always go backwards through the undolist, and pressing Ctrl-R will always go forwards.

To switch from vi-compatible to Vim-style, you add the u flag to your 'cpoptions' setting, but the most common way of doing this is by abandoning vi compatibility entirely, which is generally achieved simply by creating a .vimrc file*.

If you want to stop using vi compatibility in an existing session, you can use the command:

:set nocompatible

* Or by upgrading Vim past version 7.4.2111, which switches off 'compatible' in its defaults.vim script.

Your Answer

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

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