vi is a text editor. It may not be the easiest editor to use, but it is a standard that is widely used and very powerful. Sooner or later, if you are required to do any Linux administrative work, you will need to learn vi. This document is not intended to show you every feature of vi. It is intended to cover the basics and get you going so you can start editing text files.
- In this document all upper/lower case commands should be carefully noted.Commands in “vi” are case sensitive.
- ^ represents the entry of a control value. It is executed by holding down the “control key” while pressing the appropriate letter.
- All commands that should be entered are printed in bold.
From a shell prompt enter
- vi <filename> where <filename> would be replaced by the name of the file you want to edit.
Example: vi myfile
If the file named already exists, the current contents of the file will be displayed. If the file named doesn’t exist, a new file will be created I suggest that you start with a test file that already contains data.
To better understand vi, think of vi as having different modes of operation. I like to label the modes as follows:
|What it does
|Allows you to use arrow keys or other control commands to move around within existing text
|Allows you to actually insert new text
|Allows you to change existing text, delete lines, change a word, change a character, undo changes
|Allows you to execute global edit commands, read/write to a file, exit vi and many more options
|Allows you to search for text and change the text
|Special and misc. commands
The simplest method to move the cursor is to use the arrow keys, up, down, left or right. Other handy movement commands are…
|What it does
|Goes forward a screen at a time
|Goes backwards a screen at a time
|Jumps to the end of the file
|Jumps to a specific line number. Example: 3G jumps to line 3
|Moves forward a character at a time
|Moves backwards a character at a time
|Drops down a line at a time
|Jumps to end of current line
There are many more, but for now master the ones above.
The first rule to remember is how to exit insert mode. Every time you enter insert mode, you have to remember to press ESC before executing other commands!
|What it does
|Exit insert mode
|Insert characters BEFORE cursor position
|Insert characters at BEGINNING of current line (Capital i)
|Append characters AFTER cursor position
|Append characters at END of current line
|Open a new line BELOW current line
|Open a new line ABOVE current line
Now that you know how to insert, let’s find out how to change existing text.
|What it does
|Replace characters starting at cursor position until ESC
|Join the line below to the end of the current line
|Replace current character with the letter “x”
|Change the word at the current position up to the first space. Press ESC after new entry
|Delete character at current position
|Delete several characters starting at current position. Example: 3x deletes 3 characters
|Delete the current word
|Delete the current line
|Delete a number of lines, starting with current line. Example: 3dd will delete 3 lines
|Delete from cursor to end of the line
|Undo last command
|Restore the current line
Now it starts to get interesting. Let’s do the basic colon commands first.
|What it does
|Exit from COLON MODE. It doesn’t hurt if you press ESC more than once
|Start COLON MODE. You will jump to the last line on the screen where a colon will display and await your next command
|Write the file and exit
|Abort all changes and exit
|Write the file saving current changes, but does not exit vi
|Write the current file to another file name
|Write a range of lines of from current file to another file.
Example: 10,20w myfile will write lines 10 to 20 to a file called “myfile”
|Reads a file and inserts it the line below current position
|Executes a shell command. Example: !ls will list files in the current directory
|Performs global substitution starting at line 1 to end of file. The “$” sign represents end of file
|Same as above, except this will perform the substitution as many times as required per line. Without the letter “g” at the end, the command will only perform the substitution once per line. Example: 10,20s/Chi /Chicago/g will change all occurrences of “Chi ” to “Chicago ” from line 10 through line 20. Note the inclusion of the space character after Chi and Chicago.Without this it would have changed “Chirp” to “Chicagorp”
|Two characters that are frequently used in many Linux commands are the up caret (^) and the dollar sign ($). The up caret means “at the beginning”. The dollar sign means “at the end”.
|10,20s/^Chi /Oswego /
|Changes the occurrence of “Chi ” to “Oswego” only if the line STARTS with “Chi “
|Changes the occurrence of “01” to “99” only if the line ENDS with “01”
|What if you want to change a date 01/31/94 to /02/28/94 on the current line?
You are editing a large file of over 3000 lines and you want to find the first occurrence of “Password”. How would you do it?
/Password (That’s a forward slash at the beginning) Like the colon command, the slash will take you to the last line of the screen. The cursor will jump to the FIRST occurrence of “Password” from your current cursor position.
Well that’s great, but now you want to find the NEXT occurrence. Don’t fret.
Enter n to find the next occurrence of the last find command.
But now you want to return to the PREVIOUS occurrence.
Enter N too find the previous occurrence.
Instead of the “/” for FORWARD search, you can use the ? for BACKWARD search.
?Password – Searches backwards from the current position.
top of page
Assume you have a large document and you want to check for “there” vs “their”. You can’t use the substitute command because this would change all occurrences. You may only want to change selective there’s.
- /there – This will find the first “there”
- cwtheir and press ESC
– cw changes the word “there” to “their”
- n – Find the next “there”
- . (Yep, just the decimal point.)
This REPEATS the previous EDIT/CHANGE command
You have 3 lines of text near the beginning and you want to move this down several pages to another area. Position the cursor on the first line of text to move.
- ma – This marks this location as spot “a” Move your cursor to where you want to relocate
- mb – This marks this location as spot “b“
- ‘a – This jumps back to marked spot “a“
- Y3 – This yanks 3 lines into buffer memory
- ‘b – This jumps to marked spot “b“
- P – This puts the yanked text at new location
- ‘a – This jumps back to mark “a“
- 3dd – This deletes the 3 lines The mark options can also be used just to remember locations. They remain until you perform another “mX”. Valid letters are “a-z”.
^G – Display the current file info, current line number, number of lines in the file and location percentage.
You have now been presented with the basics. There are many more commands and options for vi. In your spare time feel free to crack open a vi manual and learn more.