York University Statistical Consulting Service S-Plus short course October 8, 1999 Summary of vi commands and Regular Expressions =============================================== Command: vi filename Two modes: command -> insert: i insert (before) current character I insert (before) current line a append (after) current character A append (after) current line o add new line after current line O add new line before current line insert -> command ESC In insert mode: Backspace to erase last character Ctrl-W to erase last word In command mode: Basic: x delete one character r replace one character s delete one character and enter insert mode (until ESC) Motions (= objects): h left one character | These 4 keys are under the l right one character | fingers of the right hand j down one line | if you shift your hand left k up one line | by one key from its normal position. | If you're lucky the arrows will work | but hjkl always works in vi. w to beginning of next word b to beginning of word (previous word if already at beginning) 0 (zero) to beginning of this line ^ to first non-blank character in this line $ to end of this line G to end of file 1G to line 1 5G to line 5 } to next paragraph ) to next sentence /re (re is a regular expression) to next occurrence of re ?re to previous occurrence of re n to repeat last search (/ of ?) N to repeat last search (/ of ?) reversing direction fc (c is any character) forward on line to next c ; repeat previous "f" tc (c is any character) forward on line to char preceeding next c % to balancing brace (one of [{(< or >)}] ) Operations on objects: Special forms: c change cc line C to end of line d delete dd line D to end of line y yank to buffer yy line Y line ! filter through command !! filter line e.g. cw changes a word dG deletes to the end of file d0 delete to beginning of line {!}fmt filter the current paragraph through the unix command fmt !!spell find mispelled words in current line (use 'u' to get the line back after seeing the mispelled words) Other commands: . repeat last action mx mark location as 'x' 'x move to location marked 'x' p put buffer after current location P put buffer before current location J join lines together (watch out when you press j after "caps lock") u undo last change U undo changes to current line ZZ write and exit ^L refresh screen "EX" commands: (all these commands are preceded by a ":") :wq write and exit :w write and continue :e filename edit filename instead of current file :e # edit last active file (note that Ctrl-^ toggles between two files) :ab jb Jonathan Brown defines an abbreviation :map ^A !}fmt^V^M creates a macro invoked by Ctrl-A :s/re/replacement/ substitute regular expression "re" with "replacement" in current line (once) :s/re/replacement/g ibid but in every occurrence on line :g/re1/s/re2/replacement/g substitute re2 with replacement in every line that contains regular expression "re2" :!ls -al executes command ls -al :l "list" line showing invisible characters Setting options: :set ai autoindent (e.g. for programming" :set noai often crucial when copying an X buffers :set sm automatically show "match" for )}>] :set nosm Initialization file: Create a ".exrc" in your home directory or in the current directory to initialize vi. Here are some example of what I have in mine. Note that ^V (Control-V) means "verbatim" and to enter a control character you preceed it with ^V in insert mode. Thus to enter what you see as "^V^M" below, you actually type "^V^V^V^M". The first pair of of ^V's enters one ^V and "^V^M" enters ^M. The ^M is a carriage return that ends the macro so it is executed. Without it, you have to hit "Enter" after pressing the macro key. Also note that you don't use ":" to preceed each command as you would have to if you were executing these commands from vi. " some lines from my .exrc file set autoindent autowrite showmatch wrapmargin=0 report=1 exrc "note that this is how you enter a comment "set shell=/usr/local/bin/tcsh set shell=/bin/sh "set tmp=/tmp map  {!}fmt map! !!bend o ab hh