24.04.2021
Code formatting
:set list
,\l
- show invisibles:retab!
- auto converting between tabs and spaces:set expandtab
,retab!
- to spaces:set noexpandtab
,retab!
- to tabs
.
- repeat
Normal mode
h
,j
,k
,l
- left, down, up, rightgj
- go down display linegk
- go up display line0
- to the first character of real lineg0
- to the first character of display line^
- to the first nonblank character of lineg^
- to the first nonblank character of display line$
- to the end of real lineg$
- to the end of display linew
- forward to start of next wordW
- to the next WORDb
- backward to start of current/previous wordB
- backward to start of current/previous WORDe
- forward to end of current/next wordE
- forward to end of current/next WORDge
- backward to end of previous wordgE
- backward to end of previous WORD<number>G
- to the linegg
- beginning of the fileG
- end of the file^G
- show current line%
- move to matching bracketsf<char>
- find nextF<char>
- find backwardt<char>
- find next char and place cursor beforeT<char>
- find next char and place cursor before backward;
- go to the next off
/t
,
- go to previous off
/t
g;
- back to the last changeg,
- forward to the next change:changes
- list changes{number}+
- go to the{number}
lines down{number}-
- go to the{number}
lines up
Marks
m{a-zA-Z}
- set a mark`{mark}
- go to the exact mark place'{mark}
- go the first non-whitespace character on where mark was set
Automatic marks
`
- Position before the last jump within current file.
- Location of last change^
- Location of last insertion[
- Start of last change or yank]
- End of last change or yank<
- Start of last visual selection>
- End of last visual selection
Search
/<text>
- forward searchd/ge
- delete till you findge
v/ge
- select tillge
/{text}\c
- case insensitive/{text}\C
- case sensitive/\v{regex}
- very magic search (more close to regex)/\V{text}
- very nomagic/\v<{word}>
- search for the word\zs
,\ze
- helps to define boundaries of a match, for example/Practical \szVim<CR>
?<text>
- backward search:hlsearch
- highlight search results*
- find next current wordn
- next occurrence in same directionN
- next occurrence in opposite direction
Substitution
:[range]s/{pattern}/{string}/{flags} [count]
- replace
Flags
g
- globally (within line)c
- confirm changesy
to substitute this matchn
to skip this matchesc
to skip this matcha
to substitute this and all remaining matches {not in Vi}q
to quit substituting {not in Vi}⌃E
to scroll the screen up {not in Vi}⌃Y
to scroll the screen down {not in Vi}
&
- same flags as previous invocation of substitutee
- subpress errorsi
- ignore case for the patternI
- don’t ignore case for the patternp
- print the line containing last substitute
Range
%
- whole file<n>,<m>
- between lines (including)
Special characters for the replacement string
\r
- carriage return\t
- tab\\
- single backslash\1
- first submatch\0
,&
- entire matches pattern~
- use string from previous invocation of substitute\={Vim script}
- evaluate script
Edit
O
- insert line above the cursoro
- insert line below the cursorA
- start typing at the end of lineI
- start typing at the beginning of the linea
- start typing after cursori
- start typing in place of cursorC
- replace from cursor to the end of the lineS
- replace whole lines
- replace current selectionx
- replace under cursorr<char>
- replace char under cursor withgr
- virtual replace, if tabs = spacesR
- in place replacement (more than one letter)gR
- virtual replace, if tabs = spaces{register}d{motion}
- delete {motion}"_d{motion}
- to delete in black hole (not a cut)c<motion>
- change{register}y<motion>
- yank~<motion>
- swap case (when selected)u<motion>
- lowercase (when selected)U<motion>
- uppercase (when selected)><motion>
- shift right (>>
line)<<motion>
- shift left (<<
line)=<motion>
- autoindentu
,:u[ndo]
- ungo changes⌃R
,:red[o]
- redo changesU
- undo all latest changes on the line
Insert mode
⌃w
- delete word⌃u
- delete line⌃b
- backspace⌃[
- as escape⌃o
- insert Normal Mode (just for one command)⌃r{register}
- paste in insert mode⌃r={expression}
- evaluate expression and paste:let i = 1
^r=i<CR>
- insert i from let
⌃v[u]{digit}
- insert letter by code⌃k{char1}{char2}
- insert letter by :diagraphs (« » 12 14 34) see:diagraph-table
Visual mode
v
- character-wiseV
- line-wise⌃v
- block-wisegv
- reselecto
- go to the other end of the highlighted text
Motions
a)
,ab
- a pair of(parentheses)
i)
,ib
- inside of(parentheses)
ci)
- replace everything inside(...)
a}
,aB
- a pair of{braces}
i}
,iB
- inside of{braces}
a]
- a pair of[brackets]
i]
- inside of[brackets]
a>
- a pair of<angle brackets>
i>
- inside of<angle brackets>
a'
- a pair of'single quotes'
i'
- inside of'single quotes'
a"
- a pair of"double quotes"
i"
- inside of"double quotes"
a`
- a pair ofbacktricks
i`
- inside of backtricksat
- a pair of<xml>tags</xml>
it
- inside of<xml>tags</xml>
iw
- current wordciw
- replace current word.
aw
- current word plus one spacedaw
- delete word and one space after it.
iW
- current WORDaW
- current WORD plus one spaceis
- current sentenceas
- current sentence plus one spaceip
- current paragraphap
- current paragprah plus one blank line
Yanking
{register}p
- paste text:put {register}
- paste from register below current line⌃r{register}
- paste in insert mode, where register can be"
,0
,+
gp
- paste and place cursor at the end of pasted text
{register}P
- paste previous{register}y{motion}
- yankyy
- yank the line- by default yank always puts everything into unnamed and 0 registers
- uppercase registes are used for append
"+
,"*
- system clipboard
:reg
- see content of all registers"%
- name of current file"#
- name of the alternate file".
- last inserted text":
- last Ex command"/
- last search pattern
Windows
⌃w w
- next window⌃w h
- next window on left⌃w j
- next window below⌃w k
- next window above⌃w l
- next window rigth⌃w c
,:close
,:cl
- close current window⌃w o
,:only
,:on
- keep only current window open⌃w =
- equalize width and height of all windows⌃w _
- maximize height of active window⌃w |
- maximize width of active window{n} ⌃w _
- set active window height to{n}
rows{n} ⌃w |
- set active window width to {n} rows⌃w s
,:split {filename}
- split by horizontally⌃w v
,:vsplit {filename}
- split by vertically⌃w H
,⌃w J
,⌃w K
,⌃w L
- swap windows (:help window-moving
)⌃w x
- rotate current window (:help window-moving
)⌃w r
- rotate all windows (:help window-moving
)^w z
- close preview window
Tabs
:tabedit {filename}
,:tabe {filename}
- open filename in new tab^w T
- move current window into its own tab:tabc[lose]
- close current tab:tabo[nly]
- keep the active tab page{N}gt
,:tabn[ext] {N}
- switch to tab page number{N}
gt
,:tabn[ext]
- switch to next tabgT
,:tabp[revious]
- switch to the previous tab:tabmove [N]
- rearrange the tab
Buffers
:ls
- list of buffers[b
,:bn[ext]
- next buffer]b
,:bp[revious]
- previous buffer[B
,:bfirst
- go to the first in the buffer]B
,:blast
- go to the last in the buffer:buffer N
- go to the buffer N⌃-^
- alternate file:bd[elete]
- delete current buffer:bufdo {command}
- execute command for each buffer
Args
:args
- see args passed to the vim:args {list}
- build arguments list:args **/*.js **/*.css
- load all js and css files in args
:argdo command
- execute command on each argument:argdo normal @a
- execute macro a for each file in args:argdo write
- write all files
]a
,:next
- go to the next in args[a
,:prev
- go to the prev in args]A
,:first
- go to the first in args[A
,:last
- go to the last in args
Jumps
⌃o
- jump to previous location⌃i
- jump to forward location:jumps
- show all jumps^]
- jump to definition of keyword under the cursor- Use ctags to generate ctags file
⌃t
,:pop
- to navigate back for our tag historyg ⌃]>
- if multiple matches - show choices:tselect
,:tnext
,:tprev
,:tfirst
,:tlast
- navigate between multiple choices:tag {keyword}
- jump to definition:tjump {keyword}
- jump and ask for multiple:tjump /{regex}
- try to find
gf
- jump to file name under the cursor:set suffixesadd+=.rb
to add specific suffix:set path+=,%:h
to add current directory
(
,)
- jump to start of previous/next sentence{
,}
- jump to start of previous/next paragraph
Command line mode
ga
- find code of selected character:set nowrap
- unwrap lines:setlocal spell
- turn on spell check@:
- repeat the last ex command⌃r
,⌃w
- copy and paste current word to the command line⌃p
,Up
- previous item from history⌃n
,Down
- next item from historyq:
- see history of command lineq/
- see history of searches:sh
- go to shell:e <filename>
- open file name for edit:e!
- re-read file from the disk (discard all changes):e %<Tab>
- to get edit current opened file:e %:h<Tab>
- to get edit directory of current opened file
:w <filename>
- write current to file (whole file or selection):wall
- write all buffers:update <filename>
- same as write, but only if modified:r <command|filename>
- read from external file or command and paste:!{command}
- execute command with shell:2,$!sort -t',' -k2
- sort all lines except header by second columnread !{cmd}
- executecmd
in shell and insert its stdout below cursor:[range]write !{cmd}
- executecmd
in the shell with[range]
lines as stdin
:[range]t {address}
,:[range]co[py] {address}
- copy line(s) from [range] and paste to {address}.
- instead of address means current line:6copy.
- copy from line 6 and insert on current line:t6
- copy current line to below line 6:t.
- duplicate the current line:t$
- copy the current line to the end of the file'<,'>t0
- copy the visually selected lines to the start of the file
:[range]m[ove] {address}
- move line(s) from [range] and paste to {address}:[range]normal {command}
- execute command on selected range:%normal i//
- instead//
for whole file (command all lines)'<,'>normal .
- repeat last command for selected lines
:[range] gloabl[!] /{pattern}/ [cmd]
- execute command on range with matched pattern:v
,:vglobal
- execute cmd on each line which does not match pattern
:sort
- sort selected lines:grep
- grep in files:grep -R Quickfix *
- search in all directories
:vim[grep][!] /{pattern}/[g][j] {file} ...
- search with internal vim grepg
- multiple matches on the same linej
- don’t jump to first match
:make
- execute make command:setlocal makepkg=NODE_DISABLE_COLORS=1\ nodelint\ %
- change make program:setglobal errorformat?
- see how errors are getting formatted:compiler
- to set compiler
Macros
q{register}{macro}q
- start recording macros- Uppercase register appends to the macro
@{register}
- replay macro10@a
- execute 10 times macro from registera
Quickfix
:cnext
- jump to next item:cprev
- jump to previous item:cfirst
- jump to first item:clast
- jump to last item:cnfile
- jump to first item in next file:cpfile
- jump to last item in previous file:cc N
- jump to Nth item:copen
- open the quickfix window:cclose
- close the quickfix window:colder
- older version of the quickfix list:cnewer
- newer version of the quickfix list
Autocompletition
⌃n
- generic keywords⌃p
- generic keywords (previous)⌃y
- accept the currently selected match (yes)⌃e
- revert to the originally typed text⌃h
- delete one character from current match⌃l
- add one character from current match⌃x ⌃n
- current buffer keywords⌃x ⌃i
- included file keywords⌃x ⌃]
- tags file keywords⌃x ⌃k
- dictionary lookup:set spell
should be enabled
⌃x ⌃l
- whole line completion⌃x ⌃f
-filename completion:pwd
- to from where autocompletion will work:cd public
- to change to folder:cd -
- to previous working directory
⌃x ⌃o
- omni-completion
Vim spell checker
:set spell
- enable spell checkingspellfile
to share file with custom words
:set spelllang=en_us
- change speaking region[s
- previous error]s
- next errorz=
- fix errorzg
- add current word to spell filezw
- remove current word from the spell filezug
- revertzg
orzw
command for current word⌃x s
- fix misspelled word in insert mode
Foldings
zi
- switch folding on or offza
- toggle current fold open/closedzc
- close current foldzR
- open all foldszM
- close all foldszv
- expand folds to reveal cursorzj
- move down to top of next foldzk
- move up to bottom of previous foldzo
- open current foldzO
- recursively open current foldzc
- close current foldzC
- recursively close current foldza
- toggle current foldzA
- recursively open/close current foldzm
- reduce foldlevel by onezM
- close all foldszr
- increase foldlevel by onezR
- open all folds
Exit/Save
:q[uit]
- quit vim:q[uit]!
- quit without saving:cq[uit]
- quit always, without writingZZ
,:wq
- write and quitZQ
,:wq!
- write current file and exit always:wq {file}
- write to{file}
and exit after
Vim from command line
vim -u NONE -N
- launch vim without settings
Plugins
surround.vim
S"
- surround selection with"
cs}]
- change surrounding from}
to]
pymode
K
- show help for current symbol[ [
- Jump to previous class or function (normal, visual, operator modes)] ]
- Jump to next class or function (normal, visual, operator modes)[M
- Jump to previous class or method (normal, visual, operator modes)]M
- Jump to next class or method (normal, visual, operator modes)aC
- Select a class. Ex: vaC, daC, yaC, caC (normal, operator modes)iC
- Select inner class. Ex: viC, diC, yiC, ciC (normal, operator modes)aM
- Select a function or method. Ex: vaM, daM, yaM, caM (normal, operator modes)iM
- Select inner function or method. Ex: viM, diM, yiM, ciM (normal, operator modes)^r
- Run script^b
- Set breakpoint
Rope
^c-d
- Show doc^c-rr
- Rename^c-r1r
- Rename module^c-ro
- Organize imports^c-r1p
- Convert module to package^c-rm
- Extract method^c-rl
- Extract variable^Space
- Rope completion
find project -name '\*.rb' -type f -exec sed -i -e 's/regex/replacement/g' -- {} +
# Piece by piece:
project = search in "project" tree
-name '\*.rb' = for things whose names match '\*.rb'
-type f = and are regular files (not directories, pipes, symlinks, etc.)
-exec sed = run sed with these arguments:
-i = with in-place edits
-e 's/regex/replacement/g' = execute a "substitute" command
(which is almost identical to Vim's :s, but note lack of % -- it's the default in sed)
-- = end of flags, filenames start here
{} = this tells find that the filenames it found should be placed
on sed his command-line here
+ = this tells find that the -exec action is finished, and we want to group the arguments into as few sed invocations as possible (generally more efficient than running it once per filename). To run it once per filename you can use \\; instead of +.
comments powered by Disqus