The vi Editor
Why is it important to learn vi instead of using a
GUI to edit files?
It is on almost every UNIX/Linux system you will
encounter.
- It is the standard editor in all UNIX-related
systems
- It requires little space
- It does not require X Windows or any GUI
Vi is the front end to the ex editor. There is a man
page for both vi and ex.
Features:
- full-screen editor
- two modes of operation: Command and Insert
- Use of one-letter commands
- Flexible search-and-replace facility with
pattern matching
- User-defined editing features using macros
Envoking vi
$ vi filename
- If filename exists, vi creates a copy of
it and puts the copy into a buffer file in the current directory (typically named .yourfilename.swp).
- If filename doesn't exist, vi opens an
empty buffer in the same directory and gives the file the name
specified in your vi command.
Exercise: create a new file
- start at your home directory
$ cd
$ vi testfile
- Notice at the bottom of the screen, the editor is
telling you it is a "New File".
- Exit vi without making any changes, type
:q
- Since you didn't add any lines to the file, it
was not created by vi.
- Lets create a practice file by copying the fstab
file to your home directory
$ cp /etc/fstab .
$ vi fstab
Getting around a file that
exists in vi
Command Mode
Once you type the command vi filename you
leave the shell and move into vi's Command mode.
From the command mode you can:
- move around
- delete text, delete lines
- search forward using the slash (/)
- search backward using the question mark (?)
NOTE: as long as you have not added a
charater you are still in Command mode.
Moving the Cursor
- use arrow keys
- use j (down), h (left), k (up), l
(right)
- $ - takes you to the end of the current
line
- ^ - takes you to the beginning of the
current line
Searching for text
- Typing the forward slash '/' automatically puts
you in text search forward mode.
Exercise: moving around the fstab file in our
home directories
- Once inside vi, find the string "swap" by
typing a forward slash and the word swap
$ vi fstab
- to continue searching in the same direction for
the string, type
n
- to search in the opposite direction, type N
- Typing the question mark '?'
automatically puts you in text search backward mode.
?swap
- Delete two line below the line with the string
swap in it
- Position your cursor below the line swap device
- Type 2dd
- this deletes the current line
and the line beneath it
- Save your changes and exit vi by typing
:wq
General movement within the file
| Moving cursor to top of file |
1G |
| Moving cursor to bottom of file |
G |
| moving to line number 7 |
7G |
| Finding out how many lines are
in the file and the file name |
<Control-g> |
Deleting text in Command mode: lines, words, or characters
| dd
|
remove a whole line |
| u
|
undo the last change |
| U |
Restore the
entire line (if the cursor has not left the line) |
| 2dd
|
delete the current line and
the one below it |
| D
|
delete the remainder of the
line after the cursor |
| yy
|
copy one line |
| 2yy
|
copy two lines |
| P
|
paste the copied or deleted
line(s) above the line with the cursor |
| p
|
paste the copied or deleted
line(s) below the line with the cursor |
| dG
|
deletes to the end of the
file after the cursor |
| dw
|
delete current word - place
cursor on first letter |
| 3dw
|
delete 3 words |
| x
|
delete individual characters
|
| d$
|
delete to the end of the line |
| dG
|
delete to the end of the file |
Insert Mode (also known as
append or text mode)
- The mode you are in when inserting (i) or appending (a) text
- Also for opening lines above (O) or below (o)
the current location
- You leave command mode to add text to a file.
Single letter commands for inserting text:
| i |
inserts to the left of cursor |
| I |
inserts text at beginning of
the line where cursor sits |
| a |
appends to the right of cursor |
| A |
adds text beginning at end of
the line where cursor sits |
| O |
opens a line above the line
where cursor sits |
| o |
opens a line below the line
where cursor sits |
Exercise: practice using these single letter
commands in insert mode
- First, positin the cursor at the point in a new
or existing file where you want to begin inserting text.
$ vi fstab
- move cursor to end of the first line, type $
- then type a -
this will append text to the right of your cursor
- press <return>
and being typing your new line (anything you want)
- Press the Escape to exit insert mode
<Esc>
- now you can move about the file using the arrow
keys
- save your changes and exit
:wq
Leaving insert mode
- To instruct vi to get you out of
Insert mode and back to Command mode press <Esc>.
- If you press <Esc> a second time vi will
send a beep sound to the terminal.
Inserting Text
$ vi fstab
- move cursor to middle of the first line
- type i
- this inserts text to the left of the cursor begin typing text
- press <Esc> to go back to Command mode
<Esc>
now you can move about with the arrow keys
Exit vi without saving changes
:q!
Saving as you go
- It is a good practice to save your work every so
often when inside vi
- If you want to save your work without exiting,
press <Esc> to reenter Command mode, then type the following:
:w
Opening a line above or below the cursor
- make sure you are in Command mode (press <Esc>)
- move cursor to any location on a line
- type the open line command, o (lowercase o),
- this opens a line below the cursor
- press escape to go back to Command mode
<Esc>
- move to another location in the file
- type O (uppercase
O), this opens a line above the cursor
- begin typing
- then save your changes and exit vi
<Esc>
:wq
Summarizing the Append Commands
- i inserts to the left
- I inserts text at beginning of the line
- a appends to the right
- A adds text beginning at end of the
line
- O opens a line above
- o opens a line below
Summarizing the Deleting
Commands
- x deletes a character
- dw deletes a word
- dd deletes a line
- D deletes the remainder
of a line
Moving Text: using yank
and put
Exercise: create a new file
$ cd
$ vi jack
- Type a, to
enter append mode and being typing.
All work and no play makes Jack a
dull boy.
- Press <Esc>
to enter Command mode
- Type yy, to
yank the one line of text you just created
- Type p, to
put the text below the cursor line.
- Now you have:
All work and no play makes Jack a
dull boy.
All work and no play makes Jack a dull boy.
- Add a 3rd line by typing o
after positioning the cursor on bottom line and begin typing:
no play
- Press <Esc>
to enter Command mode
- Move the cursor to the top line and yank two
lines by typing 2yy.
- Move the cursor to the third line (no play) and
type p for put
- Your new file should look like this:
All work and no play makes Jack a
dull boy.
All work and no play makes Jack a dull boy.
no play
All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy.
- Move the cursor to the third line again (no play)
and type yy then P.
- This will put the yanked line above the line
where you placed the cursor.
- The yanked line is still in the vi buffer
- Use the dd command to delete a line and then use the P or p
command to put it somewhere else.
- Deleting a line put that line in the buffer
- You can also experiment with deleting and putting
several lines.
- Save you file
- Jack would be proud!
:wq
Executing Linux/UNIX Commands in
vi
- Lets say you are inside vi and need to run a
command but don't want to exit vi.
- The exclamation point (!)
used in Command mode will create a shell for you to run a
command.
$ cp /etc/passwd .
$ vi passwd
- To list the contents of the current directory
from inside vi:
:!ls
- You can also insert the output of a command into
your file.
G
o
<Esc>
!!date
- Type !!or :.! followed by the command to insert
the output of the command in your text.
- If you have a number of commands you want to run
but you don't want to exit vi you can create a shell by typing
<Esc>
:!sh
- When you are done, exit the shell typing <Ctrl> d or exit
- This will take you back to the file you were
editing.
- You can also read the contents of another file
into the file you are currently editing.
<Esc>
:r jack
:wq
Options for Changing How vi Operates (more advanced)
- The default appearance and behavior
characteristics of vi are set in the .exrc file in your
home directory.
$ cat /home/pattyo/.exrc
set ts=4
A few vi Customizing Options (do not use the
":" inside the .exrc file)
|
:set all
|
display all settings
|
|
:set nu
|
turn on line numbering
|
|
:set nonu
|
turn off line numbering
|
|
:set list
|
display nonprintable characters
|
|
:set nolist
|
hide nonprintable characters
|
|
:set ts=4
|
tab set to four character jumps
|
|
:set ic
|
ignore case sensitivity
|
| :set noic |
set case sensitivity |
|
:set wrapmargin=15
|
set automatic word wrap to 15 spaced before
the right margin
|
$ vi jack
:set nu
:set ic
- search for the string all (lower-case)
/all
:set noic
- search again for the string all
(lower-case)
/all
:q
Advanced Concepts
Global
Changes Substitution
- Change all occurances of the string dull
to fun in our test file jack.
$ vi jack
:%s/dull/fun/g
Two other examples for making global substitutions
- From the current cursor location to the end of
the file substitute:
:.,$s/Jack/Jill/g
- From the beginning of the file to the end of the
file substitute:
:1,$s/Jack/Jill/g
Deletion
- To delete all lines
beginning with Jill
:g/^Jill/d
:g/^$/d
Sorting
- To sort the first column
starting from the line where the cursor is to the first blank line
!}sort
- or
:.,$!sort
- To sort the second column
starting from the line where the cursor is to the first blank line
!}sort +1
Moving large
blocks of text from one file to another
- "a is the
name of the buffer
- you could have a buffer named "b as well
- If you modify the file jack, you must save it
before it will allow you to edit the other file
- Here we are yanking 5 lines from jack
and putting it into the file newjack.
$ vi /tmp/jack
"a5yy
:vi newjack
"ap