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

VI cheat sheet

The document provides a comprehensive guide to using the VI text editor, detailing commands for starting and ending sessions, cursor movements, text insertion, deletion, searching, yanking, and changing text. It also covers settings, buffers, markers, and shell functions, along with regular expressions and the use of ranges in commands. The information is structured into sections for easy reference, making it a valuable resource for both beginners and experienced users of VI.

Uploaded by

Franco Ronconi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

VI cheat sheet

The document provides a comprehensive guide to using the VI text editor, detailing commands for starting and ending sessions, cursor movements, text insertion, deletion, searching, yanking, and changing text. It also covers settings, buffers, markers, and shell functions, along with regular expressions and the use of ranges in commands. The information is structured into sections for easy reference, making it a valuable resource for both beginners and experienced users of VI.

Uploaded by

Franco Ronconi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

– Motion –

– Starting and Ending VI –


h Move left
Starting VI j Move down
vi filename Edits filename k Move up
vi -r filename Edits last save version of filename after a crash l Move right
vi + n filename Edits filename and places curser at line n These do work, but they may be too slow on big files. Also may have
vi + filename Edits filename and places curser on last line Arrow Keys unpredictable results when arrow keys are not mapped correctly in
vi +/string filename Edits filename and places curser on first occurance of string client.
vi filename file2 ... Edits filename, then edits file2 ... After the save, use :n w Move to next word
W Move to next blank delimited word
Ending VI b Move to the beginning of the word
ZZ or :wq or :x Saves and exits VI B Move to the beginning of blank delimted word
:w Saves current file but doesn't exit ^ Moves to the first non-blank character in the current line
:w! Saves current file overriding normal checks but doesn't exit + or Moves to the first character in the next line
:w file Saves current as file but doesn't exit - Moves to the first non-blank character in the previous line
:w! file Saves to file overriding normal checks but doesn't exit e Move to the end of the word
:n,mw file Saves lines n through m to file E Move to the end of Blank delimited word
:n,mw >>file Saves lines n through m to the end of file ( Move a sentence back
:q Quits VI and may prompt if you need to save ) Move a sentence forward
:q! Quits VI and without saving { Move a paragraph back
:e! Edits file discarding any unsaved changes (starts over) } Move a paragraph forward
:we! Saves and continues to edit current file [[ Move a section back
]] Move a section forward
0 or | Move to the begining of the line
n| Moves to the column n in the current line
– Status –
$ Move to the end of the line
:.= Shows current line number 1G Move to the first line of the file
:= Shows number of lines in file G Move to the last line of the file
Shows filename, current line number, total lines in file, and % of file nG Move to nth line of the file
Control-G
location :n Move to nth line of the file
Displays tab (^l) backslash (\) backspace (^H) newline ($) bell (^G) fc Move forward to c
l
formfeed (^L^) of current line
Fc Move back to c
– Modes – H Move to top of screen
nH Moves to nth line from the top of the screen
Vi has two modes insertion mode and command mode. The editor begins in command
M Move to middle of screen
mode, where the cursor movement and text deletion and pasting occur. Insertion mode
begins upon entering an insertion or change command. [ESC] returns the editor to L Move to botton of screen
command mode (where you can quit, for example by typing :q!). Most commands nL Moves to nth line from the bottom of the screen
execute as soon as you type them except for "colon" commands which execute when
Control-d Move forward ½ screen
you press the ruturn key.
Control-f Move forward one full screen
– Inserting Text – Control-u Move backward ½ screen
Control-b Move backward one full screen
i Insert before cursor
CTRL-e Moves screen up one line
I Insert before line
CTRL-y Moves screen down one line
a Append after cursor
CTRL-u Moves screen up ½ page
A Append after line
CTRL-d Moves screen down ½ page
o Open a new line after current line
CTRL-b Moves screen up one page
O Open a new line before current line
CTRL-f Moves screen down one page
r Replace one character
CTRL-I Redraws screen
R Replace many characters
z z-carriage return makes the current line the top line on the page
While inserting, ignores special meaning of char (e.g., for
CTRL-v char nz Makes the line n the top line on the page
inserting characters like ESC and CTRL) until ESC is used
z. Makes the current line the middle line on the page
:r file Reads file and inserts it after current line
nz. Makes the line n the middle line on the page
:nr file Reads file and inserts it after line n
z- Makes the current line the bottom line on the page
CTRL-i or
While inserting, inserts one shift width nz- Makes the line n the bottom line on the page
TAB
% Move to associated ( ), { }, [ ]
Things to do while in Insert Mode:
CTRL-h or – Putting text –
While inserting, deletes previous character
Backspace
p Put after the position or after the line
CTRL-w While inserting, deletes previous word
P Put before the poition or before the line
CTRL-x While inserting, deletes to start of inserted text
"(a-z)p or "(a- Pastes text from a named buffer a through z after or before the
Take the next character literally. (i.e. To insert a Control-H, z)P current line
CTRL-v type Control-v Control-h)
– Deleting Text – – Search for strings –

