Friday, October 10, 2014

Vim Commands Cheatsheet

Some great Vim commands are listed at Best of Vim Tips.

Moving the Cursor
  • h Move cursor left
  • l Move cursor right
  • k Move cursor up
  • j Move cursor down
  • nG Move cursor to line n
  • $ Move cursor to end of current line
  • 0 Move cursor to beginning of current line
  • w Move cursor forward one word
  • b Move cursor backward one ward
  • fc Move forward to c
  • Fc Move back to c
  • H Move to top of screen
  • M Move to middle of screen
  • L Move to bottom of screen
  • % Move to associated ( ), { }, [ ]
  • ( Move a sentence back
  • ) Move a sentence forward
  • { Move a paragraph back
  • } Move a paragraph forward
Save & Exit
  • :q Quit Vim (fails when changes have been made)
  • :q! Quit without saving
  • :cq Quit always, without saving
  • :w Save current file
  • :w [filename] Save current file as [filename]
  • :wq Save current file and exit
  • :wq! Save current file and exit always
  • :wq [filename] Save to [filename], exit if not editing the last
  • :wq! [filename] Save to [filename] and exit always
  • :[range] wqSave only lines in [range] and exit
  • ZZ Save current file, if modified, and exit
  • ZQ Quit current file and exit
Editing a File
  • :e Edit the current file
  • :e! Edit the current file always, discard any changes to current buffer
  • :e [filename] Edit [filename]
  • :e! [filename] Edit [filename] always, discard any changes to current buffer
  • gf Edit the file whose name is under or after the cursor
Working with multiple files
  • :e [filename] edit a file in a new buffer
  • :bnext or :bn go to the next buffer
  • :bprev or :bp go to the previous buffer
  • :bd delete a buffer (close a file)
  • :sp [filename] open a file in a new buffer and split window
  • :vsp [filename] open a file in a new buffer and vertically split window
  • Ctrl + ws split window
  • Ctrl + ww switch windows
  • Ctrl + wq quit a window
  • Ctrl + wv split window vertically
  • Ctrl + wh move cursor to next buffer (right)
  • Ctrl + wl move cursor to previous buffer (left)
Tabs
  • :tabnew [filename] or :tabn [filename] open a file in a new tab
  • Ctrl + wt move the current split window into its own tab
  • gt or :tabnext or :tabn move to the next tab
  • gT or :tabprev or :tabp move to the previous tab
  • #gt move to tab number #
  • :tabmove # move current tab to the #th position (indexed from 0)
  • :tabclose or :tabc close the current tab and all its windows
  • :tabonly or :tabo close all tabs except for the current one
Inserting Text
  • a Append text after the cursor
  • A Append text at the end of the line
  • i Insert text before the cursor
  • I Insert text before the first non-blank in the line
  • o Begin a new line below the cursor and insert text
  • O Begin a new line above the cursor and insert text
Inserting a File
  • :r [name] Insert the file [name] below the cursor
  • :r ![cmd] Execute [cmd] and insert its standard output below the cursor
Deleting Text
  • x Delete characters under and after the cursor
  • X Delete characters before the cursor
  • d Delete text that [motion] moves over
  • dw Delete word from cursor on
  • db Delete word backward
  • d$ Delete to end of line
  • dd Delete lines
  • D Delete the characters under the cursor until the end of the line
Yank (to copy)
  • yy Yank current line
  • y$ Yank to end of line from cursor
  • yw Yank from cursor to end of current word
  • nyy Yank n lines
Put (or paste)
  • p Paste below cursor
  • P Paste above cursor
  • u Undo last change
  • U Restore line
  • J Join next line down to current line
Substitute (Replace)
  • :s/pattern/string/flags Replace pattern with string according to flags in current line only.
  • :%s/pattern/string/flags Replace pattern with string according to flags in all lines.
  • g Flag - Replace all occurrences of pattern
  • :%s/\<pattern\>/string/g Change only whole words exactly matching 'pattern' to 'string'
  • c Flag - Confirm replaces. 
  • :%s/pattern/string/gc Change each pattern to string, but ask for confirmation first.
  • & Repeat last :s command
Visual commands
  • > shift text right
  • < shift text left
  • y yank (copy) marked text
  • d delete marked text
  • ~ switch case
Other Useful Commands
  • . Repeat last command
  • cw Change current word to new word
  • r Replace one character at the cursor position
  • R Begin overstrike or replace mode
  • :/ [pattern] Search forward for [pattern]
  • :? [pattern] Search backward for [pattern]
Regular Expressions
  • . Any single character except newline
  • * zero or more occurances of any character
  • [...] Any single character specified in the set
  • [^...] Any single character not specified in the set
  • ^ Anchor - beginning of the line
  • $ Anchor - end of line
  • \< Anchor - begining of word
  • \> Anchor - end of word
  • \(...\) Grouping - usually used to group conditions

No comments: