practical vim
significant
- act, repeat & reverse
- operator{motion}
- “a @a
- :g//p
vim way
Tip | Command | Meaning |
---|---|---|
1 | . u | repeat & reverse miniature macro |
2 | j. CSIAO | repeat among lines combined commands |
3 | f+ s ;. | make the motion repeatable |
4 | … uuu ;;; ,,, nnn NNN | act, repeat & reverse |
5 |
| find and replace by hand |
6 | j. ;. n. | one keystroke to move, one to execute |
modes
normal mode
Tip | Command | Meaning |
---|---|---|
7 | normal mode | natural resting state a new perspective to texts |
8 | <Esc>o | chunk your undos |
9 | daw | make your change repeatable |
10 | <C-a> <C-x> | add minus |
11 | dw. | rather than d2w, don’t count if you can repeat |
12 | operator{motion} | operator + motion = action operator:d, c, y, g[~|u|U], >, <, =, ! motion: iw aw s p $ G |
insert mode
Tip | Command | Meaning |
---|---|---|
13 | <C-h> <C-w> <C-u> | delete back |
14 | <Esc> <C-o> | go back to normal mode & insert-normal mode |
15 | <C-r>0 | paste from register |
16 | <C-r>=6*35 | simple calculation |
17 | <C-v>u1234 ga | ascii & unicode find unicode |
18 | <C-k>?I | digraph for unicode |
19 | R | replace mode |
visual mode
Tip | Command | Meaning |
---|---|---|
20 | viw | rather than select mode |
21 | vV<C-v> ebo | end, before, other |
22 | Vj >. | repeat line wise |
23 | vitU gUit | character-wise visual may be strange when ., try normal command |
24 | <C-v>r- | tabular data |
25 | <C-v>c | change columns of text |
26 | <C-v>A | A & I in visual block mode |
command line mode
Tip | Command | Meaning |
---|---|---|
27 | delete yank put copy move join normal substitute global | executed across many lines |
28 | 1 $ 0 . ‘m ‘< ‘> % | different address |
29 | :6t. :6m. | copy or move lines, do not use register |
30 | :’<,’>normal A; | ex+normal=powerful |
31 | :bnext @: <C-o> | repeat last :bnext & reverse |
32 | <C-d> <Tab> <S-Tab> | ex completions |
33 | <C-r><C-w> | insert current word to ex |
34 | <Up> <Down> q: q/ <C-f> | recall from history |
35 | :!ls :!echo % :shell exit <C-z> fg :read :write :[range]!{cmd} | execute in shell & back stdout stdin filter |
36 | :source batch.vim | run multiple ex command |
files
multiple files
Tip | Command | Meaning |
---|---|---|
37 | :bn :bp :b 1:bufdo :bd | buffer: in-memory representation of a file |
38 | :args :argdo :next :prev | argument list: group buffers into a collection |
39 | set hidden | dirty buffer must hidden |
40 | :sp :vs :clo :on mouse | manage windows |
41 | :tabe <C-w>T :tabc :tabo gt gT :tabm | manage tabs: virtual desktops |
file open & save
Tip | Command | Meaning |
---|---|---|
42 | :e %:h<Tab> | expand filepath |
43 | :find main.cpp | find in filepath |
44 | :e . | explore file system |
45 | :!mkdir -p %:h | save files to nonexistent directories |
46 | :w !sudo tee % > /dev/null | write as sudo tee |
getting around
motions inside files
Tip | Command | Meaning |
---|---|---|
47 | hjkl | basic move |
48 | gj gk g0 g$ | act on display lines |
49 | w b e ge W B E gE | move word and WORD wise |
50 | fx ;;; , | find more rarer characters |
51 | /ta n N v /ta d d/ta | search to navigate |
52 | i ) } ] > ’ ” ` t | delimited text objects |
53 | ciw W s p daw W s p | bounded text objects: delete around, change inside |
54 | m{a-zA-Z} `{mark} ‘{mark} “ . ^ [ ] < > | manual and automatic |
55 | % | jump between parentheses |
jumps between files
Tip | Command | Meaning |
---|---|---|
56 | <C-i> <C-o> | jump list |
57 | g; g, `. `^ | change list |
58 | gf | path + filename + suffix |
59 | mM `M | set global mark before diving |
registers
copy & paste
Tip | Command | Meaning |
---|---|---|
60 | fx xp dd p | swap next two characters lines |
61 | “ayiw “ap “_ “+ “* | use named registers |
62 | ve p de mm ww ve p `m P | replace visual selection swap two words |
63 | <C-r>0 gP | paste fom register |
64 | :paste ”+p | use system clipboard |
macros
Tip | Command | Meaning |
---|---|---|
65 | qa @a | record and play back macro |
66 | 100@a | macro abort as feature |
67 | qq;.q 22@q | macro with dot |
68 | VG :normal @a | play back parallel |
69 | qA | append to register a |
70 | :argdo normal @a | act upon files |
71 | :let i=0 :let i+=1 | for i = 0 … n |
72 | :put a 0”ay$dd | edit macros |
patterns
matching
Tip | Command | Meaning |
---|---|---|
73 | ignorecase smartcase \c \C | smart: case sensitive when include upper |
74 | \v#(\x{6}) | switch to regex search |
75 | \Va.k.a | switch to verbatim text |
76 | \v() | use parentheses to capture submatches |
77 | \v<> | word boundaries |
78 | \zs\ze | stake actually match |
79 | escape(@*, getcmdtype().’\’) | escape problem characters |
search
Tip | Command | Meaning |
---|---|---|
80 | / ? * # n N | ’s next are prev |
81 | :noh | mute hightlighting |
82 | :incsearch | live preview search result |
83 | //e | use search offset to end |
84 | gUgn | operate on match |
85 | \v’(([^’]|’\w)+)’ | iterating upon search history |
86 | :vimgrep //g % | count current pattern |
87 | xnoremap * | search on select |
substitution
Tip | Command | Meaning |
---|---|---|
88 | g c n & e | substitute flag |
89 | :%s///g | % and g |
90 | :%s///gc | confirmation mode |
91 | / :s | use search pattern |
92 | :%s//\=@0/g | use register @/ and @0 |
93 | :&& | repeat previous subtitute |
94 | :%s//\3,\2,\1 | rearrange fields |
95 | submatch(0)-1 | minus on replace |
96 | :let swapper={“dog”:”man”,”man”:”dog”} | swap two words |
97 | :cfdo %s//Practical/gc | replace accross multiple files |
global
Tip | Command | Meaning |
---|---|---|
98 | :[range] global[!] /{pattern}/ [cmd] | global command |
99 | :g//p :v//p | print match/unmatch |
100 | :g/TODO/yank A | append TODO to register a |
101 | :g/{/ .+1,/}/-1 sort | sort css properties |