hjkl: left, down, up, right
-
word
w: beginning of nextb: beginninge: end
-
line
O or ^: beginning (Ito insert)$: end (Ato insert):nornG: jump to linen
-
document
gg: beginningG: end
-
page
CTRL-F: forwardCTRL-B: backward
-
x: character (nxto deletencharacters) -
dw: word -
dd: line (nddto deletenlines from current position) -
delete
nline(s)dnk: abovednj: below
-
delete current line to
Dord$: endd0ord^: beginning
-
delete from document to
dgg: startdG: enddnG: line numbern
-
csw"orviwS": surround word with quotes (repeat on next word:W.)csw<strong>orvS<strong>: add html tags around wordcs'": change surrounding quote to single quoteds": delete surrounding quotationdst: delete surrounding tag
-
other
di": delete inside double quotation marksd/c: delete until next charactercon any line:g/pattern/d: delete lines matching pattern:g/patternthen:v//d: delete lines not matching pattern
-
/patternand?pattern: forward/backward for pattern -
nandN: move to next/previous occurrence of search pattern -
*: if already on word on screen, hit the*key to search for next occurrence of whole word -
pattern1\|pattern2\|pattern3: find any of the patterns -
after searching for word, use
:%s//new/gto change all occurrences to 'new', or:g/to list all lines containing the word. -
:set hlsearchand:noh: enable highlighting of all searches and turn off highlighting -
search & replace
:s/, /\r&/g: replace all,on current line with a new line:%s/ \+/,/g: replace all whitespace with single comma:%s/cat/dog/ig: replace everything, case insensitive:%s/$/foo/g: addfooto the end of each line:5,12s/foo/bar/g: replacefoowithbarglobally between lines 5 and 12:%s/\s*\w\+\s*$//: delete last word of each line/\v<[A-Z]+>&:%s//\r&/g: find all capital words and create a newline after match:g/\w$/ s/$/,/: find all lines that end with\wand append a comma
search directory (silver searcher with ag.vim)
,a [options] {pattern} [{directory}]: search code across files in directory,a flask -l -i -G py: list matching filenames forflask, case insensitive, in filenames that containpy
fc: search forward on current line to characterc(dfc: delete)Fc: search backward on current line to characterc(dFc: delete)tc: search forward on current line to character before characterc(dtc: delete)Tc: search backward on current line to character after characterc(dTc: delete)
-
v- select by characters -
V- select by lines -
preprend multiple lines
CTRL-v, highlight lines,Shift-Ito insert at beginning of block, pressEsc
-
visual select
>>: indent the text right by shiftwidth<<: indent the text left by shiftwidthy: "yank" the text into the paste buffer
-
visual insert
CTRL V(select column),Shift i(type text to insert),Esc: insert text in multi-line selection
-
paste output from stdout into vim
- type valid shell/python/R command in vim, visually select all lines, and enter
:!python, or:!bash, or:!R --vanilla
- type valid shell/python/R command in vim, visually select all lines, and enter
-
maximum awesome features
vii/vaivisually select in or around the cursor's indentVp/vpreplaces visual selection with default register without yanking selected text (works with any visual selection)
o: start a new line belowO: start a new line above
# in .vimrc.local (http://evertpot.com/osx-tmux-vim-copy-paste-clipboard/)
set clipboard=unnamed
y: copy/yankp: paste
or
:w !pbcopy: copy (after visual selection):r !pbpaste: paste
-
yy: yank (cut) the current line and put it in buffer -
Nyy: yank (cut) N lines and put it in buffer -
:g/pattern/y A: yank all matching lines in one buffer -
after searching for a pattern, copy only the text that matches, where
ais any register (seeCopyMatches):let @a = '' :CopyMatches a
p: paste at the current position the yanked line or lines from the buffer
gU: uppercase"HellO"to"HELLO"gu: lowercase"HellO"to"hello"
u: undo the previous operationCTRL-r: redo changes which were undone (undo the undos)
gcctoggles current line commentgctoggles visual selection comment lines
:1,10sort: sort lines 1 through 10- visual select +
:sort: sort selected lines
:tabe {file}; edit file in new tab:tabfind {file}: search path to find and open file in new tab:args *.py | :tab all: open multiple files in new tabsgt: go to next tabgT: go to previous tabCTRL-w SHIFT-t: move pane to tab
:!ls: shows file list in directoryr:!ls: copies the above command into vim window
q <letter>: records everything you type (qagain ends recording)@ <letter>: replays recordingq a+<your edit>+ESC+j h q, use with@ a: repeat command on multiple lines- explanation:
q astarts recording macro to registera, perform edit, exit insert mode,jmoves down one line,qends recording)
- explanation:
:set colorcolumn=80
CTRL-w R: rotate windows up/leftCTRL-w r: rotate windows down/rightCTRL-w L: move current window to the far rightCTRL-w H: move current window to the far leftCTRL-w J: move current window to the very bottomCTRL-w K: move current window to the very top
:qa: close all tabs/windows:wqa: close and save all tabs/windows
-
:spopen new file in horizontal split -
:vspopen new file in vertical split -
vim -p *: open multiple files in tabs -
vim -o *: open multiple files stacked -
vim -O *: open multiple files side by side -
move between multiple windows
CTRL-w+ arrow /CTRL-w+w: move between windowsCTRL-w t+CTRL-w K: change vertically split windows to horizonally splitCTRL-w t+CTRL-w H: horizonally split to vertically split
-
Explore filesystem with NERDTree
,d: toggle the drawer,f: open NERDTree to the current filei: open selected file in a horizontal split windows: open selected file in a vertical split windowt: open selected file in a new tabm: delete file in menu
-
Change working directory
:pwd: present working directory:cd %:p:h: change directory of currently open file (sets current directory for all windows in Vim):lcd %:p:h: change directory only for current window
-
full path fuzzy file, buffer finder with ctrlp.vim
,tbrings up a project file filter for easily opening specific files,brestrictsctrlp.vimto open buffers
Easily jump to code definitions
- Create a cron job to update tags daily
0 12 * * * ctags --languages=python -R -o ~/.tags ~/path/to/code- add
--python-kinds=-ito~/.ctagsto ignore imports - add
set tags=~/.tags;/tovimrc - use
CTRL-w ]to jump to code definition (or :TagbarOpen to open viewer)
install plugins by adding following to vimrc.bundle.local
Plugin '<user>/<plugin>'
and run
:PluginInstall
:so filename: source vim code in filename
,[space]strips trailing whitespace,lbegins aligning lines on a string, usually used as,l=to align assignments