vi +216 <filename> | Start editing at line 216 of filename. This is quite handy when your compiler tells you you have an error
on line 216.
|
vi +/tropospheric <filename> | Start editing at the
first position in filename containing string tropospheric
(This works with regular expressions too).
|
vi -r <filename> | Recover file from a vi backup. This
allows you to continue editing a file in the case that the editor was killed
by a signal. So, if you were in the middle of editing a file when the system
reboots, you can probably resume editing this way,
|
vi file1 file2
|
Edit multiple files. This will open up "file1" first, and when
you are done with file1, you type :n to move to "file2".
If you want to save file1, you'd instead enter :wn
The main reason for doing all this instead of editing the files seperately
is that you can yank and put between files this way. This will all make
sense as you read the section on buffers.
|
There are three modes in vi:
j |
moves the cursor down one line |
k |
moves the cursor
up one line |
h |
moves the cursor
left one column |
l |
moves the cursor
right one column |
CTRL-f |
moves the cursor
down one screen |
CTRL-b |
moves the cursor
up one screen |
G |
moves the cursor
to the end of the doc. |
nG |
moves the cursor
to line n |
w |
moves the cursor
forward one word |
b |
moves the cursor
back one word |
^ |
moves the cursor
to start of the line |
$ |
moves the cursor
to end of the line |
i |
change to input
mode (characters are inserted at current position) |
a |
change to input
mode (characters are inserted AFTER current position) |
I |
change to input
mode (characters are added at the beginning of the current line) |
A |
change to input
mode (characters are added at the end of the current line) |
R |
change to input
mode (replaces or overwrites old text) |
r |
change to input
mode (overwrites only the current character) |
C |
change to input
mode (rest of the current line is replaced by new text) |
o |
change to input
mode (after current line a new line is added for text) |
O |
change to input
mode (before current line a new line is added for text) |
cw |
change to input
mode (the rest of the current word is replaced and put into the default buffer) |
:q |
quits vi without saving
changes |
:w <filename> |
saves in <filename> |
:x |
saves changed file and quits
(x! may be required to save and quit read only files) |
:e <filename> |
edits <filename>. If filename is not specified and the
command is followed by a bang (!), the current file is reloaded
from disk (aborting changes). |
:E <filename> |
opens <filename> in a new window. You can switch to the new
window and back with CTRL+W, and yank and put operations work between
windows. This appears to only work in the nvi clone of vi (the only
official replacement for the original CSRG vi). If you're not using
nvi and want to yank text from one file and put it in another, there
is another way, opening two files from the command line, which is
explained at the top of this page.
|
:file |
Tells you the file name and line number, among other things.
|
:nu |
Tells you the line number and its contents.
|
:s/text1/text2/ |
Substitutes text2 for text1 in the current line the first time it
occurs.
|
:s/text1/text2/g |
Substitutes text2 for text1 in the current line every time it
occurs.
|
:17,26s/text1/text2/g |
Substitutes text2 for text1 in every line from line 17 through 26
every time it occurs.
|
:%s/text1/text2/g |
Substitutes text2 for text1 in every line every time it
occurs.
|
:%s/text1/text2/gc |
Substitutes text2 for text1 in every line every time it
occurs, after prompting you to be sure for each substitution.
|
All of the EX mode substitute commands can take