Docsity

VI Cheat Sheet: Editing Commands and Shortcuts, Cheat Sheet of Operating Systems

Toggle sidebar
bg1
vi Cheat Sheet
Editing Modes
Command mode Keystrokes used as commands
Insert mode Characters inserted into file
Invoking and Terminating
Invoking % vi filename
Make sure terminal setup is correct (% set term=vt100)
% vi -R
filename
invoke vi in read only mode
Terminating :wq exit vi, saving changes
:q! quit vi, without saving changes
Recovering Lost
Editing Sessions % vi -r
filename
recover file after system crash
% vi -r list all recoverable files
Initialization .exrc file containing ex commands to be
executed upon startup
EXINIT environment variable containing vi
commands to be executed upon startup
% vi filename
command mode insert mode
%
i, a โ€” insert mode command
<ESC> โ€” escape
:wq โ€” exit (save changes)
:q! โ€” quit (discard changes)
Cursor Movement
Inserting Text
Moving by characters h (or backspace)
move left one character
jmove down one line
kmove up one line
l (or space) move right one character
Arrow keys may also be used
Moving by lines 1G go to first line
n
Ggo to
n
th line
Ggo to last line
^go to beginning of line
$go to end of line
Moving by words wgo forward one word
bgo backward one word
Scrolling Ctrl-f scroll forward one screen
Ctrl-b scroll backward one screen
Ctrl-d scroll down one half screen
Ctrl-u scroll up one half screen
Inserting Text The following commands place you in insert mode:
iinsert text before cursor
aappend text after cursor
Iinsert text at beginning of line
Aappend text at end of line
ostart adding text in new line below
present line
Ostart adding text in new line above
present line
Use Escape to return to command mode
Replacing Text r
char
substitute
char
for character under
cursor
Roverwrite old text (end with Esc)
wchange current word to new word (end
with Esc)
Setting Margins :set wrapmargin=
x
Sets right margin to
x
, automatic word wrap
pf2

Partial preview of the text

Download VI Cheat Sheet: Editing Commands and Shortcuts and more Cheat Sheet Operating Systems in PDF only on Docsity!

vi Cheat Sheet

Editing ModesCommand mode

Keystrokes used as commands

Insert mode

Characters inserted into file

Invoking and TerminatingInvoking

%^ vi^ filename Make sure terminal setup is correct (

% set term=vt

%^ vi^ -R

filename^ invoke vi in read only mode

Terminating

:wq^

exit vi, saving changes :q!^

quit vi, without saving changes

Recovering LostEditing Sessions

%^ vi^ -r

filename^ recover file after system crash %^ vi^ -r

list all recoverable files

Initialization

.exrc^

file containing ex commands to beexecuted upon startup EXINIT^

environment variable containing vicommands to be executed upon startup

% vi filenamecommand mode

insert mode

i, a โ€” insert mode command โ€” escape

:wq โ€” exit (save changes):q! โ€” quit (discard changes)

Cursor MovementMoving by characters Inserting Text

h^ (or backspace)

move left one character j^

move down one line k^

move up one line l^ (or space)

move right one character Arrow keys may also be used

Moving by lines

1G

go to first line nG^

go to^ nth line G^

go to last line ^^

go to beginning of line $^

go to end of line

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 Ctrl-d^

scroll down one half screen Ctrl-u^

scroll up one half screen

Inserting Text

The following commands place you in insert mode:^ i^

insert text before cursor a^

append text after cursor I^

insert text at beginning of line A^

append text at end of line o^

start adding text in new line belowpresent line O^

start adding text in new line abovepresent line Use Escape to return to command mode

Replacing Text

rchar^

substitute

char^ for character under cursor R^

overwrite old text (end with Esc) w^

change current word to new word (endwith Esc)

Setting Margins

:set wrapmargin=

x Sets right margin to

x, automatic word wrap

Deleting, Copying, and Pasting TextDeleting Text Searches and Substitution

x^

delete character at cursor dd^

delete line ndd^

delete^ n^ lines ndw^

delete^ n^ words d$^

delete to end of line d^^

delete to beginning of line dG^

delete to end of file d1G^

delete to beginning of file

Copying Text

yy^

yank current line into buffer nyy^

yank^ n^ lines into buffer yw^

yank word nyw^

yank^ n^ words

Pasting Text

p^

put most recently deleted or yankedcharacters or words after cursor; put linesbelow current line P^

put most recently deleted or yankedcharacters or words before cursor; putlines above current line

Simple Searches

/string

search forward for

string

?string

search backward for

string

n^

repeat search N^

repeat search in opposite direction

Global Search

:g/string/command

execute^ command

on lines containing string :v/string/command

execute^ command

on lines not containing string

Substitution

:x,ys/oldstring/newstring/flags Substitutes

newstring

for^ oldstring

in line range

x,y.

flags^ (optional):^ c^

wait for confirmation (

y^ or^ n)

g^

replace all occurrences within specifiedlines (default is to replace only firstoccurrence) Range specifiers:^.^

current line $^

last line %^

entire file

Searches and Substitution (cont.)Special Characters Miscellaneous Commands

[ccc]

matches any specified character [^ccc]^

matches any character except thosespecified [c1-c2]

matches any character in range ^^

matches beginning of line $^

matches end of line

.^

matches any one character *^

matches zero or more occurrences ofprevious character .*^

matches any number of characters

Case Sensitivity

:set ignorecase

disable case sensitivity :set noic

enable case sensitivity

Shell Commands

command

execute

command :sh^

invoke subshell; use the

exit^ command to return to vi :r !command

insert output of

command

below current line

Undoing Commands

u^

undo last change U^

undo recent changes made to current line

Repeating Com-mands

.^

repeat last command that made anediting change

Reading in Files

:r

filename

insert

filename

below current line

Joining Lines

J^

join next line to current line

Line Numbers

Ctrl-g^

display current line number and numberof lines in file :set number

display line numbers :set nonumber

remove displayed line numbers

Screen Refresh

Ctrl-l^

refresh screen