vimtips.txt - some useful commands Conventions CTRL - crtl key ESC - escape key INSERT - insert key DELETE - delete key BS - basckspace Operation Modes Normal - default mode when you open a file with vi or vim. Insert - use this mode to type chars into the file Replace - replace char at cursor with new char Visual - used to select and highlight text Command line (last line) - execute a function like writing the file Help - opens the default vim help file 0. Open or create a file vi filename - open file for read/write view filename - open file read-only; can override with w! 1. Keys to switch from Normal to other modes ESC - Normal mode i,INSERT - Insert at cursor pos, repeating INSERT key toggles Replace mode I - Insert at beginning of current line a - Insert at char after cursor (a for append) A - Insert at end of currnet line r - replace char with a new one; ex: ome with cursor on m; rn to make one R - Replace mode, can also get there from Insert mode using INSERT key v - Visual char mode, use to select and highlight text V - Visual line mode; use to select and highlight a line : Command line mode - command follows the colon char 2. Useful commands - for Command line mode :q - close the file and quit; complains if changes were not saved :q! - quit without saving to avoid complaint :w - write the current buffer to existing filename :w filename - write to a new filename :wq - write and quit :help - opens vim help window - :q to close it :help topic - opens help window on a topic if found :recover - recover a temp copy if vim was killed or crashed :set number - display line numbers :set nonumber - turn off line numbers :shell - open default shell, exit will return to vim :split - opens a new window - more in Split topic below 3. Move to word or line - Normal mode w - move to beginning of next word b - move back to prev word - found this myself, may not be std e - move to end of next word 0 - move to front of line $ - move to end of line H - move to top line in current screen L - move to last line in current screen M - move to middle line in current screen CTRL-f - move forward one screen CTRL-b - move back one screen G - move to end of file 1G - move to front of file 4. Delete chars in a line of text - Normal mode x - delete char cw - (change word) deletes to end of word and switches to Insert mode c$ - (change line) deletes to end of line and switches to Insert mode dw - delete word including space beyond but stays in Normal mode d$ - delete to end of line but stays in Normal mode de - delete to end of word; caution - if used on last char of a line this will move up next line and delete it's first word. Bug 5. Edit lines - Normal mode dd - delete entire line J - join current and next line o - insert new line below cursor, switch to Insert mode O - insert new line above cursor, switch to Insert mode 6. Delete chars in line of text - Insert mode delete key - deletes char at the cursor pos backspace, CTRL-h - deletes prev char only when creating a new line : 7. Undo/Redo/Repeat - Normal node u - undo last edit command, may repeat this U - undo all the edits on a line at once; may be confusing CTRL-r, :redo - reverse undo . - repeat last command, may be confusing 8. Cut/Copy and Paste text - from cursor pos in Normal mode v or V - change to Visual mode and highlight desired text d or y - delete or yank (copy) the text and store it in a buffer Cursor to insert point or create an empty line (o or O) p - paste (from Normal mode) Caution: when you copy into a buffer don't delete anything else until you paste your buffer or you may loose your text. Undo can get it back. 9. Find text - Normal mode * - find the next occurrence of current word (the cursor is on) # - find prev occurrence of current word (the cursor is on) /blah - find the next occurrence of blah ?blah - find the prev occurrence of blah n - finds next occurrence of blah from Normal mode N - finds prev occurrence Use cw to delete to end of word and switch to Insert mode. Special characters ^ $ . * [ / To use these in a search string put a \ before them /\$stupid\/ searches for the string $stupid/ 10. Replace string of chars with a new string - Command line mode :%s/high/low/gc - this will turn "hightlight" into "lowlight" % searches entire file for a match g means do more than one c asks y/n for each one found 11. Replace a complete word with a new one :%s/\/new/gc - replace an entire word with a new word % searches entire file for a match < looks for start of word delineated by whitespace > looks for end of word delineated by whitespace g means do more than one c asks y/n for each one found 12. Opening new windows - from Normal mode :split - opens a new window on the same file CTRL-ww moves between windows :q or :q! to close the window you are in :new - open empty file in a new window :new filename - open existing file in a new window You can copy text between files by cutting or copying text, moving to a new window and then using p to paste. See Cut, Copy and Paste sections above. 13. Recording keystrokes - from Normal mode qz..q - store a sequence of keystrokes (..) in a buffer called z q - starts the operation z - or some other char is the buffer name .. indicates the keys stored q - ends the sequence Note: ESC does not end the sequence, it becomes part of it @z to execute the keystrokes Example: qcddddq after qc is typed the bottom line says "recording". after q is typed recording dissappears. now @c will delete 2 lines by calling dddd Caution: the command you created is saved in /home/user/.viminfo If you want to delete it you can edit or delete this file. 14. Syntax highlighting :set hls - highlight strings found with a / search :syntax enable - changes text color for recognized file types :syntax off 15. Using a .vimrc file to extend vi behavior sample /usr/share/doc/vim/vimrc_example1 cp to /home/user/.vimrc To revert just remove .vimrc Among other things this adds syntax highligting enables the BS key to delete chars in insert mode auto-indents new lines 16. References user@host:$ vimtutor http://www.apmaths.uwo.ca/~xli/vim/misc.html