vi (vim) E D I T O R ... A Q U I C K R E F E R E N C E
================================================================================
To start the vi editor, type vi [file-name] at the linux$ prompt. When vi starts
you are in COMMAND mode. To add text, you must first get into INPUT mode.
To delete, change, or move text, you must return to COMMAND mode. There are
several ways to get into INPUT mode, as described below. To return to the
COMMAND mode, press the ESC (escape) key. If you get stuck or confused while
using the vi editor, press ESC.
O P E N I N G, S A V I N G, and C L O S I N G F I L E S U S I N G vi
________________________________________________________________________________
vim [file] [ENTER] Open a file using the vim version of vi
ZZ or :wq [ENTER] Exit vi, saving changes (:w write and :q quit)
I N P U T M O D E
________________________________________________________________________________
Note: To get into INPUT mode, type any of these characters from COMMAND mode:
i I a A o O R s S c C . Press ESC (escape) to return to COMMAND mode.
i [text] Insert text before cursor
I [text] Insert text at start of line
a [text] Append text after cursor
A [text] Append text at end of line
o [text] Open a new line below the cursor
O [text] Open a new line above the cursor
R [text] Replace (overwrite) current text
s [text] Substitute one or more characters for a char.
S [text] Substitute an entire line
cw [text] Change a word to [text]
cb [text] Change from beginning of word to cursor
ce [text] Change from cursor to end of word
C [text] Change from cursor to end of line
C O M M A N D M O D E
________________________________________________________________________________
Note: To repeat a command N times enter: N[command]
To repeat the last modification, type: . (period)
To undo the last modification, type: u
-------------------- M O V I N G T H E C U R S O R ---------------------
Note: The arrow keys work but the mouse does NOT move the cursor.
h Move left one space
j Move down one line
k Move up one line
l Move right one space
0 Move to start of line (zero)
$ Move to end of line
[n]G or :[n] Move to the n-th line in the file
G Move to the last line in the file
H Move to first line on screen
L Move to last line on screen
M Move to middle line on screen
w Move to beginning of next word
( Move to beginning of sentence
) Move to end of sentence
{ Move to beginning of paragraph
} Move to end of paragraph
------------------------ D E L E T I N G T E X T -------------------------
x Delete a single character at the cursor
dd Delete an entire line and put into buffer
D Delete from cursor to end of line
dw Delete from cursor to start of next word
df[char] Delete from cursor through [char]
dt[char] Delete from cursor to, but not including [char]
dH Delete from top of screen to current line
dL Delete from current line to bottom of screen
------------ Y A N K I N G A N D P U T T I N G T E X T ---------------
yy or Y Copy a line into buffer without deleting
yw Copy a word into buffer without deleting
p Put last yank or delete after the cursor
P Put last yank or delete before the cursor
xp Reverse two characters
ddp Reverse two lines
J Join the next line to the current line
r [char] Replace a single character with [char>
r [ENTER] Split a line at the cursor
-------------------- P A T T E R N S E A R C H I N G ---------------------
/[pattern] [ENTER] Search forward for [pattern]
n or / [ENTER] Repeat the last search forward
?[pattern] [ENTER] Search backward for [pattern]
N or ? [ENTER] Repeat the last search backward
'' (2 single quotes) Return to previous cursor position
---------- S C R E E N B Y S C R E E N S C R O L L I N G ------------
[CTRL] U Scroll up half a screen
[CTRL] D Scroll down half a screen
[CTRL] F Scroll forward one screen
[CTRL] B Scroll backward one screen
z [ENTER] Redraw the screen, cursor line at top
-------------------- ex E D I T O R C O M M A N D S -----------------
Note: These commands (issued from COMMAND mode) belong to ex, a line editor.
:wq [ENTER] Write (save) the file to disk and quit vi
:w [ENTER] Write (save) the file to disk without quitting
:q! [ENTER] Quit without saving the last changes
:r [file] [ENTER] Copy contents of [file] into your file after the cursor line
:f [ENTER] Show filename and current line
:r [file] [ENTER] Read [file] into buffer at cursor position
:! [command] [ENTER] Execute a UNIX command without quitting vi
:1,$ s/[old]/[new]/g Replace [old] with [new] , throught the file
:n,m s/[old]/[new]/g Replace [old] with [new] , between lines n and m
:set spell Highlight mispelled words (spell-check)
:set nospell stop spell-check
g [CTRL] g Word count and character count
U N I X C O M M A N D S
================================================================================
Note: These commands are not part of the vi editor.
Issue these commands from the unix% prompt.
ls, ls -l, ls -la [dir] List contents of a directory
cp [file1] [file2] Copy [file1] to [file2]
mv [file2] [file2] Move [file1] to [file2]
rm [file] Remove (delete) a file
mkdir [dir] Create a new directory
cd [dir] Change the current directory
rmdir [dir] Remove (delete) a directory
[CTRL] z Stop the current process
bg Resume a stopped process in the background
ps -a List all processes and characteristics
kill -9 [job number] Kill a stopped job (process, or program)
man [command] Display manual pages about a command, such as vi