BASIC EDITING COMMANDS IN vi [Documented by Mike McCarthy, 2003] ============================================================================================ TO ENTER TEXT YOU MUST BE IN "INSERT" MODE... -------------------------------------------------------------------------------------------- i begin text insertion AT THIS character postion (Starts INSERT mode) a begin text insertion AT NEXT character position (Starts INSERT mode) o start new line BELOW current line and begin insertion there (Starts INSERT mode) O start new line ABOVE current line and begin insertion there (Starts INSERT mode) esc end text insertion (Ends INSERT mode) FOR ALL OTHER COMMANDS YOU MUST BE IN "DISPLAY" MODE... -------------------------------------------------------------------------------------------- h,j,k,l MOVE left, down, up, right NOTE: backspace key can be used to move back while still in entry mode without deleting characters, this is useful to move back and overwrite characters previously entered in current INSERT mode session. w MOVE forward one word b MOVE backward one word -------------------------------------------------------------------------------------------- x DELETE current character r REPLACE current character with next character entered dd DELETE current line (and store it in buffer) Can be used with leading digits, for example: 2dd CUT 2 lines and store in buffer 8dd CUT 8 lines and store in buffer THIS COMMAND IS LIKE 'CUT' -------------------------- (Buffer storage operation may be disregarded, this command can be used to simply delete current line without subsequent pasting) yy YANK line (copy current line and store it in buffer) Can be used with leading digits, for example: 2yy COPY 2 lines into buffer 8yy COPY 8 lines into buffer THIS COMMAND IS LIKE 'COPY' --------------------------- p PASTE current buffer contents UNDER current line P PASTE current buffer contents ABOVE current line u UNDO last change -------------------------------------------------------------------------------------------- $ MOVE cursor to end of current line 0(zero) MOVE cursor to beginning of current line 1G MOVE to beginning of file Numeric prefix used to MOVE to specified line, for example: 12G MOVE to line 12 19G MOVE to line 19, etc. G MOVE to end of file d$ DELETE to end of line (including current character) dG DELETE to end of file (including current line) d84G DELETE from current line to line 84 -------------------------------------------------------------------------------------------- z. MOVE view position of current line to center screen ^F (Ctl+F) MOVE view position forward one screenful ^B (Ctl+B) MOVE view postion backward one screenful -------------------------------------------------------------------------------------------- :w SAVE current changes and continue editing :wq SAVE current changes and quit (same as ZZ) :q! Abandon all changes to document and QUIT vi :set number DISPLAY LINE NUMBERS to the left of file contents :set nonumber UNSET LINE NUMBER DISPLAY (May be abbreviated to :set nonu) :NUMBER MOVE cursor and view postion to line number NUMBER /PATTERN SEARCH document DOWNWARD from current position for character pattern PATTERN. Type 'n' to continue searching for next occurrance. ?PATTERN SEARCH document UPWARD from current position for character pattern PATTERN. Type 'n' to continue searching for next occurrance. :r FILENAME INSERT contents of FILENAME into current document below current line :1,$s/str1/str2/g SEARCH from beginning of document for str1 and replace all occurances of str1 with str2 IMMEDIATELY. :1,$s/str1/str2/gc SEARCH from beginning of document for str1 and replace str1 with str2 INTERACTIVELY. The search string str1 will be highlited, answer 'y' to do replacement, answer 'n' to bypass this occurance and move to next. :sh Leave current document intact and open a new shell. Type 'exit' to quit that process and return to open document. --------------------------------------------------------------------------------------------