Welcome back, in this post we are going to discuss what I call, the most famous editor ever created, vi, the name vi is an abbreviation of the word visual. which was an evolution of the original ex editor, a line editor. When vi was introduced, it was like a revolution, it introduced the ability to move the cursor on the whole file and edit single characters.
In UNIX and derived systems, vi is a foundational editor, it’s the editor that you are going to find even on a minimalist system. I retain important, that to effectively operate on UNIX, you must learn vi, or at least enough to be able to edit a file. This post addresses the minimal function you must learn to effectively edit a file.
First of all, and take this from a person that has been using vi for over 30 years, there are two things that you must remember in order to avoid unnecessary trouble:
- Remember this. NEVER save a file with the combination key ‘Shift ZZ‘. This combination sames a file without confirmation and it will put in you trouble. NEVER USE THIS FUNCTION, always use ‘:wq‘ instead.
- Should you find yourself in a situation that you need to exit without saving the file, remember the key combination ‘:q!‘. This combination forces the exit.
Nowadays, when we say vi, we really mean vim, or vi improved. If you use Linux, vim is what you are using, not the original vi. The command vi in Linux is simply a symbolic link to vim.
This post assumes that you know how to navigate directories with a UNIX terminal. Now, how do you enter vi? Simply by issuing a command like the following:
vi {file path}
With this command, the visual editor will open with the specified file ready to be edited, if the file does not exist, there will be an empty screen which will help you create the file. Now, when you operate in vi, keep in mind that it was created when the mouse did not exist, so all operations are with the keyboard.
At this point vi is waiting for you input, the arrow keys allow you to move just like any other editor, the difference is when you get to a point that you need to make changes to the file, there are commands you have to issue before you can type the actual text. In order to understand you will have to experiment with it. You have to memorize the following command:
- Press ‘i‘ is used to insert
- Press ‘r‘ to replace a single character
- Press ‘R‘ (shift+r) to replace many characters
- Press ‘Esc‘ to exit the mode you are in
- Press ‘o‘ to insert a line after or ‘O‘ to insert a line before the cursor
- When you are ready to save the file, press ‘Esc‘ then ‘:wq‘ to write and quit
- If you need to exit without writing anything, then you press ‘Esc‘ followed by ‘:q!‘
- Never, ever use Shift+zz
With the commands specified above, you can start editing any files, now, vi has way more commands, too long to list them all, but we are going to list a few more commands that could be useful.
- Press ‘C‘ (shift+c) to replace from the cursor to the end of the line
- Press ‘Esc‘ followed by ‘/‘ to search within the file, this can be a string or advanced regular expressions
- Press ‘Esc‘ followed by ‘:{line number}‘ to move to a specific line, like :1 to go to the top of the file
- Press ‘G‘ (shift g) to move the cursor to the end of the file
I think at this point, you have enough ammunition to start editing files in vi. I am going to write another article for more advanced functions. In the meantime, have fun with vi.
Thanks for stopping by…