Almost all deletion commands are performed by typing d followed by a motion. For /string Search forward for string
example, dw deletes a word. A few other deletes are: ?string Search back for string
x Delete character to the right of cursor n Search for next instance of string
Deletes n characters starting with current; omitting n deletes current N Search for previous instance of string
nx
character only
% Searches to beginning of balancing ( ) [ ] or { }
X Delete character to the left of cursor
fc Searches forward in current line to char
Deletes previous n characters; omitting n deletes previous character
nX Fc Searches backward in current line to char
only
D Delete to the end of the line tc Searches forward in current line to character before char
d$ Deletes from the cursor to the end of the line Tchar Searches backward in current line to character before char
dd or :d Delete current line ?str Finds in reverse for str
ndw Deletes the next n words starting with current
ndb Deletes the previous n words starting with current :set ic Ignores case when searching
ndd Deletes n lines beginning with the current line :set noic Pays attention to case when searching
:n,md Deletes lines n through m Searches from n to m for str1; replaces str1 to str2; using opt-opt can be
:n,ms/str1/
Deletes everything included in the Motion Command (e.g., dG g for global change, c to confirm change (y to acknowledge, to
str2/opt
dMotion_cmd would delete from current position to the end of the file, and d4 suppress), and p to print changed lines
would delete to the end of the fourth sentence). & Repeats last :s command
"np Retrieves the last nth delete (last 9 deletes are kept in a buffer) :g/str/cmd Runs cmd on all lines that contain str
Scrolls through the delete buffer until the desired delete is retrieved :g/str1/s/
"1pu.u. Finds the line containing str1, replaces str2 with str3
(repeat u.) str2/str3/
:v/str/cmd Executes cmd on all lines that do not match str
– Yanking Text –
, Repeats, in reverse direction, last / or ? search command
yy Yank the current line
:y Yank the current line – Replace –
nyy or nY Places n lines in the buffer-copies The search and replace function is accomplished with the :s command. It is commonly
Copies everything from the curser to the Motion Command (e.g., used in combination with ranges or the :g command (below).
yMotion_cmd yG would copy from current position to the end of the file, and y4 :s/pattern/string/flags Replace pattern with string according to flags.
would copy to the end of the fourth sentence)
g Flag - Replace all occurences of pattern
"(a-z)nyy or "(a- Copies or cuts (deletes) n lines into a named buffer a through z;
z)ndd omitting n works on current line c Flag - Confirm replaces.
& Repeat last :s command
– Changing text –
– Regular Expressions –
The change command is a deletion command that leaves the editor in insert mode. It is
performed by typing c followed by a motion. For example cw changes a word. A few . (dot) Any single character except newline
other change commands are: * zero or more occurances of any character
C Change to the end of the line [...] Any single character specified in the set
cc or S Change the whole line until ESC is pressed [^...] Any single character not specified in the set
xp Switches character at cursor with following character \< Matches beginning of word
stext Substitutes text for the current character until ESC is used \> Matches end of word
cwtext Changes current word to text until ESC is used ^ Anchor - beginning of the line
Ctext Changes rest of the current line to text until ESC is used $ Anchor - end of line
Changes to text from current position to Motion \< Anchor - begining of word
cMotion_cmd
Command until ESC is used
\> Anchor - end of word
Shifts the line left or right (respectively) by one shift
<< or >> \(...\) Grouping - usually used to group conditions
width (a tab)
Shifts n lines left or right (respectively) by one shift \n Contents of nth grouping
n<< or n>>
width (a tab) Escapes the meaning of the next character (e.g., \$ allows you to search for
\
<Motion_cmd or Use with Motion Command to shift multiple lines left or $)
>Motion_cmd right \\ Escapes the \ character

– Buffers – [...] - Set Examples

