Join David D. Levine for an in-depth discussion in this video Entering and leaving vi, part of Up and Running with vi.
As you might expect for a Unix command, vi starts from the command line. The command vi, or vi, is followed by the name, or names of the file or files, that you're going to be editing. If you specify the name of an existing file, it reads it into memory. If you specify a file that doesn't exist, it starts off with an empty buffer, and then later on, when you save, it'll save to that file name. But note that the file is not created until the first time you save. After you've finished editing the file, you enter Shift+ZZ, that is to say, two capital Z's, one right after the other, which writes the file and quits from vi.
Alternatively, you can type :wq Return. Either of these does the same thing. It writes the file and then quits vi. If you have modification to the file and you don't want to save them, you type :q! That's a force quit. It quits without saving changes. So let's take a look at how this looks in action. In this directory, we have a few example files. So, I'll start by editing the dukeofyork.txt file. So here we see that this is what vi looks like when you have just begun editing a file.
You see the contents of the file, at the top of the screen. And then below the file, you see a number of lines with just a tilde on them. The tilde represents a line below the end of the file. And if you begin editing a file that doesn't exist yet, you'll see nothing but tildes on the screen. At the bottom of the screen, you see the file name that it has just read into memory, and the number of lines and characters in the file. I'll do :wq, Return. Or, if I made a change, and I'll show you how to do that in the next lesson, then I can :wq to write out the change and quit, or I can do just q to quit.
Now if I do :q to quit, it'll tell me that there hasn't been a write since the last change. Meaning that I've made a change to the file, but I haven't saved it to disk. So I can do :q! to quit without saving.
- Entering and leaving vi
- Understanding the Command, Insert, and Colon modes
- Moving around in files
- Editing text
- Moving content with delete, yank, and put
- Searching with regular expressions
- Customizing vi
- Filtering text through shell commands