Linux
Edit with vi
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:
| Mode |
What it does |
| Movement |
Allows you to
use arrow keys or other control commands to move around within existing
text |
| Insert |
Allows you to
actually insert new text |
| Change |
Allows you to
change existing text, delete lines, change a word, change a
character, undo changes |
| Colon |
Allows you to
execute global edit commands, read/write to a file, exit vi and many more
options |
| Search |
Allows you to
search for text and change the text |
| Oh
Yeah |
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...
| Command |
What it does |
| ^F |
Goes forward a
screen at a time |
| ^B |
Goes backwards a
screen at a time |
| G |
Jumps to the end
of the file |
| #G |
Jumps to a
specific line number. Example: 3G jumps to line
3 |
| space bar
|
Moves forward a
character at a time |
| backspace
key |
Moves backwards
a character at a time |
| ENTER key
|
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!
| Command |
What it does |
| ESC key |
Exit insert
mode |
| i |
Insert characters BEFORE
cursor position |
| I |
Insert characters at
BEGINNING of current line (Capital i) |
| a |
Append characters AFTER
cursor position |
| A |
Append characters at END of
current line |
| o |
Open a new line BELOW
current line |
| O |
Open a new line ABOVE
current line |
Now that you know how to insert,
let's find out how to change existing text.
| Command |
What it does |
| R |
Replace
characters starting at cursor position until ESC |
| J |
Join the line
below to the end of the current line |
| rx |
Replace current
character with the letter "x" |
| cw |
Change the word
at the current position up to the first space. Press ESC after new entry
|
| x |
Delete character
at current position |
| #x |
Delete several
characters starting at current position. Example:
3x deletes 3 characters |
| dw |
Delete the
current word |
| dd |
Delete the
current line |
| #dd |
Delete a number
of lines, starting with current line. Example: 3dd
will delete 3 lines |
| D |
Delete from
cursor to end of the line |
| u |
Undo last
command |
| U |
Restore the
current line |
Now it starts to get interesting.
Let's do the basic colon commands first.
| Command |
What it does |
| ESC |
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 |
| x |
Write the file
and exit |
| q! |
Abort all
changes and exit |
| w |
Write the file
saving current changes, but does not exit vi |
| w <filename> |
Write the
current file to another file name |
| #,#w <filename> |
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" |
| r <filename> |
Reads a file and
inserts it the line below current position |
| !<command> |
Executes a shell
command. Example: !ls will list files in the
current directory |
| 1,$s/thisvalue/withthis/ |
Performs global
substitution starting at line 1 to end of file. The "$" sign represents
end of file |
| 1,$s/thisvalue/withthis/g |
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
" |
| 10,20s/01$/99/ |
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? |
| .s/01\/03\/94/02\/28\/94/ |
- The dot at the beginning
represents the current line only.
- Proceed special
characters, i.e. the "/", with a reverse slash "\".
- The "\"
tells the command to use the NEXT character as a
parameter and NOT the command separator.
- So if you want to change a
"$" in your command you would also proceed it by a "\".
|
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?
(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.
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"
- n - Go on to next "there" and leave the
current "there" as it was
- . (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.
http://www.ahinc.com/linux101/vi.htm
|