Wow! I Didn't Know You Could Do That In vi compiled by: Patricia Bender ESC = the escape key RET = the return key char = any lowercase character 8 = any number num = any number ^char = control character (hold the control key while enter character) return must be hit after : commands STARTING vi open a vi session vi filename open filename, either a new or existing file vi filename1 filename2 filename3 open filename1 for editing, then use :n when done with filename1 to access filename2 (see SAVING, WRITING, MANIPULATING, OPENING, AND EXITING FILES for more commands) view filename open filename in a vi session, but for viewing only - can not change the file and save INSERTION (must hit ESC after done entering text) a append after cursor location A append after end of line i insert at cursor I insert at beginning of line o open new line below cursor O open new line above cursor HIGHLY USEFUL MISC COMMANDS . repeat last command u undo last command ^L refresh screen ^G display line number CHANGE, MOVING, AND DELETION (* means must hit ESC after done entering text) (you can type a number first to do numerous deletions/changes for some of these commands) r change character below cursor R *replace text (overwrite) s *substitute text for character ~ change the case of the character cw *change word (word defined as whitespace, non-alphanumeric, alphanumeric) cW *change word (word defined as from cursor to next whitespace) cb *change word from cursor to start of word cB * " " " " cc *change current line c) *change from cursor to end of sentence (sentenced defined as ending with . ? or ! and two whitespaces or one empty line) cfchar *change from cursor to first occurrence of character c8RET *change this and next 8 lines C *change all characters from cursor to end of line c/pattern *change from cursor until 1st occurrence of pattern :m 30 move current line to line 30 :1,20 m 30 move lines 1 through 20 to below line 30 x delete character below cursor X delete character just prior to cursor dd delete line D delete from cursor to end of line dG delete all lines from cursor to end of file d8G delete all lines from cursor to line 8 d) delete from cursor to end of sentence d/pattern delete from cursor until 1st occurrence of pattern d?pattern delete from cursor until last occurrence of pattern xp swap characters dwwP swap words ddp swap lines :d 8 delete current line and next 7 lines :1,20 d delete lines 1 through 20 :.-2 d 8 start 2 lines above cursor and delete 8 lines :set shiftwidth=8 sets the shiftwidth 8>> shift this and next 7 lines to the right shiftwidth spaces 8<< shift this and next 7 lines to the left shiftwidth characters J join next line to this line 8J joins next 8 lines :j! retain leading whitespace when joining lines :j! 8 joins next 8 lines MOVING THE CURSOR (you can type a number first to do numerous moves for most of these commands) 0 move cursor to start of the current line $ move cursor to end of the current line j or RET down to the next line k or - up to the previous line l or SPACE to the left one character h or BACKSPACE to the right one character w or W forward to the next word e or E forward to the end of the word b or B back to the start of the last word H move cursor to the top of the screen M move cursor to the middle of the screen L move cursor to the bottom of the screen fchar forward to the next occurrence of character ) move cursor to the start of the next sentence ( move cursor to the start of the last sentence 8H move cursor to 8th line from the top of the screen 8L move cursor to 8th line from the bottom of the screen G go to the end of the file 1G go to the start of the file numG go to line num ^F forward 1 screen ^B back 1 screen ^U back 1/2 screen ^D forward 1/2 screen zRET scroll cursor line to the top of the screen z. scroll cursor line to the middle of the screen z- scroll cursor line to the bottom of the screen ^E scroll screen up one line ^Y scroll screen down one line SEARCH AND REPLACE /pattern locate next occurrence of pattern ?pattern locate last occurrence of pattern n find next occurrence of pattern (after using / or ?) N search for pattern in opposite direction (after using / or ?) :g/pattern/p global search for all occurrences of pattern, like grep :line1,line2g/pattern/p grep search from line1 to line2 for all occurrences of pattern :line1,line2s/searchpattern/replacepattern/gRET search from line1 to line2 for searchpattern and replace all occurrences with replacepattern. The /g at the end causes multiple occurrences of searchpattern on a line to be replaced (without the /g it would only replace the first occurrence of searchpattern on each line). :%s/temp/temp2/gRET searches and replaces the whole file :.,$s/temp/temp2/gRET searches and replaces from the current line to the end of file :.-8,.+8s/temp/temp2/gRET searches and replaces from 8 lines above cursor to 8 lines below cursor SPECIFYING PATTERNS ^ Matches the beginning of a line $ Matches the end of a line . Matches any single character \< Matches the beginning of a word \> Matches the end of a word * Matches 0 or more occurences of the preceding character [chars] Matches any characters in the enclosed set [^chars] Matches any characters not in the enclosed set \char Takes char as char, not its meta character meaning For example: :%s/^/ / inserts two spaces at the start of each line /stop$ matches only lines ending with "stop" /\<word\> matches only "word", not "words", "wording", ... /f[io]rm matches "firm" or "form" /\/\. matches "\." MARKERS mchar set marker (characters a-z) `char go to marker 'char go to the start of the line containing marker >'char shift all lines from cursor to marker <'char " " " " `ad`b delete from marker a up to (but not including) marker b 'ad'b delete all lines from marker a through marker b `ad'b delete from marker a through marker b's line COPYING, YANKING, PUTTING, AND BUFFERS :co 30 copy current line and place below line 30 :1,20 co 30 copy lines 1 through 20 and place below line 30 :.-2,10 co 30 copy 2 lines above current line through line 10 and place below line 30 yw yank word into default buffer y8w yank 8 words into default buffer Y yank line into default buffer 8Y yank 8 lines into default buffer y/pattern yanks from cursor up to (but not including) pattern :1,20 y yank lines 1 through 20 p put contents of default buffer after/below cursor P put contents of default buffer before/above cursor There are 63 buffers: a-z, A-Z, and 1-9, stuff saved to buffers remain as long as vi is up (thus you can switch stuff between files with buffers) "aY yank line into buffer a "a6dd deletes 6 lines and puts them into buffer a "AdG deletes all text from cursor to end of file and appends it to buffer A (capital character instead of lowercase) "ap put contents of buffer a after/below cursor :1,20 y a yank lines 1 through 20 into buffer a SAVING, WRITING, MANIPULATING, OPENING, AND EXITING FILES :f newfilename change the name of the current file being edited :w writes the file without exiting :w filename writes the text into filename :w! filename force write to filename :10,50w filename write lines 10 through 50 of current file to filename :.,$w! filename force write current line through end of file to filename :.+2,'aw filename write two lines down from current line through marker a to filename :/pattern1/,/pattern2/w filename write text from pattern1 to pattern2 to filename :w >>filename append current file to filename :2,5w >>filename append lines 2 through 5 to filename :r filename reads filename into current file below the cursor line :r !command reads the results of the shell command into the current file below the cursor (ie, :r !cat temp1 temp2) :g/pattern/r filename locates pattern and reads filename into current file below the lines containing pattern :e filename aborts the opened file and opens filename :n go to the next open file :rewind closes the current file and reopens the first file ^~ switch between files :q quits out of vi (won't let you if changes have been made) :q! force quits out of vi without saving :wq writes file and quits out of vi :x writes file and quits SPELL CHECKING within vi :w !spell > spell.errors spell check the file and write the misspelled words into spell.errors :r spell.errors read in the misspelled words for easy viewing (might want to G first - write at end of file) OTHER COMMANDS ^Vcontrolchar writes the control character (doesn't interpret it) :%l show all control characters :!command run an external UNIX shell command :!csh spawn a C shell :!ls list the current directory :!tabsRET correct strange screen behavior :cd directory change directory (:cd! dir force changes directory) :set number display line numbers in left margin :set autowrite write buffers automatically if user uses a next, rewind, tag, or shell command :set wrapmargin=8 line wrap at -8 characters from screen width (also :set wm=8 ) :set autoindent for programming, lines up next line with previous line (also :set ai ) :set showmatch points out matching ( or { when closing ) or } is entered (also :set sm ) :set nowrapscan does not wrap around to start of file for searches (also :set nows ) .exrc the .exrc file contains personal commands to be set for using vi set wrapmargin=8 set showmatch set term=vt100 set scroll=8 set shiftwidth=8