vi/vim absolute basics [vim is usually aliased to vi in newer OS installs. If you type vim instead of vi and it isn't recognized, you are likely on an old OS. ] Opening vi ----------------- vi file open vi editor on file Insert Mode ----------------- i Insert characters before cursor [ Backspace key works in insert mode unless it's a really old configuation] Stop inserting ----------------- Esc [ Esc from insert mode in order to use any other commands ] Move cursor ----------------- h right one character l Left one character k up one line j down one line [ If configured, actual arrow keys can work in insert mode ] Editing ----------------- x Delete character Exit and Save ----------------- :wq Save ("write") file and quit :w Save file :q Quit (only works if no changes were made) :q! Quit, discarding any changes ---------------------------------------------------- ---------------------------------------------------- More Opening Methods view file open vi editor on file in read only mode vi -r file Recover file and recent edits after system crash ---------------------------------------------------- ---------------------------------------------------- More Editing ----------------- ----------------- Insert ----------------- a Insert characters after cursor I Insert text at beginning of line A Insert text at end of line o Open new line for text below cursor O Open new line for text above cursor Replace ----------------- r Replace current character with next typed characer R Continuously replace characters [until Esc] Change ----------------- cc Change current line cw Change from cursor to end of word c$ Change from cursor to end of line Deleting characters/words/lines ----------------- X Delete character to the left of the cursor dd Delete current line dw Delete from cursor to end of word d$ Delete from cursor to end of line dG Delect from current line to end of file Yank a.k.a Copy words/lines ----------------- yy Yank current line yw Yank from cursor to end of word y$ Yank from cursor to end of line [Add a number before any Change, Delete, or Yank command to execute on that number of characters, words, or lines. ] Paste ----------------- p Put last deleted text before cursor P Put last deleted text after cursor J Join line below to current line . Repeat last edit command Undo / Redo ----------------- These two are recursive, each use will undo a change until last save or redo until last edit. u Undo last command Ctrl-r Redo last edit ---------------------------------------------------- ---------------------------------------------------- More Cursor Navigation ----------------- ----------------- Word ----------------- w forward one word b backward by word Line ----------------- 0 first position of current line $ last position of current line ^ First non blank character of current line Screen ----------------- H Top line of screen M Middle line of screen L Last line of screen Ctrl-F Scroll forward one page in window Ctrl-B Scroll backward one page in window Ctrl-D Scroll down half a page in window Ctrl-U Scroll up half a page in window Ctrl-E Show one more line at bottom of page in window Ctrl-Y Show one more line at top of page in window File ----------------- Ctrl-G Display current line number G Move to last line in file ---------------------------------------------------- ---------------------------------------------------- vi command line actions ----------------- ----------------- [ These kind of actions usually start with : such as save and quit. ] [ Search functions are exempt from the : but appear on the command line. ] :n move cursor to line number n Search ----------------- /pattern Search forward for pattern ?pattern Search backward for pattern / Repeat previous search forward ? Repeat previous search backward n Repeat last search in same direction N Repeat last search in opposite direction Search and Replace All ---------------------- :1,$s/search string/replace string/g Explanation :n means move to line number n. :1,$ means from line 1 to last line. / is the forward search delimiter which is a special character. To use it in the search string you have to escape it \/ You have to escape ? and every other special character including \ itself. If all this escaping is too much, stick to alphanumerics and space to start. Second / ends the search portion and starts the replace. Third / ends the replace portion. g is a flag that says to continue searching and replacing after first instance is found. If you want to confirm each replacement, add c after the g.