vi undo redo command


vi is by far the most popular console based Text Editor for UNIX/Linux Operating Systems. vi is pre-installed on nearly all UNIX flavors (includes macOS and centOS), you have to have a good hold of keyboard shortcuts and commands if you want to work with it, yes may seem old-school if you are new to the programming environment, but it is very popular among software engineers.

You can use the undo command to revert the text to how it was before each change. You can also apply the changes again (redo) or get back to the text before the changes you made. vi keeps a track of all the last changes you make.

How to undo changes in vi:

  1. If you are in INSERT mode, press Escape - Esc key to get back to the COMMAND mode,
  2. Now type :u (colon and u) to undo the last changes.
  3. You can also use the long-form i.e: undo, but as :u is shorter so is more preferred.

How to redo changes in vi:

  1. Make sure you not in INSERT mode, press Esc key to goto COMMAND mode,
  2. Now type Ctrl+R (press and hold Ctrl and then R key) to redo the last changes.
  3. You can also use the long-form i.e :undo, but as :u is shorter so is more preferred.
  4. You can also add a number to redo command example: To redo the last two changes you can use 2Ctrl+R

If you want to undo multiple steps you can try syntax :u{n} where n -> number of steps you want to undo. Example: :2u or uu in vim will undo 2 previous changes,:3u will undo 3 changes. Note that there is a difference between capital letter U (Shift + U) and lower case letter 'u',

  1. :u undoes last change
  2. :U undoes all changes on line

Undo/Redo Examples:

  1. Open Terminal on your macOS/Linux (or Bash Shell if you are working on Windows 10)
  2. Open a file using vi command, example: vi sampleFile.txt
    Step1 Open file using vi
    Step 1: Open file using vi
  3. Press i to enter insert mode
    Step2 Type i for insert mode
    Step 2: Type i for insert mode
  4. Write some text,
    Step3 Write some text
    Step 3: Write some text
  5. Press (Esc) Escape colon and u (:u),
    Step4 Press Ecsape colon u.PNG
    Step 4: Press Ecsape colon u
    Step5 Undo operation result
    Step 5: Undo operation result

Revert changes using :earlier or :later commands:

Just like undo and redo, you can make use of :earlier command to undo changes, and :later to redo changes. If you add a number after this command example- :later 10 it will redo last 10 changes.

⚠️ Vi maintains a swap file while you are working on a text file, it is the undo/redo history buffer, this swap file gets deleted when you quit vi. If you exit vi then there is very less chance that you may finds undo/redo details, the possibility of recovering the changes is very less.

Undo up to a file write

If you had made a lot of changes to a file and you want to go back to the state in the past where you had last written the file, you can make use of the :earlier 1f command where "f" stands for "file". Similarly you can user :later 1f to go forward.

Undo blocks

One undo command usually undoes a typed command, no matter how many changes that command makes. This sequence of undo-able changes forms an undo block. Therefore if the typed key 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[oin]

⛏️ Tips:. (dot) can be used to repeat the same previous command, so if you use :u followed by . it will perform undo again.

✌️ Did you know? The name vi is derived from the shortest unambiguous abbreviation for the ex command visual, which switches the ex line editor to visual mode. - Wikipedia

Vi Undo/Redo Command Terminal
Summary:
  • use :u to perfom undo operation.
  • :u{Number} to perform mutliple undo operations.
  • Use Ctrl+R to perform Redo operation.



Comments:

  • > Fantastic! This is what I was looking for! I am a student and have started learning vi - Thank you!
    anonymous 06 Sept 2020 04:22:13 GMT
  • > Does it even work when I close the file and open it again sometime later? Can I undo those changes as well?
    bagrd 22 Jul 2020 11:02:11 GMT
  • > Extract from the vi documentation:
    1. Undo and redo commands				*undo-commands*
    
    
    <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}
    
    								*E830*
    :u[ndo] {N}		Jump to after change number {N}.  See |undo-branches|
    			for the meaning of {N}.  {not in Vi}
    
    
    							*CTRL-R*
    CTRL-R			Redo [count] changes which were undone.  {Vi: redraw
    			screen}
    
    
    							*:red* *:redo* *redo*
    :red[o]			Redo one change which was undone.  {Vi: no redo}
    
    
    							*U*
    U			Undo all latest changes on one line.  {Vi: while not
    			moved off of it}
    
    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.
    anon | 31 Jul 2020 05:01:57 GMT
  • > I usually use u to do undo in vi editor. Is there a way I can undo all changes to get back to the initial state of the file?

    reply: @c2cDev - You can try using Shift + U to undo all the changes you have done. There is a another way, simply press Escape and type :!q to quit without saving anything!
    alecK | 26 Jul 2020 04:07:13 GMT
  • > Is there a way we can see all previous undo history?

    reply: @c2cDev you can try :undolist command
    :undolist
    
    number changes  when               saved
         1       1  34 seconds ago
         2       1  28 seconds ago
         3       1  22 seconds ago
         4       1  14 seconds ago
    Press ENTER or type command to continue
    And then try :earlier and :later command
    :earlier {count}	Go to older text state {count} times.
    :earlier {N}s		Go to older text state about {N} seconds before.
    :earlier {N}m		"   "    {N} minutes before.
    :earlier {N}h		"   "    {N} hours before.
    :earlier {N}d		"   "    {N} days before.
    :earlier {N}f		"   "    {N} file writes before.
    
    :later {count}		Go to newer text state {count} times.
    :later {N}s		"   "    {N} seconds later.
    :later {N}m		"   "    {N} minutes later.
    :later {N}h		"   "    {N} hours later.
    :later {N}d		"   "    {N} days later.
    :later {N}f		"   "    {N} file writes later.
    user121 | 24 Jul 2020 15:07:13 GMT
  • > Does this work on Windows Bash as well? I am using Git Bash and Ctrl + R not working for me! Esc + colon + U worked though - Thanks

      reply: @c2cDev - Yes it should work! I did try both on Windows Bash and Git Bash, maybe you did not press escape to come out of command mode.
    user101 | 18 Jul 2020 15:07:58 GMT
  • > I am new to Linux, trying to get familiar with the console, VIM is an awesome editor, i know not everyone would agree to it, but as you get its grip! It makes your life easy dealing with files on server machines.
    user101 | 17 Jul 2020 05:14:11 GMT
  • Further comments disabled!