I am closing down my old blog on
socketwiz.com and I didn't have much
content over there and this was and article I felt like preserving.
Since I have been using Vim almost exclusively lately I made myself a
cheat sheet to help me remember some stuff. This is certainly not an
exhaustive list, just some things that I don’t know how to do off the
top of my head that I wrote down and ended up getting a little carried
away. Just thought I would share:
Motions
motion | description |
h | Count characters left |
l | Count characters right |
^ | To the first character of the line. |
$ | To the last character of the line. |
f | To the counth character occurrence to the right. F to the counth character occurrence to the left. |
t | To 1 character just before the counth character occurrence to the right. |
T | To 1 character just before the counth character occurrence to the left. |
w | Count words forward. |
W | Count words forward (different definition for what a word is, includes special characters and such.) |
e | Count forward to the end of word. |
b | Count words backward. |
trigger | effect |
c{motion} | Change |
d{motion} | Delete |
y{motion} | Yank into register |
g~{motion} | Toggle case |
gu{motion} | Make lowercase |
gU{motion} | Make uppercase |
>{motion} | Shift right |
<g{motion} | Shift left |
={motion} | Auto-indent |
Buffer management
command | description |
<C-^> or | Switch to the buffer you just left. |
Split windows
command | description |
s or :sp[lit] | Split file horizontally. |
v or :vsp[lit] | Split file vertically. |
w | Cycle between open windows. |
h | Focus the window to the left. |
j | Focus the window below. |
k | Focus the window above. |
l | Focus the window to the right. |
:cl[ose] c | Close the active window. |
:on[ly] o | Keep only the active window, closing all others. |
Spelling
command | description |
:setlocal spell | Enable spell checker |
]s | Jump to next spelling error. |
[s | Jump to previous spelling error. |
z= | Suggest corrections for current word. |
zg | Add the current word to spell file. |
zw | Remove the current word from spell file. |
Code folding
command | description |
zf{motion} | Folds code when "foldmethod" set to manual or "marker". |
za | Toggle fold at cursor. |
zo | Opens fold at cursor. |
zc | Close fold at cursor. |
zR | Open all. |
zM | Close all. |
Search and replace
command | description |
:args **/*.txt | Multi-file Step 1) populate the argument list with the files you want to search. |
:argdo %s/search/replace/gc | Mult-file Step 2) replace all occurrences of search with replace but prompt before doing so. |
* | Search for the word under the cursor. |
Jumps
command | description |
:jumps | Display the jump list. |
| Jump backwards through the jump list. |
| Jump forwards through the jump list. |
:changes | Display the change list. |
g; | Jump backwards through the changes list. |
g, | Jump forwards through the changes list. |
gf | jump to the file name under the cursor. |
Marks
command | description |
:marks | Display the marks list. |
m | Set a file bookmark. |
m | Set a buffer bookmark. |
' | Jump to the mark. |
'' | Jump to the line in the current buffer where jumped from. |
:delmarks | Delete specified mark. |
:delmarks a-d | Delete marks a through d |
:delmarks a,b,x,y | Delete only marks a,b,x and y. |
:delmarks! | Delete all lower case marks. |
Registers
command | description |
:registers | Display the register list. |
"{motion} | Overwrite or use contents of register. |
"{motion} | Append or use contents of register. |
0 | Populated with last yanked text. |
Macros
command | description |
q{register} | Start recording and store it in the specified register. |
q | Stop recording. |
{count}@{register} | Execute specified macro count times. |
Sessions
command | description |
:mksession /path/to/session.vim | A Session keeps the Views for all windows, plus the global settings. |
:source /path/to/session.vim | Open a session from within Vim. |
vim -S /path/to/session.vim | Start Vim with a session. |
Quickfix window
command | description |
:copen | Open the quickfix window. |
:ccl | Close it. |
:cw | Open it if there are "errors", close it otherwise (some people prefer this). |
:cn | Go to the next error in the window. |
:cnf | Go to the first error in the next file. |
:cc{num} | Go to the error by number. |
Navigation
key | move to |
% | End of construct |
[[ | Backwards to the beginning of the current function. |
][ | Forwards to the beginning of the current function. |
]} | Beginning of the current block. |
[{ | End of the current block. |
}[ | Beginning of the current comment block. |
}] | End of the current comment block. |
gd | First usage of the current variable name. (Mnemonic: go to definition). |
gD | Go to the first global usage of the current variable name. |
gg | Beginning of file. |
G | End of file. |
Help
key | move to |
~<C-]>~ | follow link |
~~ | jump back |
Addon References
ctrlp.vim https://github.com/kien/ctrlp.vim
command | description |
CtrlP | Invoke ctrl-p in file mode. |
F5 | Purge the cache for the current directory. |
| Cycle between modes. |
| Switch to filename instead of full path. |
| Switch to regex mode. |
| Navigate the result list. |
.. | To go up the directory tree. |
: | Excute command against the file. |
: | Jump to line number. |
Tcomment https://github.com/tomtom/tcomment_vim
command | description |
gc{motion} | Toggle comments (for small comments within one line the &filetype_inline style will be used, if defined). |
gcc | Toggle comment for the current line. |
C{motion} | Comment region. |
Cc | Comment the current line. |
| :TComment. |
b | :TCommentBlock. |
vim-fugitive https://github.com/tpope/vim-fugitive
command | description |
Gstatus | git status (- to add/reset changes) |
Gcommit | git commit |
Gmove | git move |
Gremove | git rm |
Ggrep | git grep |
Glog | git log |
Gbrowse | open current file on Github |
Git | run arbitrary git command |
vim-surround https://github.com/tpope/vim-surround
command | description |
cs | Change old delimeter to new. |
ds | Remove delimeters. |
ysiw | Wrap current word. |
S | While in visual mode. |
gitgutter https://github.com/airblade/vim-gitgutter
command | description |
[h | Previous hunk |
]h | Next hunk |
Miscellaneous
command | description |
v | Enable characterwise Visual mode. |
| Enable linewise Visual mode. |
| Enable blockwise Visual mode. |
gv | Reselect the last visual selection. |
o | Go to other end of highlighted text. |
"+p | Paste from system clipboard. |
:0,$d | Delete every line in a file. |
| Increment number. |
| Decrement number. |
vit | Select contents inside of an HTML tag. |
:map | List all currently define mappings. |
:verbose map | Display where mapping was defined. |
g/{pattern}/d | Delete every line matching pattern. |
v/{pattern}/d | Delete every line not matching pattern. |
:w !sudo tee % | Save a file with sudo that you opened with proper permissions. |
bufdo {command} | Execute specified command on every buffer. |
{trigger}i{container} | Inside some container like brackets, tag, or parenthesis for example. |
:e | Refresh or reload buffer |
Merge Conflicts
(requires the fugitive plugin)
:Gdiff
Target | Working | Merge
//2 //3
[c
and ]c
to navigate between conflicts
:diffget //2 or //3
:diffupdate
:only
:wq
$ git add .
$ git commit -m 'message'
Build from source
$ cd ~
$ hg clone https://code.google.com/p/vim/
$ hg tags | grep less # find out what latest stable build is
$ hg checkout <latest_stable>
$ cd vim
$ ./configure --with-features=huge \
--enable-rubyinterp \
--enable-pythoninterp \
--enable-gui=gtk2 --enable-cscope --prefix=/usr
$ make VIMRUNTIMEDIR=/opt/local/share/vim/vim73
$ make install
Tags:
#dtrace
#git
Categories:
#technology