Vi is probably my favorite linux text editor, partly because I learned on it and partly becuase I find it installed on every linux box I touch. So... on with the tutorial.
Disclaimer: Like I say in all my other tutorials, I cannot be responsible to anything you do to your box while using this tutorial. This tutorial has worked on all my boxes, but may not work on yours.
-------------------------------------------------------------------------------------------------------------
How to Use Basic Vi Text Editor Commands
What is the Vi Text Editor?
Vi is a very versatile Text Editor for Linux. It comes with most Linux distros and is vital to the maintenance of your Linux box. The main issue with Vi is the steep learning curve. Using Vi requires knowledge of a few different commands, and for the person just getting used to Linux it can be a bit challenging. This How-To is designed to help you with that.
To Run Vi
Open a new console window or SSH window. Login.
Part I: Command Mode
1) To edit a file or create a new file type vi filename.txt
Note: Some files may not have an extension, like .txt. In that case, just type the filename. Example:
vi start
Is what I type to edit my Counterstrike: Source server startscript.
2) Navigation
Now you are inside the Vi text editor. The default way to navigate is with the arrow keys, however you can also navigate faster with these keys:
Move down a line: Enter
Note: Do NOT use Enter to move down a line when in Insert Mode (see part 2)
Move up a line: k
Alternate key to move down a line: j
To move forward on a line, word by word: w
To move backwards on a line, word by word: b
To move to the end of the file: shift G
3) Searching
To search for a character or word, type / and the word you want to search for. Example:
/mouse
Hitting n will take you to the next occurance of that word, while hitting shift N will take you to the previous occurance.
4) Editing
If you just need to make a few quick changes, editing from the command mode (default mode that you are in) is reccomended, because it is faster and easier.
To delete a character: Move the cursor over it and press x
To replace a character: Type r then the character you want to replace it with. Example:
Move your cursor over the letter t. Type r then s to change the letter t to a letter s.
To delete a word: wd while your cursor is anywhere on that word.
To delete a line: dd while your cursor is anywhere on that line
To delete multiple lines: # then dd Example:
10dd
would delete 10 lines
To undo anything you have done: u
Part II: Insert Mode
You have just been in Command mode. Command mode allows a user to delete characters, words, and even lines and replace characters, but does not allow a user to add new characters. To do that, we must enter insert mode.
To enter insert mode, type: i
At the bottom of your SSH window or console you will see:
-- INSERT --
You are now in insert mode. Insert mode is very easy to use. The following is a short list of common insert mode commands:
To navigate: arrow keys
To delete a character: delete key or backspace
To exit Insert mode and return to command mode: Esc
Part III: Quitting
There are serveral different ways to quit Vi. All of them require you to be in command mode, so make sure you get out of insert mode first (Esc). Now that you are in command mode...
1) To quit a file that you didn\'t make changes to, type:
:q
2) To quit a file that you made changes to but don\'t want to save, type:
:q!
3) To quit a file that you made changes to and want to save, type:
:wq
-------------------------------------------------------------------------------------------------------------
I will be adding a reply to this thread with more advanced Vi Commands. Check back in the next few weeks for the reply. And of course feel free to leave feedback!