Vi uses a modal system of editing, the two modes are command mode and insert mode. The editor initially comes up on command mode.
|a||append after cursor|
|A||append at end of line|
|c||begin change operation|
|C||change to end of line|
|i||insert before cursor|
|I||insert at end of line|
|o||open a line below current line|
|O||open a line above current line|
|R||begin overwriting text|
|s||substitute a character|
|S||substitute an entire line|
As a general rule it is a good habit to get into to remain in command mode except when you are actually typing in text. Learn to automatically hit <Escape> each time you finish editing.
Note: You can configure vi to display whether you are in insert mode or command mode. From command mode (the default when vi starts) type in:
|vi||begin editing unnamed file|
|vi file1||begin editing file1|
|vi file1 file2 file3||begin editing 3 files, file1 will appear initially|
|vi *.txt||begin editing all files ending in '.txt'|
|vi -r important.file||view file 'important.file' in read-only mode|
|view important.file||same as vi -r|
Note that the following commands are to be executed from command mode
to go to the next file in the list of files you supplied.
This is especially useful when you are entering long pathnames after :e
Note: the following commands are all executable only from command mode
|:q||quit (assumes no changes made)|
|:q!||quit, discard any changes that were made|
|:wq||write the file, then quit|
|ZZ||write the file, then quit|
|:w newname||write the file to 'newname'|
One associated command is :w which writes the current file to disk, but does not exit vi. A good habit to develop is to periodically type :w during your editing session, as this prevents loss of data if the system crashes.
This is the command you will use most often. Adds new text before the character above the cursor.
This command is most useful when your cursor is at the end of a line and you wish to add more text. The i command can be used as a substitute for a (by repositioning the cursor) in all other situations, but a is a necessity for the above situation.
Most useful when your cursor is in the middle of a line and you wish to add text to the next line. Use A<Enter> to achieve this effect.
suppose you have a line of text:
Now is the thyme for all good men ...
move your cursor to the 't' in thyme and type cw, you will see:
Now is the thym$ for all good men ...
Type in 'time' and then hit <Escape>, the line will be corrected.
most useful when the number of characters you want to replace is the same as the current number of characters. For example:
The untied states of america
Although the <PageUp> and <PageDown> and arrow keys work on some systems, I would strongly discourage their use. The following commands are guaranteed to work properly on all systems.
|delete current line||dd|
|delete from cursor to end of line||D|
|delete character under cursor||x|
|delete character before cursor (backspace)||X|
Note that many of the above commands can be preceeded by a number, for example:
|show line numbers||:set nu|
|turn off line numbers||:set nonu|
|display current line number/file name||^g|
|go to line number (1 for example)||:1|
|go to last line in file||G|
|copy current line to "clipboard"||yy|
|paste contents of "clipboard" below current line||p|
|paste contents of "clipboard" above current line||P|
|copy current line, and next 4 lines to "clipboard"||5yy|
|write lines 200 -> 205 to temp.file||:200,205w temp.file|
|write lines 100 -> end of file to temp.file||:100,$w temp.file|
|write current line -> line 300 to temp.file||:.,300w temp.file|
|write current line and the next 3 lines to temp.file||:.,.+3w temp.file|
|write lines 200 -> next to last line of the file to temp.file||:200, $-1w temp.file|
|read contents of temp.file into current file||:r temp.file|
|set case insensitive search||:set ic|
|set case sensitive search||:set noic|
|search forward (down) for "hello"||/hello|
|search backward (up) for "hello"||?hello|
|search again, (same direction as original)||n|
|search again, (opposite direction as original)||N|
|search for "hello" at start of a line||/^hello|
|search for "hello" at end of a line||/hello$|
|search for "hello" or "Hello"||/[hH]ello|
|search for "int" as a word (i.e. not print or sprint)||/\<int\>|
|search for "eat" but not "beat" or "neat"||/\[^bn]eat|
|replace "dog" with "cat" (first occurrence of dog) on the current line||:s/dog/cat|
|replace "dog" with "cat" on lines 1 -> 3 (first occurrence of dog on each line) of the file||:1,3s/dog/cat|
|replace "dog" with "cat" on lines 1 -> 3 of the file, every occurrence||:1,3s/dog/cat/g|
|replace "dog" with "cat" (every occurrence) for the entire file||:1,$s/dog/cat/g|
|replace "dog" with "cat" (every occurrence) for the entire file (alternative method)||:%s/dog/cat/g|
|replace "dog" with "cat" (every occurrence) for the entire file but confirm each replacement||:%s/dog/cat/gc|
|swap two letters (i.e. cta -> cat) note that the cursor must be on the first of the two letters||xp|
|swap two lines (swaps current line with the line below it)||ddp|
delete all lines containing "dog"
delete all blank lines
mark the current line as "line a"
mark the current line as "line b"
return to the line marked "a"
write all lines between mark "a" and mark "b" to temp.file
here is line one here is line two
you change the first line to read
Here is line one
Put your cursor on the "here" of "here is line two" and type ., the same change will be repeated.
The dot command is extremely powerful. It can be used to "do it again" with a surprising number of vi editing commands
rats live on no evil star
changing the line to:
bats live on no evil star
then you move your cursor to another line reading:
rats, rats, rats!
when you type & you'll see
bats, rats, rats!
pressing & twice more will convert "rats" to "bats" for the entire line
i am shouting
placing your cursor on the initial 'i' and holding down the ~ key results in:
I AM SHOUTING
I think therefore I am
you wish to change "am" into "spam"
which puts your cursor directly on the 'a' in 'am'
Note that F searches to the left (f searches to the right). Also ; searches for subsequent matching characters (next occurrence) and , searches for the next match in the opposite direction of the initial search.
To solve this problem, save your text in a "named" buffer.
copies 10 lines into the named buffer "a" then puts the contents of buffer "a" into the newly opened temp.file.
the only real difference between using yy or dd with named buffers is you preceed the command with " then the buffer name (a-z)
Note that to use "special" keys (<Enter> and <Esc>) for example in a macro, you must type ^v before the special character.
map '>' to "indent the current line by 3 spaces"
:map > :s/^/ /^M
Note that the ^M is formed by typing: ^v<Enter>
map 'q' to make a copy of the current line and append it to itself
:map q yyPJ
To turn off a mapping we created:
To list all the current mappings:
:ab zgs Zeh Graphics Systems, Inc.
then while editing you type:
I work for zgs<Esc>
The line expands to:
I work for Zeh Graphics Systems, Inc.
To turn off the abbreviation we just created:
To list all current abbreviations
|Letters:||g, K, q, V, v|
|Control keys:||^a, ^k, ^o, ^t, ^w, ^x|
|Symbols:||_, *, \, =|
Located in your home directory ($HOME). This file allows you to customize your vi environment, and preserve your settings between sessions.
Note that although the set and map commands are preceeded by a : when we enter them directly in vi, they have not starting : in the .exrc file. Here is an example .exrc file
""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Note that a " at the start of a line is a comment in " the .exrc file """"""""""""""""""""""""""""""""""""""""""""""""""""""""" " Map [>] to [Indent current line by 3 spaces] map > :s/^/ /^M " Map [<] to [Delete all spaces / tabs from the beginning " of the current line] map < :s/^[ ]*//^M " enable mode (input/command) display set showmode " default to ignore case for all searching set ic