The Sysadmin Notebook  

Sitemap

Vi Notes

Notes for using Vi

Contents

Insertion Mode

Top Bottom
Insertion Mode
Keystroke Function
a (A) append (at end of line)
c (C) start editing (from current position to end of line)
i (I) insert (at beginning of line)
o (O) begin editing a new line below (above) cursor
r (R) replace character (characters)
s (S) substitute character (entire line)

Starting vi

Top Bottom
Starting Vi
Keystroke Function
vi open a new, unnamed file
vi file open file
vi +14 file open file with cursor at line 14
vi +/pattern file open file with cursor at first occurence of pattern

Movement

Top Bottom
Movement
Keystroke Function
w (b) {e} move forward (backward) {forward} a word at a time placing cursor at beginning (beginning) {end}of word
0 ($) beginning (end) of line
H (M) {L} top (middle) {bottom} line of current screen
ctrl+f (b) scroll a screenful forward (back)
ctrl+e (y) show one more line at top (bottom) of screen
z return (.) {-} reposition screen with current line at the top (middle) {bottom}
(?) /pattern search (backward) forward for pattern
n (N) repeat last search (in opposite direction)
% find match of current bracket, parens or braces
ctrl+m go to first non-blank character of next line

Cut and Paste

Top Bottom
Cut and Paste
Keystroke Function
y copy
p (P) paste below,after (above, before)
d cut
u undo
r redo
:di or :reg display registers
"xp put content of register x
J join current line to previous line

Visual Mode

Top Bottom

Enter visual mode with 'v' (character-wise), 'V' (line-wise). Exit visual mode witn <ESC>

Visual Mode
Keystroke Function
d delete
c change
y yank
> shift right
< shift left
! filter through external command
= filter through option command
gq format lines to text width length

Opening and Closing

Top Bottom
Opening and Closing Files
Keystroke Function
:w write
:q quit
:q! quit don't save
:wq! write (over-riding protection settings)
:e file edit another file, named file
:e! restore last saved version of current file
:n edit next file
:e# return to previous file

Interacting with the Shell

Top Bottom
Interacting with the shell
Keystroke Function
:r read in contents of file
:r !command read in output from command
:!command run command
:sh run subshell

Substituting

Top Bottom
Search and Replace
Keystroke Function
1,\$s/this/that/ (%s/this/that/) substitute "this" for "that" once on every line
.s/this/that/g substitute all occurrences of "this" for "that" on current line
x,y lines x to y