Vi-like editors are a class of editors, some instance of which is on every *nix ever made. So learning to use a vi-like editor is a must! In Slackware, elvis is the default editor, but Slackware also provides a console-only and gui-enabled vim as well. The core command set is the same, though details may vary. (For instance, 'g' and 'gg' are vim commands but not nvi commands, but both recognize 'G' commands.) To find out what you'll be executing, try
$ ls -l `which vi`
Then open the editor to an example file (as a user)
Now before we start you must realize that there are two modes. First is a command mode where you type commands, move the cursor. The other mode is the Insert Mode and this mode is where you type text. The basic movement keys are: (sorry for the bad graphic below)
^ k <h l> j v
Move around with the keys above and you will find out that you will get used to it faster then you thought you would. Movement is the key thing to learn in VI and there are a few things you might need to know:
$ Moves to end of the line 0 Moves to the beginning of the line ^ Moves the the end of the white space in the line.
The above are pretty easy to understand, but I want to explain the ^ key more. Say you have this line:
Hello Sandman1 is nice
When you are on the line above and you press the ^, You will land on the H. In VI you can also move forward on words as well:
w Goes to the next word including Punctuation W Goes to the next word NOT including Punctuation b Goes back a word including punctuation B Goes back a word NOT including Punctuation
The above commands make your text editing go faster. To explain the no punctuation look at the follow line:
Hello, My name is Sandman1.
Now imagine your cursor is on the H. If you press the w key you will land on the comma, but if you press the W key it will land on the M in My. Now say you are in the insert mode (you will learn how to later in this tutorial) and you make a mistake? the c command is useful:
cw Change the next word cb Change the word behind the cursor r Change Character C Change from the cursor to the end of the line c0 Change from the cursor to the beginning of the line c^ Change from the cursor to the white space at the begging of the line.
Now these are pretty self explanatory, but I want you to notice how we used the other commands we learned already such as the B, $ , etc. You can also use the capital B and the capital W and get the same effect.Now say you want to erase things as well? We use the d key for that:
dd Delete current line dw Delete word d$ Delete from the cursor to the end of the line d^ Delete from the cursor to the white space at the beginning of the line d0 Delete from the cursor to the beginning of the line
These are again pretty basic to understand.
i Insert text before the cursor I Insert text at the beginning of the line A Insert text at the end of the line a Insert text after the cursor o Insert text on a NEW line below the current line O Insert text on a NEW line above the current line
Now these are a lot of memorize, but I only usually use o, A and i. Now say you are typing and you made a few mistakes the best way to get rid of the mistakes are getting out of insert mode:
ESC <<<<<<<<<<< IMPORTANT, learn how to get out of insert mode
Yes you actually press the escape key:). Now you can type the commands you wish such as delete, or even change a word. Once you start using VI you will learn these commands and you wont even have to think to do the tasks. Now say you have an error in a config file and you want to go to the line type this outside of vi:
vi +100 /etc/apache/httpd.conf
This will open to line 100. To verify that you're on that line, type C-g. You can also head to a different line by typing:
:100 100G G Goes to the end of File (Special) 1G Goes to the beginning of the file (special)
The reason why I marked the 1G and the G special is because they are REALLY useful and you shouldn't forget them. Since we have used a ctrl key in here why not just show you how to move up and down a page:
C-f Forwards one page C-b Goes back on page
Ok we have a general idea on how we make VI work, but we still don't know how to close the damn thing. The following are the basic commands to open and close VI:
:w Saves the current buffer to the file you opened with :e! Reloads the current file not saving any changes :e Loads a file :q Quits :wq Quits and saves
Now the :w and the :e you can also put with a file, for instance:
This will open the file /etc/lilo.conf. If you opened a file, but also had another file opened? Just press C-6 to switch. Now say you have opened vi and don't want to save, but still quit? Just put a ! in the command:
The ! works with all the commands even :w. Another quick way to save and exit is just type ZZ in the command mode. That will exit and save.
The next thing is out of place, but I think you should know it. Say you want to move down 5 lines?
That will move down five lines and you can do this on a lot of commands.For instance:
The follow will erase 5 lines in the down direction! There are more commands than just listed in this tutorial and perhaps I will write an advanced tutorial for VI, But for now you know the basic VI. Good luck:) This is by Sandman1 @ freenode