0% found this document useful (0 votes)
40 views

VI Cheat Sheet

This cheat sheet summarizes key commands for navigating, editing, searching, and working with files in the vi text editor. It covers movement within files using cursor keys, words, lines, and screens. It also covers inserting, deleting, copying, and pasting text, as well as searching, substitution, undoing changes, and reading/writing files. The cheat sheet is designed to provide vi users with an at-a-glance reference to its many commands.

Uploaded by

zogzog
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

VI Cheat Sheet

This cheat sheet summarizes key commands for navigating, editing, searching, and working with files in the vi text editor. It covers movement within files using cursor keys, words, lines, and screens. It also covers inserting, deleting, copying, and pasting text, as well as searching, substitution, undoing changes, and reading/writing files. The cheat sheet is designed to provide vi users with an at-a-glance reference to its many commands.

Uploaded by

zogzog
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

vi Cheat Sheet Cursor Movement

Moving by characters h (or backspace)


Editing Modes move left one character
j move down one line
Command mode Keystrokes used as commands k move up one line
Insert mode Characters inserted into file l (or space) move right one character

Arrow keys may also be used

% vi filename Moving by lines 1G go to first line


nG go to nth line
i, a insert mode command G go to last line
^ go to beginning of line
$ go to end of line

command mode insert mode Moving by words w go forward one word


b go backward one word
Scrolling Ctrl-f scroll forward one screen
Ctrl-b scroll backward one screen
<ESC> escape Ctrl-d scroll down one half screen
Ctrl-u scroll up one half screen
:wq exit (save changes)
:q! quit (discard changes) Inserting Text
%
Inserting Text The following commands place you in insert mode:

Invoking and Terminating i insert text before cursor


a append text after cursor
Invoking % vi filename I insert text at beginning of line
A append text at end of line
Make sure terminal setup is correct (% set term=vt100)
o start adding text in new line below
% vi -R filename present line
invoke vi in read only mode O start adding text in new line above
present line
Terminating :wq exit vi, saving changes
:q! quit vi, without saving changes Use Escape to return to command mode
Recovering Lost % vi -r filename Replacing Text r char substitute char for character under
Editing Sessions recover file after system crash cursor
R overwrite old text (end with Esc)
% vi -r list all recoverable files w change current word to new word (end
with Esc)
Initialization .exrc file containing ex commands to be
executed upon startup Setting Margins :set wrapmargin=x

EXINIT environment variable containing vi Sets right margin to x, automatic word wrap
commands to be executed upon startup
Deleting, Copying, and Pasting Text Searches and Substitution (cont.)
Deleting Text x delete character at cursor Special Characters [ccc] matches any specified character
dd delete line [^ccc] matches any character except those
ndd delete n lines specified
ndw delete n words [c1-c2] matches any character in range
d$ delete to end of line ^ matches beginning of line
d^ delete to beginning of line $ matches end of line
dG delete to end of file . matches any one character
d1G delete to beginning of file * matches zero or more occurrences of
previous character
Copying Text yy yank current line into buffer
.* matches any number of characters
nyy yank n lines into buffer
yw yank word Case Sensitivity :set ignorecase
nyw yank n words disable case sensitivity
:set noic enable case sensitivity
Pasting Text p put most recently deleted or yanked
characters or words after cursor; put lines
below current line
Miscellaneous Commands
P put most recently deleted or yanked
characters or words before cursor; put Shell Commands :!command execute command
lines above current line :sh invoke subshell; use the exit command
to return to vi
:r !command insert output of command below current
Searches and Substitution line
Undoing Commands u undo last change
Simple Searches /string search forward for string U undo recent changes made to current line
?string search backward for string
n repeat search Repeating Com- . repeat last command that made an
N repeat search in opposite direction mands editing change

Global Search :g/string/command Reading in Files :r filename insert filename below current line
execute command on lines containing
string Joining Lines J join next line to current line
:v/string/command Line Numbers Ctrl-g display current line number and number
execute command on lines not containing of lines in file
string
:set number display line numbers
Substitution :x,ys/oldstring/newstring/flags :set nonumber remove displayed line numbers
Substitutes newstring for oldstring in line range x,y. Screen Refresh Ctrl-l refresh screen
flags (optional):
c wait for confirmation (y or n)
g replace all occurrences within specified
lines (default is to replace only first
occurrence)

Range specifiers:

. current line
$ last line
% entire file

You might also like