SMU School of Engineering
General Use UNIX Machines
Useful Vi Tips
What
is the best way to learn Vi?
My advice is for people to follow the tutorials
and read the man page. Experience is the best teacher!
How
do I get into a file using Vi?
Just type Vi filename (where filename
is any name you want).
How do I move the cursor
in the file?
- l moves the cursor one character to the right
- h moves the cursor one character to the left
- k moves the cursor up one row
- j moves the cursor down one row
- G moves the cursor to the bottom of the file
- 1G moves the cursor to the top of the file
- H moves the cursor to the top row on the screen
- M moves the cursor to the middle row on the screen
- L moves the cursor to the last row on the screen
How do I insert text?
- i inserts text immediately before current
cursor position
- a appends text immediately after current
cursor position
- o opens line below current one for input
- I inserts to beginning of current line
- A appends text to end of current line
- O opens line above current one for input
Once you entered input mode, the way to return to command
mode is by hitting the Escape key: ESC or CONTROL-[.
How do I delete something?
dw deletes current word (starting from current cursor position)
dG deletes to bottom of file
d) deletes a sentence (sentences are delimited by TWO spaces)
d% deletes to the matching bracket, brace, or parenthesis
d'' deletes to the last place we jumped from
dtp deletes through the letter p, but leaves the p
dfp deletes to and including the letter p
How to undo
and repeat commands?
- u undoes the last action you did
- . (period) repeats the last command you
did
I just finished my document, how do
I save?
There are several save options. One of these should
work for you:
- :w writes the file and save the changes
- :w name same, but the changes go into
a file with the new name
- :x,y w name writes only the lines x to
y to the file (for excerpting)
- :wq writes and quits at the same time
- ZZ writes and quits at the same time
How can I include text in my document?
:r name reads in the file name at the point of the cursor
:r!cmd executes the shell command cmd and inserts output
at the point of the cursor
How can I edit my text?
J joins next line to current line
x deletes the character under the cursor
r* replaces the character under the cursor with *
sstring ESC substitutes the character under the cursor with string
cwstring ESC changes current word to string
c$string ESC changes from cursor to end of line with string
ccstring ESC changes current line to string
Is
there a way I can cut and paste?
yy yanks lines into a temporary buffer, where they are
saved for later use
p pastes the previously yanked lines
The yanked lines can be put as often as one likes, which
is a good way to repeat things. Also, deleted lines can be put as well, so dd
is the same as yy dd. Also, there is only one default buffer. To move
two things, or to yank and put with other work in between, you can save to named
buffers. For example "ayy will yank a line in buffer a. Then buffer
a can be pasted using "ap.
How do I change the case of letters?
Use the tilde (~) to toggle the
case (lower/upper) of letters. It will ignore non alphabetical characters.
How do I switch between files when
editing multiple files?
When editing several files, you can switch to file #x
using the command: :e#x
Is there a way I can switch two characters?
You can reverse 2 characters in a word by typing xp
(x to delete and p to paste).
How can I search
for specific text?
/ pattern searches for pattern in the text after the cursor
? pattern searches for pattern in the text before the cursor
n keeps searching in the same direction
N keeps searching in the opposite direction
Can deleting be used with a search
operation?
d/pattern ESC deletes up to the pattern,
but leaves the pattern. Then neat thing about this principle is it works with
most other Vi commands such as y.
What are some examples
of search and replace patterns?
:s/pattern/xx/ changes first pattern in current line to xx
:s/pattern/xx/g changes every pattern in current line to xx
:%s/pattern/xx/g changes every pattern in all lines to xx
:%s/pat/xx&yy/g changes every pat in all lines to xxpatyy
:3s/pattern/\U& changes first pattern in third line to PATTERN
As usual, type u to undo and & to repeat
a substitution.
Is there a way to execute
a unix command from inside Vi?
Type :!cmd where cmd
is a unix command.
How can I set options
inside Vi?
Type :!set name value
where name is the option you want to change and value
is what you want to set it to.
For a list of options, type :set all.
I'm tired of having
to redefine all of my options every time I go into Vi, is there any
way around this?
You can setup a file named .exrc in your home
directory that has the following syntax:
set ai
set wrapmargin=1
map @* I/*^[A*/^[
map @f !}fmt^M
map @d :r !date +"\%a \%b \%d \%y"^M
map @t :%s/^I/ /g^M
I'll explain this line by line:
1: sets the autoindent mode
2: sets a wrapmargin so text will auto-wrap at end of line
3: implements line commenting (C languagne). To comment a line, hit @*
4: implements paragraph formatting. To reformat a paragraph, hit @f
5: implements a current date function. To insert the current date, hit @d
6: converts tabs to 4 spaces. All you need to do is hit @t
Notes:
- Please note that the .exrc file contains
commands that Vi reads when you first run it. The default directory
that Vi looks in for the .exrc file is your home directory.
However, if you run Vi from another directory containing a .exrc
file, Vi will use the .exrc in that directory and bypass the
home directory .exrc.
- You must precede control characters (like
^[ or ^M) with CONTROL-V. For example to enter ^M,
type CONTROL-V CONTROL-M.
How do I reformat paragraphs
in Vi?
Rather than write a set of macros to do this for
you, it's usually easier just t o use an external program that does it for you.
If you have fmt(1), then {!}fmt will reformat the paragraph
under the cursor. This can be fitted into a macro easily
enough.
I hope this answers many of your questions. If you
have more questions, you can check the USENET
Vi FAQ.
Good luck, and have fun!