Skip to content

Instantly share code, notes, and snippets.

@adeleinr
Forked from actaneon/VimCommands.txt
Created April 16, 2010 05:55
Show Gist options
  • Save adeleinr/368080 to your computer and use it in GitHub Desktop.
Save adeleinr/368080 to your computer and use it in GitHub Desktop.
Vim Cheatsheet
http://vim.wikia.com/wiki/
GREP STUFF
==========
The :substitute command searches for a text pattern, and replaces it with a text string. There are many options, but these are what you probably want:
:%s/foo/bar/g
Find each occurrence of 'foo', and replace it with 'bar'.
:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first.
:%s/\<foo\>/bar/gc
Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.
REPLACE TABS WTIH SPACES
========================
To insert space characters whenever the tab key is pressed
:set expandtab
number of space characters that will be inserted when the tab key is pressed
:set tabstop=4
To change all the existing tab characters to match the current tab settings
:retab
Multiple Windows
CTRL + w, s - Split Horizontally, reopen the same file in the new window.
CTRL + w, v - Split Vertically
CTRL + w, n - Opens a Blank Window
CTRL + w, c - Close active window
CTRL + w, o - Close all windows except the active one.
CTRL + w, f - Open file under cursor
CTRL + w, UP/DOWN/RIGHT/LEFT will move focus to corresponding window.
CTRL + w, h/j/k/l will move focus to corresponding window.
CTRL + w, w will move between the windows, in order.
ctrl + w, _ Maximise current window
ctrl + w, = Gives the same size to all windows
Exiting/Saving
:wa - write all.
:wq - Save changes and Exit.
Delete Ranges
:21,25d delete lines 21 to 25 inclusive
:$d delete the last line
:1,.-1d delete all lines before the current line
:.+1,$d delete all lines after the current line
:21,25t 30 copy lines 21 to 25 inclusive to just after line 30
:$t 0 copy the last line to before the first line
:21,25m 30 move lines 21 to 25 inclusive to just after line 30
:$m 0 move the last line to before the first line
Shell
:!pwd Execute the pwd unix command, then returns to Vi
!!pwd Execute the pwd unix command and insert output in file
File Browsing
:Ex - Ex(plorer) mode
Diff
do - Get changes from other window into the current window.
dp - Put the changes from current window into the other window.
]c - Jump to the next change.
[c - Jump to the previous change.
Ctrl W + Ctrl W - Switch to the other split window.
:diffupdate - diff update
:syntax off - syntax off
:diffget 3 If you were in buffer 1, writing this command would obtain the difference in buffer 3
:diffput 1 Again, write this in buffer 3 and it will place the change in buffer 1
Folding
zf#j creates a fold from the cursor down # lines.
zf/string creates a fold from the cursor to string .
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zc close folded text
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.
:set foldmethod=indent
:set foldmethod=marker
:set foldmarker=nnn,mmm
Match Pairs - %
% is used to match paired characters.
If the cursor is at a starting brace, pressing % will take cursor to the matching closing brace, and vice versa.
This works for () (parantheses), /**/ (block comments) etc.
This is handy, can be used extensively with other commands like d (cut), y (copy) etc.
If you need to delete an entire function block, place cursor at either one of the braces and d% will delete that full block.
Autocomplete
CTRL + n and CTRL + p will open the autocompletion box. You can use UP and DOWN to select one option and ENTER to use that word.
VIM searches the current file for matching completions. If you have tags (will come to that shortly), then VIM will search the whole tags for matches
Keyword Program
SHIFT + k - if pressed within a word, it will execute a command (which can be specified), and passes the word as argument.
Default is man. So, if you press SHIFT + k on a function, it will directly go to its man page, very useful during programming.
Settings - Prefix with no to disable
ai - Auto indent
Case
Vu Lowercase line
VU Uppercase line
vEU Switch word to uppercase
vEu Switch word to lowercase
vE~ Modify word case
ggguG Set all text to lowercase
Search
/word Search word from top to bottom
?word Search word from bottom to top
gd gD local, global definition of symbol under cursor
Replace
:g/string/d Delete all lines containing string
:v/string/d Delete all lines containing which didn’t contain string
:%s#<[^>]\+>##g Delete HTML tags but keeps text
:%s/^\(.*\)\n\1$/\1/ Delete lines which appears twice
Ctrl+a Increment number under the cursor
Ctrl+x Decrement number under cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment