UNIX VI Commands Cheat Sheets

www.techrepublic.com

VI Commands Cheat Sheets
Before doing anything to a document, type the following command followed by a carriage return: :set showmode GOOD PRACTICE NOTE ESPECIALLY FOR BEGINNERS: WHEN USING VI, HIT [ESC] TWICE BEFORE EVERY NEW COMMAND. THIS MAKES SURE YOU AREN’T IN THE WRONG MODE. When you open VI, you can’t just start typing a thesis because VI has different modes such as APPEND MODE, INSERT MODE, REPLACE MODE, and COMMAND MODE. So to do certain actions, you must first enter the appropriate mode. See “MODES” for more in-depth information. The final page of this document is a list of VI core commands. These are the bare essentials of VI compressed onto one page.

STARTING VI
Command vi filename vi -r filename vi + n filename vi + filename vi filename file2 ... vi +/string file

(page 1/1 total) (VI is CaSe SEnsItiVe!!! So make sure Caps Lock is OFF.) Result Edits filename. Edits last saved version of filename after a crash. Edits filename and places the cursor at line n. Edits filename and places cursor on the last line. Edits filename and then edits file2 and so on. After saving file1 enter :n for the next file. Edits file and places cursor at the first line containing string.

ENDING VI
Command ZZ or :wq or :x :w :w file :w! file :n,mw file :n,mw>>file :q :q! Q :e! :we!

(1/1) (Saving, exiting, etc.) Result Saves and exits VI. Saves current file but does not exit. Saves current as file but does not exit. Saves file overriding normal checking. Saves lines n through m to a file named file. Appends lines n through m to the end of a file named file. Quits VI, saving changes before leaving. (You may be prompted to save.) Quits VI without saving changes. Escapes VI into ex editor with the same file; :vi returns. Re-edits current file disregarding changes since last save. Re-edits current file saving changes.

©2001 TechRepublic, Inc. www.techrepublic.com. All rights reserved.

www.= := CTRL-g :l (letter “l”) (1/1) (Line numbers.UNIX VI Commands Cheat Sheets STATUS :. Moves to the first column in the current line. and % of file location. etc. Moves to the end of the next word in the line. All rights reserved. Moves to the first non-blank character in the previous line. total lines in file. down. W ignores punctuation.) MOVING THE CURSOR MOVING Command (ESC exits all modes except the initial Command Mode. E ignores punctuation.techrepublic. (1/2) (These will tell you how to get the cursor where you want it–fast. respectively. Moves to the first character in the next line.com Command Shows filename. ©2001 TechRepublic. and right. Moves back to the beginning of sentence. up. Moves backward ½ screenful. Moves to the middle of the screen. Moves to the column n in the current line. . Moves to the first non-blank character in the current line.techrepublic. Moves forward to the beginning of the next paragraph. Moves forward ½ screenful. Inc. Moves to the end of file. current line number.com. but they may be too slow on big files. Result Moves to the start of the next word in the line. Moves to the start of the previous word in the line.) Result Shows current line number. Moves back to the beginning of the paragraph. Moves to line n from the top of the screen. Moves backward one entire screenful. Displays tab (^l) backslash (\) backspace (^H) newline ($) bell (^G) formfeed (^L^) of current line. Moves to the top of the screen. Moves to the bottom of the screen. Left. Moves forward to the beginning of the next sentence. Moves to the last character in the current line.) arrow keys hjkl CTRL-d CTRL-f CTRL-u CTRL-b nG G H nH M L nL w or W b or B e or E 0 (zero) or | n| ^ $ + or <CR> ( ) { } These do work. B ignores punctuation. Moves to line n in the file. Moves forward one entire screenful. Moves to line n from the bottom of the screen. Shows number of lines in file. www.

