A Brief Intro to vi

Introduction

The vi editor and its clones elvis, vim, vile, nvi and several others are common on various versions of Unix and Linux; vi is routinely shipped with MacOS X and there are versions for Windows. The purpose of these pages is not to make you an expert, but rather, to get you started with a powerful but sometimes difficult to learn editor. There are many much longer tutorials that try to make you a vi expert; the vi lovers home page has a good index of these.

General Notes

Vi is designed with several interesting features that speed editing for experienced users but can be very frustrating for beginners:

Common Problems

 

Some Common vi Commands

Launching vi from the command line
to edit file.txt vi file.txt[CR]    ([CR] means return or enter)
to create file.txt vi file.txt[CR]    (file.text will be created by :w)
Cursor movement
      left h (may use arrow keys)
down j 
up k 
right l (or space)
to next (previous) word w (b) 
to start of next line [CR] 
to line 55 of the file :55[CR] 
to end of the file :$[CR] 
Search
to next xxx in file /xxx[CR] 
to previous xxx in file ?xxx[CR] 
match xpx or xqx or xrx /x.x[CR] 
match xx or xyx or xyyyx /xy*x[CR] 
match only xax or xbx /x[ab]x[CR] 
Undo
undo last change u 
Save Changes and Exit
save changes :w[CR] 
save to temp.c :w temp.c[CR] 
save this line to temp.c :.w temp.c[CR] 
exit :q[CR] 
force exit without saving :q![CR] 
Yank (copy) text into cut-paste buffer
current word yw 
current line yy 
next 5 lines 5yy 
Delete text (cut into cut-paste buffer)
current character x 
current word dw 
to end of line D 
current line dd 
next 5 lines 5dd 
all lines containing xxx :1,$g/xxx/d[CR] 
Paste from cut-paste buffer
after this character or line p (lower case)
before this character or line P (upper case)
Insert text into file
append xxx after cursor axxx[ESC]    ([ESC] means the escape key)
append xxx at end of this line   Axxx[ESC] 
insert xxx before cursor ixxx[ESC] 
insert xxx at start of this line Ixxx[ESC] 
from temp.txt after this line :r temp.txt[CR] 
Replace
current character with x rx 
next few characters with xxx Rxxx[ESC] 
on this line, all xxx with yy :s/xxx/yy/g[CR] 
globally, all xxx with yy :1,$g/xxx/s//yy/g[CR]