<<

“Cheat” Sheet ACNS Bulletin –03 February 1995 vi Editor “Cheat Sheet”

Invoking vi: vi filename of vi commands: [count][] (count repeats the effect of the command)

Command versus input mode management commands

Vi starts in command mode. The positioning commands : name edit buffer to file name operate only while vi is in command mode. You switch vi :wq Write to file and quit to input mode by entering any one of several vi input com- :! Quit without saving changes mands. (See next section.) Once in input mode, any charac- ZZ Same as :wq ter you is taken to be text and is added to the file. You :sh Execute commands (d) cannot execute any commands until you input mode. To exit input mode, press the escape (Esc) key. Window motions Input commands (end with Esc) d Scroll down (half a screen) u Scroll up (half a screen) a Append after cursor f Page forward i Insert before cursor b Page backward Open line below /string Search forward O Open line above ?string Search backward : file Insert file after current line l Redraw screen Any of these commands leaves vi in input mode until you g Display current line number and press Esc. Pressing the RETURN key will not take you out file information of input mode. Repeat search N Repeat search reverse Change commands (Input mode) Go to last line nG Go to line n cw Change word (Esc) :n Go to line n cc Change line (Esc) - blanks line Reposition window: cursor $ Change to end of line z. Reposition window: cursor in middle rc character with c z- Reposition window: cursor at bottom R Replace (Esc) - typeover Substitute (Esc) - 1 char with string S Substitute (Esc) - Rest of line with Cursor motions text Upper left corner (home) . Repeat last change Middle line Lower left corner Changes during insert mode h Back a character h Back one character Down a line w Back one word Up a line u Back to beginning of insert ^ Beginning of line $ End of line l Forward a character w One word forward Back one word c ; Repeat find (find next c) Deletion commands text from file old to file new or ndd Delete n lines to general buffer vi old dw Delete word to general buffer “a10yy yank 10 lines to buffer a dnw Delete n words :w write work buffer d) Delete to end of sentence : new edit new file db Delete previous word “ap put text from a after cursor D Delete to end of line :30,60w new Write lines 30 to 60 in file new Delete character Regular expressions (search ) Recovering deletions ^ Matches beginning of line p Put general buffer after cursor $ Matches end of line P Put general buffer before cursor . Matches any single character * Matches any previous character .* Matches any character Undo commands u Undo last change Search and replace commands U Undo all changes on line

Syntax: Rearrangement commands :[address]s/old_text/new_text/ yy or Yank () line to general buffer “z6yy Yank 6 lines to buffer z Address components: yw Yank word to general buffer . Current line “a9dd Delete 9 lines to buffer a n Line number n “A9dd Delete 9 lines; Append to buffer a .+m Current line plus m lines “ap Put text from buffer a after cursor $ Last line p Put general buffer after cursor /string/ A line that contains "string" P Put general buffer before cursor % Entire file J lines [addr1],[addr2] Specifies a range

Examples: Parameters The following example replaces only the first occur- :set list Show invisible characters rence of Banana with Kumquat in each of 11 lines :set nolist Don’ show invisible characters starting with the current line (.) and continuing for the 10 that follow (.+10). :set number Show line numbers :set nonumber Don’t show line numbers :.,.+10s/Banana/Kumquat

:set autoindent Indent after carriage return The following example replaces every occurrence :set noautoindent Turn off autoindent (caused by the g at the end of the command) of :set showmatch Show matching sets of apple with pear. parentheses as they are typed :%s/apple/pear/g :set noshowmatch Turn off showmatch :set showmode Display mode on last line of screen The following example removes the last character from :set noshowmode Turn off showmode every line in the file. Use it if every line in the file ends ^M :set all Show values of all possible with as the result of a file transfer. Execute it parameters when the cursor is on the first line of the file. :%s/.$//