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:
- If you are in INSERT mode, press Escape - Esc key to get back to the COMMAND mode,
- Now type :u (colon and u) to undo the last changes.
- 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:
- Make sure you not in INSERT mode, press Esc key to goto COMMAND mode,
- Now type Ctrl+R (press and hold Ctrl and then R key) to redo the last changes.
- You can also use the long-form i.e :undo, but as :u is shorter so is more preferred.
- 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',
- :u undoes last change
- :U undoes all changes on line
Undo/Redo Examples:
- Open Terminal on your macOS/Linux (or Bash Shell if you are working on Windows 10)
- Open a file using vi command, example: vi sampleFile.txt
Step 1: Open file using vi
- Press i to enter insert mode
Step 2: Type i for insert mode
- Write some text,
Step 3: Write some text
- Press (Esc) Escape colon and u (:u),
Step 4: Press Ecsape colon uStep 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 calls 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
Summary:- use :u to perfom undo operation.
- :u{Number} to perform mutliple undo operations.
- Use Ctrl+R to perform Redo operation.
Comments:
- Pdf Text to Speech option in Mac OS X Preview App
- vi undo command
- Hide files and folders on Mac OS X
- Java location in Mac OS X
- Mac OS X Error: Could not create the Java Virtual Machine
- How to take Screenshots on Mac OS X without Keyboard
- Save webpage as pdf in Google Chrome for Mac OS X
- Safari appends .html extension to files that are downloaded
- How to See Hidden Folders and Files on Mac OS X
- How to Gzip a file directory on Mac OS X using Terminal Command
- osascript wants to make changes while Android Studio Installation on Mac OS X
- Find Java JRE Installation location Mac OS X
- Mac OS X Taking Screen Capture using Terminal
- How to Stop Photos App from auto loading when device connected to the Mac
- How to Setup maven on Mac OS X
- How to enable Do Not Disturb mode for Notification Center in Mac OS X 10.10 Yosemite
- Mac OS X Stuck During Booting Gray Screen Logo and Spinner
- Kill or force quit stuck application or process in Mac OS X
- Turn on off volume change button sounds Mac OS X
- 4 Open Source SQLite Editor for Mac OS X , Windows and Linux
- Location of eclipse.ini file on Mac OS X
- Disable Chrome Notification bell from Mac OS X menu bar
- Google Search Hot Trends Screensaver for Mac OS X
- Transfer files between Android and Mac OS X using usb cable
- [Solved] Dynamic Web Module 3.0 requires Java 1.6 or newer Mac OSX
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect - Android
- INVALID FILE NAME: MUST CONTAIN ONLY [a-z0-9_.] Android Eclipse Error - Eclipse
- Failed to find provider info for com.facebook.katana.provider.PlatformProvider - Android
- Align html element at the center of page vertically and horizontally - Html
- Android Disable EditText from Auto Focus on Activity load - Android
- How to Convert CSV file to SQL Script using Notepad++ - NotepadPlusPlus
- How to create StackOverflow error in java - Java
- Location of eclipse.ini file on Mac OS X - Mac-OS-X
- List of jar files for Jax-ws (SOAP) based Java Web Services - Java
- JSON Schema Validator Libraries: JSON Tutorial - Json-Tutorial
- List of Java versions - Java
- Static IP MTNL available at Rs. 200 per month - HowTos
- SharePoint Server 2016 IT Preview - new improved Features and Enhancements - SharePoint
- How to export bookmarks from Google Chrome Browser - Chrome
- Fetch as Google Crawl Error or Redirected Status - Google
reply: @c2cDev - You can try using Shift + U to undo all the changes you have done. There is another way, simply press Escape and type :!q to quit without saving anything!
reply: @c2cDev you can try :undolist command And then try :earlier and :later command
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.