nz. Moves screen up one page. insert mode—Same as (i) except it will insert text before the first nonblank character on the line until ESC is used. for inserting characters like ESC and CTRL) until ESC is used. z– nz– Moves screen up one line. While inserting. All rights reserved. Moves screen down one page.. www.) CTRL–e CTRL–y CTRL–u CTRL–d CTRL–b CTRL–f CTRL–I z <CR> nz <CR> z.techrepublic. Moves screen up ½ page. z–carriage return makes the current line the top line on the page. Moves screen down one line.) MOVING THE SCREEN Command (ESC exits all modes except the initial Command Mode.com. replace mode—Replaces the currently highlighted character with char until ESC is used. Inc. ©2001 TechRepublic. Reads file and inserts it after line number n. replace mode—Same as (r) except it will overwrite until ESC is used. Redraws screen. Makes the current line the middle line on the page. Makes the current line the bottom line on the page. . append mode—Allows you to insert text after the letter your cursor currently highlights until ESC is used. Moves screen down ½ page. Makes the line n the top line on the page.) Result insert mode—You can start typing and it will insert text before the letter your cursor currently highlights until ESC is used. Makes the line n the middle line on the page.g.techrepublic. Makes the line n the bottom line on the page. Opens new line above the current line and insert until ESC is used. Opens new line below the current line and insert until ESC is used. (1/1) (Adding and/or replacing text) Result MODES itext Itext atext Atext rchar Rtext o O (letter “O”) CTRL–v char :r file :nr file ESC ESC ESC ESC ESC ESC Command (ESC exits all modes except the initial Command Mode. Reads file and inserts it after current line. append mode—Same as (a) except it will insert text at the end of the line until ESC is used.com (2/2) (These will tell you how to get the cursor where you want it–fast. ignores special meaning of char (e.UNIX VI Commands Cheat Sheets MOVING www.

g. repeat command. While inserting. Searches forward in current line to character before char. Repeats last :s command. Searches forward in current line to char. Finds the line containing str1. Searches backward in current line to character before char. Finds forward to str. Matches end of line.UNIX VI Commands Cheat Sheets SEARCHING Characters ^ (caret) $ . Matches zero or more occurrences of previous character in expression. Finds in reverse for str. ©2001 TechRepublic. undo command) Result Command (ESC exits all modes except the initial Command Mode. omitting n deletes current character only.. Inc. Matches any single character in str. replaces str1 to str2.com What they match.techrepublic. Matches any single character.com. and p to print changed lines. EDITING CTRL–h or Backspace CTRL–w CTRL–x nx (1/3) (Deleting. www. Ignores case when searching.. All rights reserved.. Matches end of word. Pays attention to case when searching.ms/str1/str2/opt & :g/str/cmd :g/str1/s/str2/str3/ :v/str/cmd Searches to beginning of balancing ( ) [ ] or { }. <CR> to suppress). copying. Runs cmd on all lines that contain str.) % fchar Fchar tchar Tchar /str <CR> ?str <CR> :set ic :set noic :n. Escapes the \ character. Executes cmd on all lines that do not match str. Matches any character between a and n. Result Command (ESC exits all modes except the initial Command Mode. (1/1) (Find and/or replace what you need) www. Deletes n characters starting with current. c to confirm change (y to acknowledge. changing text. Searches backward in current line to char.techrepublic. placing text. deletes previous character. Searches from n to m for str1. Matches any character not in str. replaces str2 with str3. \$ allows you to search for $). \< \> [str] [^str] [a-n] * \ \\ Matches beginning of line. joining lines. deletes to start of inserted text. Matches beginning of word. deletes previous word While inserting. using opt–opt can be g for global change. Escapes the meaning of the next character (e. .) While inserting.

Copies or cuts (deletes) n lines into a named buffer a through z. and d4 would delete to the end of the fourth sentence). (period) CTRL–i or TAB ©2001 TechRepublic. Deletes lines n through m. Pastes copied text after cursor. While inserting. n N . repeat command. S or cc text ESC cwtext Ctext ESC ESC ccursr_cmdtext ESC u U “np “1pu. Joins the next n lines together. Copies from cursor to cursor_cmd (e. Pastes copied text before the cursor.techrepublic. (comma) . Repeats.com. changing text.techrepublic. Switches character at cursor with following character. inserts one shift width.u. also prints last deleted text. Restores current line to its original state. Changes to text from current position to cursr_cmd until ESC is used. last / or ? search command. in reverse order. joining lines. (semi-colon) . omitting n deletes the current word only.. Deletes from the cursor to the end of the line.UNIX VI Commands Cheat Sheets EDITING (2/3) nX xp ndw ndb ndd :n.). omitting n joins the beginning of the next line to the end of the current line. Places n lines in the buffer–copies. . dG would delete from current position to the end of the file. copying. Repeats last / or ? search command. Undoes last command.g.com (Deleting. Changes current word to text until ESC is used. omitting n deletes previous character only. Inc. omitting n copies only the current line to the buffer. www. Scrolls through the delete buffer until the desired delete is retrieved (repeat u. Deletes the previous n words starting with current. Pastes text from a named buffer a through z after or before the current line. in reverse direction. Deletes everything included in the cursor command (e. Repeats. undo command) Result Command (ESC exits all modes except the initial Command Mode. omitting n deletes the current line only. yG copies current line to the last line in the file to the buffer).md D or d$ dcursor_cmd nyy or nY ycursor_cmd “(a-z)nyy “(a-z)ndd p P “(a-z)p or “(a-z)P nJ stext ESC or www. omitting n works on current line. Substitutes text for the entire line until ESC is used. Retrieves the last nth delete (last 9 deletes are kept in a buffer). Substitutes text for the current character until ESC is used. also prints last deleted text. Deletes the next n words starting with current.g. Repeats last text change command. All rights reserved. omitting n deletes the previous word only. Deletes n lines beginning with the current line. Changes rest of the current line to text until ESC is used.) Deletes previous n characters. placing text. Repeats last f F t or T search command.. last / or ? search command.

repeat command. Starts a sub-shell (CTRL-d returns to editor). A.techrepublic. SUBS. Executes last shell command. Reads and executes commands in file (file is a shell script).com. represents the current line.) :[address]s/search-string/replace-string/[/g] address search-string replace-string g Result This is the format of a substitute command––description below. SHELL WORKS :! cmd (1/1) (Commands that execute outside of the VI program. Renames current file to file. Sends currently edited file to cmd as standard input and execute cmd. copying. changing text. One line number or two line numbers separated by a comma. . Sends text from current position to cursor_cmd to shell command cmd. places output in file starting at current line. ABBREVIATIONS (1/2) (Macro functions. The replacement string. you can add these special characters to indicate: % name of current file # name of last file edited Executes shell command cmd. !! cmd :!! :r! cmd :f file :w !cmd :cd dir :sh :so file !cursor_cmd cmd !}sort <CR> MACROS. Indicates a global replacement (more than one replacement per line). Inc.) Result Command (ESC exits all modes except the initial Command Mode) Executes shell command cmd. placing text. and abbreviations) Command (ESC exits all modes except the initial Command Mode. ©2001 TechRepublic. substitution strings.UNIX VI Commands Cheat Sheets EDITING (3/3) n<< or n>> < or > www. Changes current working directory to dir. Sorts from current position to end of paragraph and replaces text with sorted text. omitting n shifts one line. All rights reserved. Use with cursor command to shift multiple lines left or right. www.com (Deleting. Reads and inserts output from cmd. joining lines. undo command) Result Command (ESC exits all modes except the initial Command Mode) Shifts n lines left or right (respectively) by one shift width. and % represents the entire file. $ represents the last line.techrepublic. A regular expression that can be a simple string of characters.

Changes file type to “read only. and insert “You” (iYou ESC). Command :set all :set ai :set bf :set ic :set lisp :set mesg :set nu :set prompt :set report :set scroll=n :set showmode :set term :set timeout :set ts=n :set warn :set wm=n Result Prints all options to the screen.techrepublic.com. Shows tabs (^l) and end of line ($). Discards control characters from input. Sets tab stops to n for text input. MACROS. If you create a file called “. delete word (dw). Command :set nooption :set ap :set eb :set dir=tmp :set magic :set list :set opt :set re :set ro :set sh=shell_path :set sw=n :set terse :set tl=n :set wa :set window=n Turns off option. Speeds output.” set shell escape (default is /bin/sh) Sets shift width to n characters. CTRL–v allows ESC to be inserted. ©2001 TechRepublic. Shows line numbers. To make them execute every time you open VI.UNIX VI Commands Cheat Sheets www. Sets number of lines in a text window to n. Inc. Precedes error messages with a bell. set n lines for CTRL–d and z Indicates input or replace mode at bottom. precede them with a \ (backslash). Ignores case when searching. Inhibits normal checks before write commands. If you need to include quotes (“). Indicates largest size of changes reported on status line. Eliminates one-second time limit for macros. and abbreviations) Setting Options (2/2) Command :unmap key :ab Result Removes macro definition for key.” Sets automatic wraparound n spaces from right margin. Warns “no write since last change. SUBS. Sets significance of tags beyond n characters (0 means all). enter type :set option to turn them on or :set nooption to turn them off. . replaces it with string. Prints terminal type. Modifies brackets for Lisp compatibility. All rights reserved.exrc and type the options without the colon (:) preceding the option. Sets directory or buffer file. Command :ab str string :una str Result When str is input. (2/2) :map Displays all created macros on status line. :map key cmd_seq Defines key to run cmd_seq when pressed. EX: :map v /I CTRL–v ESC dwiYou CTRL–v ESC ESC when v is pressed. Shorten messages with terse. Prompts for command input with :. Options given are default. Allows others to send messages. Unused keys in vi are: K V g q v * = and the function keys. To imbed control characters like ESC in the macro.com Setting Options (1/2) Command Result Command Result NOTE: Map allows you to define strings of VI commands. any map or set command you place inside this file will be executed every time you run VI. Allows pattern matching with special characters. Simulates smart terminal on dumb terminal.exrc” in your home directory. ABBREVIATIONS (Macro functions. create a file in your HOME directory called . Turns on auto indentation. you need to precede them with CTRL–v. eliminates automatic RETURN. Result Prints line after d c J m :s t u commands. Unabbreviates str. Displays all abbreviations.techrepublic. To change them. www. substitution strings. search for “I” (/I ESC).

omitting n copies only the current line to the buffer. w or W Moves to the start of the next word in the line. Options given are default.techrepublic. MOVING (These will tell you how to get the cursor where you want it–fast. U Restores current line to its original state. and right. H Moves to the top of the screen. ENDING VI OPTIONS Command :set all :set ic :set nu :set scroll=n :set showmode :set ts=n Result Prints all options to the screen. Command :set nooption :set magic :set ro :set sw=n :set window=n :set wm=n Result Turns off option. SEARCHING (Find and/or replace what you need) fchar | Fchar Searches forward in current line to char.com. CTRL-b Moves backward 1 entire screenful.) ZZ or :wq or :x Saves and exits VI. undo command) While inserting. e or E Moves to the end of the next word in the line. To change them. Ignores case when searching. Sets automatic wraparound n spaces from right margin. . but they may be too slow on big files. in reverse order. | Finds in reverse for str. Inc. Rtext ESC replace mode—Same as (r) except it will overwrite until ESC is used. ndd Deletes n lines beginning with the current line. up. + or <CR> Moves to the first character in the next line. D or d$ Deletes from the cursor to the end of the line. $ Moves to the last character in the current line. atext ESC append mode—This allows you to insert text after the letter your cursor currently highlights until ESC is used. exiting. inserts one shift width. last / or ? search command. :q Quits VI. CTRL-u Moves backward ½ screenful. /str <CR> | ?str <CR> Finds forward to str. hjkl Left. Changes file type to “read only. joining lines.) :q! Quits VI without saving changes. omitting n joins the beginning of the next line to the end of the current line. To make them execute every time you open VI. Allows pattern matching with special characters.” Sets shift width to n characters. (period) Repeats last text change command. rchar ESC replace mode—Replaces the currently one highlighted character with char until ESC is used. also prints last deleted text. changing text. enter type :set option to turn them on or :set nooption to turn them off. nJ Joins the next n lines together. ^ Moves to the first non-blank character in the current line. respectively. L Moves to the bottom of the screen. saving changes before leaving. deletes previous word nx Deletes n characters starting with current. W ignores punctuation. Indicates input or replace mode at bottom. omitting n deletes the current word only.com VI Core Commands (The minimum functions) COMMAND STARTING VI (VI is CaSe SEnsItiVe!!! So make sure Caps Lock is OFF.) vi file RESULT edit file (Saving.UNIX VI Commands Cheat Sheets www. (You may be prompted to save. E ignores punctuation. All rights reserved. omitting n deletes current character only. create a file in your home directory called . EDITING (Deleting. Sets tab stops to n for text input.techrepublic. MODES (Adding and/or replacing text) itext ESC insert mode—You can start typing. . M Moves to the middle of the screen. b or B Moves to the start of the previous word in the line. B ignores punctuation. and it will insert text before the letter your cursor currently highlights until ESC is used. repeat command. nyy or nY Places n lines in the buffer–copies. deletes previous character. copying. placing text. Sets n lines for CTRL–d and z. ©2001 TechRepublic. u Undoes last command. | Repeats. Sets number of lines in a text window to n.exrc and type the options without the colon (:) preceding the option. | Searches backward in current line to char. CTRL–h or Backspace CTRL–w While inserting. ndw Deletes the next n words starting with current.) MOVING THE CURSOR arrow keys These do work. o Opens new line below the current line and insert until ESC is used. CTRL–i or TAB While inserting. down. Shows line numbers. Atext ESC append mode—Same as (a) except it will insert text at the end of the line until ESC is used. nG Moves to line n in the file. CTRL-d Moves forward ½ screenful. omitting n deletes the current line only. G Moves to the end of file. p Pastes copied text after cursor. O (letter “O”) Opens new line above the current line and insert until ESC is used. CTRL-f Moves forward 1 whole screenful. etc. n | N Repeats last / or ? search command. www.