Undoing and Redoing Changes with u and ctrl-r
Let's say we were in VI and made a couple of changes, but then realized we didn't want to make the last one. You can undo changes by pressing the "u" key while in command mode.
I'll open Test.txt back up in VI by typing "vi Test.txt"
And now, I'm going to add a line in between the two current ones by pressing the "i" key to enter insert mode, and moving the cursor with the down arrow. I'll type "This is the real second line." and press Enter to move the rest to the next row.
Then, I'll change the word "second", in the now third line, to say "third".
Okay, at this point, let's pretend you've made any number of changes to the file, and realize you didn't like the last change, but you don't want to quit without saving because you do want to keep everything else you've changed. We can undo changes by pressing the "u" key while in command mode.
I'll press the Escape key to exit "Insert" mode. Then I'll press the "u" key, and you'll see the word "third" change back to "second".
Now, I could save the file, if I'm happy with it, or, I could continue to press the "u" key and keep working backward through my changes.
I'll press "u" again, and the new second line goes away. If I press the "u" key one more time, a message is displayed at the bottom of the window that says "Already at oldest change". So, we're as far back as we can go. So, just like Word or Pages or other GUI editors, you can only go back as far as the way the file was when you opened it.
Now, let's say we went too far back when we were pressing "u". You can redo changes undone with the "u" key by pressing "ctrl-r" (that is, by holding down the Control key, and pressing the "r" key.)
I'll press ctrl-r, and you'll see the second line come back, and when I press it again, you'll see the word change back in the third line. If I press it one more time, you'll see the message at the bottom of the window that says "Already at newest change". So, I have reached the most recent version of the file.
So, like I mentioned in the last video, if you do accidentally open a file in VI that you just wanted to read, and make a change (like by pressing the "d" key twice), you can undo that change, and any others by pressing the "u" key to get the file back to the way it was when you opened it.
I'll type ":wq" and press Enter to save and quit this file.