
As a web professional, I find myself using a txt editor very often. Whether that be an HTML file, a CSS file or a server config file, there are many many times throughout the day that I find myself in a txt editor.
While on my desktop my txt editor of choice is BBedit. Unfortunatly…BBedit is not always available, especially when using the command line on a *Nix server.
While using the command line I prefer to use vi. Many people are intimidated by using vi, but I think that once you learn a few basic concepts using vi really is the best choice. With that in mind lets get started.
The first thing that helped me with vi was understanding the fact that there are two main modes. The command mode, and Edit mode. To change modes, or toggle between them, all you have to do is hit the ESC key. It is worth noting that hitting the ESC key at anytime within Vi does nothing other than change modes. It does not destroy any text at all.
I think of command mode as the mode in which you send commands to the vi editor. You use command mode to do things like save, quit, to delete lines, and Undo.
The other mode is Edit mode. This mode allows you to edit the content of the file itself.
There are many more commands at your disposal, but I have never had to use any beyond the ones I list here.
- To open a file just type –> vi filename. This will open up your file and put you in command mode.
- To create a new file –> vi newFileName . This will open up a blank file in command mode. Hit ESC and just start typing. When you save it will save it with the name you opened it with.
- Something to keep in mind… When your curser is on a character then that character is selected. You can insert things before and after that character, but you can also delete that character if you are not careful.
- While in Edit mode…To INSERT something to the left of the cursor just type i
- While in Edit mode…To APPEND something after the cursor type a.
- You can move around using the arrow keys or the Page UP/Down keys.
- While in Edit mode…you can delete things to the right of you cursor using the delete key, and things to the left of your cursor with the backspace key.
- While in Edit mode…You can INSERT A LINE by getting the cursor beyond the last character of the previous line then hit ENTER/RETURN. This creates a new line under the one you were on.
- If you want to DELETE A LINE. Go to the beginning of that line, and switch to command mode by hitting the ESC key. Once in command mode hit the D key two times. Make sure not to hit it three time, or hit it slow. Hit that D key twice with vigor.
- While in command mode…To UNDO just hit the U key.
- While in command mode…To SAVE and QUIT type in “:wq”. (That is a colon the letter w then the letter q)
- While in command mode…To JUST QUIT without saving any of the changes you made type in “:q!” ( That is a colon, then the letter q, and an exclamation point.)
- While in command mode…To QUIT if you have made no changes to the file at all just type “:q” (That is a colon then the letter q)
For a more comprehensive list of commands I always seem to find myself refering to Rick Ells article on the subject.
Using vi, the Unix Visual Editor by Rick Ells
Hopefully this article helps you out with learning vi. It really is a nice editor that seems to be installed on every *Nix server out there.