Vi has 3 modes:
command mode - Normal and initial state; others return here (use ESC to abort a partially typed command)
input mode - entered by specific commands a i A I o O c C s S R and ended by ESC or abnormally with interrupt
line mode - i.e. waiting for input after a : , / , ? or a ! command (end with CR , abort with CTRL-c ). CTRL is the control key: CTRL-c means "control c"
TERM= code Puts a code name for your terminal into the variable TERM
export TERM Conveys the value of TERM (the terminal code) to any UNIX system program that is terminal dependant.
tput init Initializes the terminal so that it will function properly with various UNIX system programs.
vi filename Accesses the vi screen editor so that you can edit a specified file.
vi file1 file2 file3 Enters three files into the vi buffer to be edited. Those files are file1, file2, and file3 .
view file Invoke vi editor on file in read-only mode
vi -R file Invoke vi editor on file in read-only mode
vi -r file Recover file and recent edits after system crash
:set option Activate option
:set option=value Assign value to option
:set no option Deactivate option
:set Display options set by user
:set all Display list of all current options, both default and those set by the user
:set option ? Display values of option
Notations:
CTRL-c CTRL is the control key: CTRL-c means "control c"
CR is Carriage return (ENTER key)
ESC end insert or incomplete command
CTRL-? CTRL is the control key: CTRL-? means "control ?" delete or rubout interrupts
CTRL-l reprint/refresh screen if CTRL-? scrambles it
ZZ Save the file and exit vi
:wq Save the file and exit vi
:w Write the current file
:w! Force write the current file, if file is read-only
:w name Write to file name
:q Exit from vi
:q! Force exit from vi (discarding changes)
:e name Edit file name
:e! reedit, discard changes
:e + name edit file name , starting at end
:e + n edit starting at line n
:e # edit alternate file
:n edit next file in arglist
:args list files in current filelist
:rew rewind current filelist and edit first file
:n args specify new arglist
:f show current file and line
CTRL-G synonym for :f , show current file and line
:ta tag to tag file entry tag
CTRL-] :ta, following word is tag
Arrows Move the cursor
CTRL-d Scroll half page down
CTRL-u Scroll half page up
CTRL-f Scroll a full page down
CTRL-b Scroll a full page up
:0 Move to start of file
:n Move to line number n
:$ Move to end of file
0 Move to start of line
^ Move to first non-blank character
$ Move to end of line
CR Move to the start of next line
- Move to the start of previous line
% Find matching bracket
G goto line (last line default)
]] next section/function
[[ previous section/function
H Home window line
L Last window line
M Middle window line
+ Next line, at first non-white
- Previous line, at first non-white
CR return, same as +
j next line, same column
k previous line, same column
0 beginning of line
$ end of line
h forward
l backwards
SPACE same as l
fx find x forward
Fx find x backward
; repeat last f F
, inverse of ;
| to specified column
% find matching { or }
w Word forward
b Word backward
e End of word
) To next sentence
( Back sentence
} To next paragraph
{ Back paragraph
W Blank delimited word
B Back W
E To end of W
`` (press twice the back-quote ` key) Previous context
'' (press twice the single-quote ` key) Previous context at first non-white in line
mx mark position with letter x
`x (back quote key and letter x) goto mark x
'x goto mark x at first non-white in line
CTRL-h Erase last character
CTRL-w Erase last word
erase Press DELETE key, same as CTRL-h
kill Your kill key, erase input this line
\ Escapes CTRL-h, DELETE and kill
ESC Ends insertion, back to command
CTRL-? Interrupt, terminates insert
CTRL-d Backtab over autoindent
CTRL-v Quote non-printing character
CTRL-l Clear and redraw
CTRL-r retype, eliminate @lines
z-CR redraw, current line at window top
z- redraw, current line at window bottom
z. redraw, current line at window center
/pat/z- pat line bottom
tn Use n line window
CTRL-e Scroll window down 1 line
CTRL-y Scroll window up 1 line
x Delete the character under the cursor
X Delete the charater before the cursor
D Delete to the end of line
d^ Delete back to start of line
dd Delete the current line
ndd Delete n lines starting with the current one
dnw Delete n words starting from cursor
i Enter input mode inserting before the cursor
I Enter input mode inserting before the first non-blank character
a Enter input mode inserting after the cursor
A Enter input mode inserting after the end of the line
o Open a new line below current line and enter input mode
O Open a new line above current line and enter input mode
r Replace the character under the cursor (does NOT enter input mode)
R Enter input mode replacing characters
C shift-c. Change rest of line
D shift-d. Delete rest of line
s Substitute chars
S Substitute lines
J Join lines
J Join lines
The "yank buffer" is filled by EVERY delete command, or explicitely by Y and yy .
Y Copy the current line to the yank buffer
n yy Copy n lines starting from the current to the yank buffer
p Paste the yank buffer after the cursor (or below the current line)
P Paste the yank buffer before the cursor (or above the current line)
" x p Put from buffer x
" x y Yank to buffer x
" x d Delete into buffer x
d delete
c change
< left shift
> right shift
! filter through command
= indent for LISP
y yank text to buffer
/ text Search forward for text
? text Search backward for text
n Repeat the last search in the same direction
N Repeat the last search in the reverse direction
/ Repeat the last search forward
? Repeat the last search backward
[ addr ] s/from/to/ [ g ] Search for the occurence of from and replace it with to in the current line, or in the range addr (two line numbers seperated by command; 1,$ is the whole file). Replaces one occurrence per line, or all occurrences if g is specified. For example, :3,20s/someword/anotherword/g Will replace "someword" with "anotherword" starting from line 3 to line 20. 'g' is global means replace all occurrences of "someword".
:sh Forks a shell (to be exited with CTRL-d)
:! command Forks a shell to execute command
:set number Switch on line numbering
:set nonumber Switch off line numbering
: Tells vi that the next commands you issue will be line editor commands.
:sh Temporarily returns to the shell to perform some shell commands without leaving vi .
CTRL-d Escapes the temporary return to the shell and returns to vi so you can edit the current window.
: n Goes to the n th line of the buffer.
: x,z w filename Writes lines from the numbers x through the number z into a new file called filename .
:$ Moves the cursor to the beginning of the last line in the buffer.
:.,$d Deletes all the lines from the current line to the last line
:r filename Inserts the contents of the file filename under the current line of the buffer.
:s /text/new_text/ Replaces the first instance of text on the current line with new_text
:s /text/new_text/g Replaces the every occurrence of text on the current line with new_text
:g /text/s//new_text/g Changes every occurrence of text on the buffer to new_text .