Named buffers may be specified before any deletion, change, yank or put command. [A-Z] The SET from Capital A to Capital Z
The general prefix has the form "c where c is any lowercase character. for example, [a-z] The SET from lowercase a to lowercase z
"adw deletes a word into buffer a. It may thereafter be put back into text with an [0-9] The SET from 0 to 9 (All numerals)
appropriate "ap.
[./=+] The SET containing . (dot), / (slash), =, and +
– Markers – The SET from Capital A to Capital F and the dash (dashes must be
[-A-F]
specified first)
Named markers may be set on any line in a file. Any lower case letter may be a [0-9 A-Z] The SET containing all capital letters and digits and a space
marker name. Markers may also be used as limits for ranges.
[A-Z][a-zA- In the first position, the SET from Capital A to Capital Z
mc Set marker c on this line Z] In the second character position, the SET containing all letters
`c Go to beginning of marker c line. [a-z]{m} Look for m occurances of the SET from lowercase a to lowercase z
'c Go to first non-blank character of marker c line. Look for at least m occurances, but no more than n occurances of the
[a-z]{m,n}
SET from lowercase a to lowercase z
– VI Settings – – Count –
Note: Options given are default. To change them, enter type :set option to turn them on Nearly every command may be preceded by a number that specifies how many times
or :set nooptioni to turn them off.To make them execute every time you open VI, create it is to be performed. For example, 5dw will delete 5 words and 3fe will move the
a file in your HOME directory called .exrc and type the options without the colon (:) cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated
preceding the option conveniently with this method, say to insert the same line 100 times.
Set Default Description
– Ranges –
:set ai noai Turns on auto indentation
:set all -- Prints all options to the screen Ranges may precede most "colon" commands and cause them to be executed on a line
or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined
:set ap aw Prints line after d c J m :s t u commands with the :s command to perform a replacement on several lines, as with
:set aw noaw Automatic write on :n ! e# ^^ :rew ^} :tag :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
:set bf nobf Discards control characters from input :n,m Range - Lines n-m
:set dir=tmp dir = /tmp Sets tmp to directory or buffer file :. Range - Current line
:set eb noed Precedes error messages with a bell :$ Range - Last line
:set ed noed Precedes error messages with a bell :'c Range - Marker c
:set ht= ht = 8 Sets terminal hardware tabs :% Range - All lines in file
:set ic noic Ignores case when searching :g/pattern/ Range - All lines that contain pattern
:set lisp nolisp Modifies brackets for Lisp compatibility.
:set list nolist Shows tabs (^l) and end of line ($) – Shell Functions –
Allows pattern matching with special Executes shell command cmd; you can add these special characters to
:set magic magic :! cmd
characters indicate:% name of current file# name of last file edited
:set mesg mesg Allows others to send messages Executes shell command cmd, places output in file starting at current
:set nooption Turns off option !! cmd
line
:set nu nonu Shows line numbers :!! Executes last shell command
Speeds output; eliminates automatic :r! cmd Reads and inserts output from cmd
:set opt opt
RETURN :f file Renames current file to file
para = macro names that start paragraphs for :w !cmd Sends currently edited file to cmd as standard input and execute cmd
:set para=
LIlPLPPPQPbpP { and } operators
:cd dir Changes current working directory to dir
:set prompt prompt Prompts for command input with :
:sh Starts a sub-shell (CTRL-d returns to editor)
:set re nore Simulates smart terminal on dumb terminal
:so file Reads and executes commands in file (file is a shell script)
:set remap remap Accept macros within macros
Sends text from current position to Motion Command to shell
Indicates largest size of changes reported on !Motion_cmd
:set report noreport command cmd
status line
Sorts from current position to end of paragraph and replaces text with
:set ro noro Changes file type to "read only" !}sort
sorted text
:set scroll=n scroll = 11 set n lines for CTRL-d and z
:set set shell escape (default is /bin/sh) to – Files –
sh = /bin/sh
sh=shell_path shell_path
:set showmode nosm Indicates input or replace mode at bottom :w file Write to file
:set slow slow Pospone display updates during inserts :r file Read file in after line
:set sm nosm Show matching { or ( as ) or } is typed :n Go to next file
:set sw=n sw = 8 Sets shift width to n characters :p Go to previous file
Path for files checked for tags (current :e file Edit file
:set tags=x tags = /usr/lib/tags
directory included in default)
:set term $TERM Prints terminal type
!!program Replace line with output from program
:set terse noterse Shorten messages with terse
:set timeout Eliminates one-second time limit for macros
Sets significance of tags beyond n characters
:set tl=n tl = 0
(0 means all) – Other –
:set ts=n ts = 8 Sets tab stops to n for text input
~ Toggle upper and lower case
Inhibits normal checks before write
:set wa nowa J Join lines
commands
:set warn warn Warns "no write since last change" Joins the next n lines together; omitting n joins the beginning of the next line
nJ
to the end of the current line
:set window=n window = n Sets number of lines in a text window to n
. Repeat last text-changing command
Sets automatic wraparound n spaces from
:set wm=n wm = 0 Undo last change (Note: u in combination with . can allow multiple levels of
right margin. u
undo in some versions)
Sets automatic wraparound n spaces from U Undo all changes to line
:set ws ws right margin. ; Repeats last f F t or T search command
:N You can open up a new split-screen window in (n)vi and then use ^w to
or :E switch between the two.

You might also like