Starting VI

Command vi file vi +n file vi + file vi +/pattern file vi -r file Description start at line 1 of file start at line n of file start at last line of file start at pattern in file recover file after a system crash

Saving files and Quitting VI
Command :e file :w :w file :w! file :q :wq or ZZ :x :q! [ESC] Description edit file (save current file with :w first) save (write out) the file being edited save as file save as an existing file quit VI save the file and quit VI save the file if it has changed and quit VI quit VI without saving changes enter VI command mode

Command :r filename . u U J <Ctrl>G p P mf `f ´f "s12yy "ty} "ly1G "kd`f "kp Description copy filename after cursor repeat last command undo last command undo all commands on the line join current line with the next line display current line number paste after current line paste before current line set marker named "f" go to marker "f" go to start of line containing marker "f" copy 12 lines into buffer "s" copy text from cursor to end of paragraph into buffer "t" copy text from cursor to top of file into buffer "l" cut text from cursor up to marker "f" into buffer "k" paste buffer "k" into text

Moving the cursor
Keys pressed h l or <Space> k j or <Enter> b w ( ) { } 1G nG G <Ctrl>W <Ctrl>U <Ctrl>D <Ctrl>B <Ctrl>F :n :$ Effect left one character right one character up one line down one line left one word right one word start of sentence end of sentence start of paragraph end of paragraph top of file line n end of file first character of insertion up ½ screen down ½ screen up one screen down one screen move to line n move to last line of file

Inserting text
Keys pressed a A i I o O Text inserted after the cursor after last character on the line before the cursor before first character on the line open line below current line open line above current line

Changing and replacing text
Keys pressed cw 3cw cc 5cc Text changed or replaced word three words current line five lines

r R s S ~

current character only current character and those to its right current character current line switch between lowercase and uppercase

Deleting text
Keys pressed x 12x X dw 3dw d0 d$ dd 5dd d{ d} :1,. d :.,$ d :1,$ d D Text deleted character under cursor 12 characters character to left of cursor word three words to beginning of line to end of line current line five lines to beginning of paragraph to end of paragraph to beginning of file to end of file whole file after cursor to the end of line

Searching for text
Search /and ?and /^The /^The\> /end$ /[bB]ox n N Finds next occurrence of "and", for example, "and", "stand", "grand",… previous occurrence of "and" next line that starts with "The", for example, "The", "Then",… next line that starts with the word "The" next line that ends with "end" next occurrence of "box" or "Box" repeat the most recent search, in the same direction repeat the most recent search, in the opposite direction

Searching for and replacing text
Command :s/str1/str2/g :/str1/s//str2/g :.,$/\<file/directory/g :g/one/s//1/g Description replace all occurrences of str1 with str2 on current line change all occurrences of str1 into str2 on next line containing str1 replace all words starting with "file" by "directory" on every line from current line onward, for example, "filename" becomes "directoryname" replace every occurrence of "one" with 1, for example, "oneself" becomes "1self", "someone" becomes "some1"

Matching patterns of text
Expression . * .* \< \> \ \* ^ $ [set] [XYZ] [[:upper:]][[:lower:]] * [^set] [^XYZ[:digit:]] Matches any single character zero or more of the previous expression zero or more arbitrary characters beginning of a word end of a word quote a special character the character "*" beginning of a line end of a line one character from a set of characters one of the characters "X", "Y", or "Z" one uppercase character followed by any number of lowercase characters one character not from a set of characters any character except "X", "Y", "Z", or a numeric digit