How do I undo an action in Vim?


Answer: 1

24 hours ago

I know that there is a command or something you can type in Vim that does the same as CTRL + Z does in most GUI programs in order to undo what was just typed, but I can't remember what it is. Does anyone know?

Answer: 2

12 hours ago

From the vim help:

<Undo>          or                                      *undo* *<Undo>* *u*
u                       Undo [count] changes.  {Vi: only one level}

                                                        *:u* *:un* *:undo*
:u[ndo]                 Undo one change.  {Vi: only one level}
:u[ndo] {N}             Jump to after change number {N}.  See |undo-branches|
                        for the meaning of {N}.  {not in Vi}

CTRL-R                  Redo [count] changes which were undone.  {Vi: redraw

                                                        *:red* *:redo* *redo*
:red[o]                 Redo one change which was undone.  {Vi: no redo}

U                       Undo all latest changes on one line, the line where
                        the latest change was made. |U| itself also counts as
                        a change, and thus |U| undoes a previous |U|.
                        {Vi: while not moved off of the last modified line}

The last changes are remembered.  You can use the undo and redo commands above 
to revert the text to how it was before each change.  You can also apply the 
changes again, getting back the text before the undo.

The "U" command is treated by undo/redo just like any other command.  Thus a
"u" command undoes a "U" command and a 'CTRL-R' command redoes it again.  When
mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
restore the situation of a line to before the previous "U" command.  This may
be confusing.  Try it out to get used to it.
The "U" command will always mark the buffer as changed.  When "U" changes the
buffer back to how it was without changes, it is still considered changed.
Use "u" to undo changes until the buffer becomes unchanged.

2. Two ways of undo                                     *undo-two-ways*

How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
There is the Vim way ('u' excluded) and the Vi-compatible way ('u' included).
In the Vim way, "uu" undoes two changes.  In the Vi-compatible way, "uu" does
nothing (undoes an undo).

'u' excluded, the Vim way:
You can go back in time with the undo command.  You can then go forward again
with the redo command.  If you make a new change after the undo command,
the redo will not be possible anymore.

'u' included, the Vi-compatible way:
The undo command undoes the previous change, and also the previous undo command.
The redo command repeats the previous undo command.  It does NOT repeat a
change command, use "." for that.

Examples        Vim way                 Vi-compatible way       ~
"uu"            two times undo          no-op
"u CTRL-R"      no-op                   two times undo

Rationale:  Nvi uses the "." command instead of CTRL-R.  Unfortunately, this
            is not Vi compatible.  For example "dwdwu." in Vi deletes two
            words, in Nvi it does nothing.

3. Undo blocks                                          *undo-blocks*

One undo command normally undoes a typed command, no matter how many changes
that command makes.  This sequence of undo-able changes forms an undo block.
Thus if the typed key(s) call a function, all the commands in the function are
undone together.

If you want to write a function or script that doesn't create a new undoable
change but joins in with the previous change use this command:

                                                *:undoj* *:undojoin* *E790*
:undoj[oin]             Join further changes with the previous undo block.
                        Warning: Use with care, it may prevent the user from
                        properly undoing changes.  Don't use this after undo
                        or redo.
                        {not in Vi}

This is most useful when you need to prompt the user halfway through a change.
For example in a function that calls |getchar()|.  Do make sure that there was
a related change before this that you must join with.

This doesn't work by itself, because the next key press will start a new
change again.  But you can do something like this: >

        :undojoin | delete
After this an "u" command will undo the delete command and the previous

To do the opposite, break a change into two undo blocks, in Insert mode use
CTRL-G u.  This is useful if you want an insert command to be undoable in
parts.  E.g., for each sentence.  |i_CTRL-G_u|
Setting the value of 'undolevels' also breaks undo.  Even when the new value
is equal to the old value.

Rest you can find when you do :help undo in vim.

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9