2 031e20a5 2019-04-19 hi <a href="/suggest.html">suggestion box</a> –
3 031e20a5 2019-04-19 hi <a href="https://git.high5.nl/why-vi.rocks/log/">latest additions</a> –
4 031e20a5 2019-04-19 hi <a href="/also.html">see also</a> –
5 031e20a5 2019-04-19 hi <a href="/colophon.html">colophon</a>
8 c95d1e65 2019-04-20 hi <img class="mt4" src="vi-sw.png">
10 031e20a5 2019-04-19 hi _[vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
11 031e20a5 2019-04-19 hi text editor in any Unix-like operating system._
13 031e20a5 2019-04-19 hi Here is a collection of [vi(1)](https://man.openbsd.org/vi.1)/[ex(1)](https://man.openbsd.org/ex.1) commands and command sequences.<br>
14 031e20a5 2019-04-19 hi Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
16 c57b7323 2020-05-10 git [Bill Joy's greatest gift to man - the vi editor](https://www.theregister.co.uk/2003/09/11/bill_joys_greatest_gift/)
19 031e20a5 2019-04-19 hi ## Yank/delete lines
21 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
22 031e20a5 2019-04-19 hi (2) move to last line<br>
23 031e20a5 2019-04-19 hi (3a) yank: `y'k`<br>
24 031e20a5 2019-04-19 hi (3b) delete: `d'k`<br>
25 031e20a5 2019-04-19 hi (4) move to destination line<br>
26 031e20a5 2019-04-19 hi (5) put with `P` or `p`<br>
29 031e20a5 2019-04-19 hi ## Apply regex to lines
31 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
32 031e20a5 2019-04-19 hi (2) mark the last line: `ml`<br>
35 031e20a5 2019-04-19 hi :'k,'ls/<em>regex</em>/<em>power</em>/g
39 50439ce0 2019-04-22 git ## Increment / Decrement number in command mode
41 50439ce0 2019-04-22 git (1) move cursor to number<br>
42 50439ce0 2019-04-22 git (2a) increment by one: #+<br>
43 50439ce0 2019-04-22 git (2b) increment by N (5): 5#+<br>
44 50439ce0 2019-04-22 git (3a) decrement by one: #-<br>
45 50439ce0 2019-04-22 git (3b) decrement by N (9): 9#-
48 031e20a5 2019-04-19 hi ## Add # to a block
55 c57b7323 2020-05-10 git ## Remove trailing whitespace from every line
62 64a200c4 2023-06-06 mischa ## Remove tabs
65 64a200c4 2023-06-06 mischa :%s/<u><CTRL-V><TAB></u>//g
69 031e20a5 2019-04-19 hi ## Remove trailing whitespace from a block
72 75e09f64 2020-07-19 git :'k,'ls/\ *$//
76 50439ce0 2019-04-22 git ## Remove the first N-characters from every line
81 031e20a5 2019-04-19 hi :%s/^.\{0,<em>5</em>\}//
85 031e20a5 2019-04-19 hi ## Delete all lines N-character long
90 031e20a5 2019-04-19 hi :g/^.\{<em>10</em>\}$/d
94 031e20a5 2019-04-19 hi ## Delete all lines _except_ N-character long
99 031e20a5 2019-04-19 hi :g!/^.\{<em>10</em>\}$/d
103 031e20a5 2019-04-19 hi ## Search/replace paths using # as delimiter
106 031e20a5 2019-04-19 hi :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
110 9122df5c 2023-06-06 mischa ## Search/replace ^M with LF
113 9122df5c 2023-06-06 mischa :g/<u><CTRL-V><ENTER></u>/s///g
117 031e20a5 2019-04-19 hi ## Write the file as root
119 18871106 2019-04-06 git :w !doas tee %
122 031e20a5 2019-04-19 hi ## Diff the file on disk with the buffer
124 18871106 2019-04-06 git :w !diff -u % -
127 031e20a5 2019-04-19 hi ## Make a backup of the file on disk
129 6c9b71bc 2019-04-06 git :!cp % %.bak
132 031e20a5 2019-04-19 hi ## Sort all lines
137 031e20a5 2019-04-19 hi ## Sort a block
139 031e20a5 2019-04-19 hi `}` won't be shown
142 190821ba 2019-04-20 git !<u>}</u>sort
146 031e20a5 2019-04-19 hi ## Sort from the current line to EOF
148 031e20a5 2019-04-19 hi `G` won't be shown
151 031e20a5 2019-04-19 hi !<u>G</u>sort
155 031e20a5 2019-04-19 hi ## Delete duplicated lines in the file
159 031e20a5 2019-04-19 hi ## Delete duplicated lines in the block
161 031e20a5 2019-04-19 hi `}` won't be shown
164 031e20a5 2019-04-19 hi !<u>}</u>uniq
167 031e20a5 2019-04-19 hi ## Delete duplicated lines till EOF
169 031e20a5 2019-04-19 hi `G` won't be shown
172 031e20a5 2019-04-19 hi !<u>G</u>uniq
176 031e20a5 2019-04-19 hi ## Underline all lines starting with `pattern`
179 031e20a5 2019-04-19 hi :g/^<em>pattern</em> /t.|s/./=/g
183 031e20a5 2019-04-19 hi ## Search for `pattern`, print the containing function (start with `def`) and line number
186 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^ *<em>def</em> ?#
190 031e20a5 2019-04-19 hi ## Add # to paragraph containing `pattern`
193 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^$?+,//-s/^/#
197 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block
199 9541feb7 2019-04-08 git :g/{$/+,/^}/-!sort
202 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block (media queries)
204 9541feb7 2019-04-08 git :g/^[^@].*{$/+,/}/-!sort
207 031e20a5 2019-04-19 hi ## Format content of `<p>` tag to fixed width
212 031e20a5 2019-04-19 hi :g/<p>/+,/<\/p>/-!fmt -<em>40</em>
216 c57b7323 2020-05-10 git ## Format whole document
220 c57b7323 2020-05-10 git In your .nexrc
223 c57b7323 2020-05-10 git map gF :%!fmt -s<u><CTRL-V><ENTER></u>
226 031e20a5 2019-04-19 hi ## Reverse all lines, move `m` all lines to 0
231 031e20a5 2019-04-19 hi ## Swap `Lastname, Firstname` to `Firstname, Lastname`
233 0bb4466d 2019-04-08 git :%s/\(.*\), \(.*\)/\2 \1/
236 031e20a5 2019-04-19 hi ## Convert to lowercase
241 64a200c4 2023-06-06 mischa ## Surround text with pattern
243 64a200c4 2023-06-06 mischa :%s/.*/`pattern` & `pattern`/
246 031e20a5 2019-04-19 hi ## Join all lines
250 031e20a5 2019-04-19 hi ## Copy `t` or move `m` lines containing `pattern`
253 031e20a5 2019-04-19 hi :g/<em>pattern</em>/t$
254 031e20a5 2019-04-19 hi :g/<em>pattern</em>/m$
257 031e20a5 2019-04-19 hi ## Select a column of a table
258 031e20a5 2019-04-19 hi Select 3rd column separated by colon (`:`)
261 031e20a5 2019-04-19 hi :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
264 031e20a5 2019-04-19 hi ## Insert the sum of a list of numbers after an arbitrary number of lines
266 70b3c166 2019-04-20 hi (1) mark the first line: `mk`
267 70b3c166 2019-04-20 hi (2) mark the last line: `ml`
270 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
276 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
279 031e20a5 2019-04-19 hi ## Email the block
282 031e20a5 2019-04-19 hi :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
285 031e20a5 2019-04-19 hi ## Enable and use `ex` history
287 ade8cac0 2019-04-27 git (1) Set `ESC` key to enable history or add to `~/.nexrc`:
290 c2580c10 2019-04-20 hi :set cedit=<u><CTRL-V><ESC></u>
293 c2580c10 2019-04-20 hi (2) Use it with:
296 031e20a5 2019-04-19 hi :<u><ESC></u>
300 ade8cac0 2019-04-27 git ## Integrate with tmux buffer
302 ade8cac0 2019-04-27 git (1) cut text from current position to mark 'm' into tmux buffer. Hit undo to put text back into vi buffer.
305 ade8cac0 2019-04-27 git !'mtmux load-buffer -
308 ade8cac0 2019-04-27 git (2) paste text from tmux buffer into vi buffer.
311 ade8cac0 2019-04-27 git :r!tmux show-buffer
314 ee74a164 2019-06-05 git (3) Map in ~/.nexrc (command mode)
317 ade8cac0 2019-04-27 git map gx !'mtmux load-buffer -<u><CTRL-V><ENTER></u>
318 ade8cac0 2019-04-27 git map gy !'mtmux load-buffer -<u><CTRL-V><ENTER></u>u
319 ade8cac0 2019-04-27 git map gp :r!tmux show-buffer<u><CTRL-V><ENTER></u>
323 ee74a164 2019-06-05 git ## Remap ESC to ALT-i in ~/.nexrc (insert mode)
326 9e2dabb0 2019-04-27 git map! <u><CTRL-V><ALT-i></u> <u><CTRL-V><ESC></u>