2 031e20a5 2019-04-19 hi <a href="/suggest.html">suggestion box</a> –
3 f63f6045 2024-11-11 mischa <a href="https://got.high5.nl/?action=summary&path=why-vi.rocks.git">latest additions (got)</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 bd65552c 2023-06-11 mischa <div class="logo-container">
9 bd65552c 2023-06-11 mischa <img id="logo" class="mt4" src="vi-sw.png" alt="Logo">
12 f6e4de9c 2023-06-07 mischa _Why? [vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
13 031e20a5 2019-04-19 hi text editor in any Unix-like operating system._
15 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>
16 031e20a5 2019-04-19 hi Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
18 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/)
21 031e20a5 2019-04-19 hi ## Yank/delete lines
23 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
24 031e20a5 2019-04-19 hi (2) move to last line<br>
25 031e20a5 2019-04-19 hi (3a) yank: `y'k`<br>
26 031e20a5 2019-04-19 hi (3b) delete: `d'k`<br>
27 031e20a5 2019-04-19 hi (4) move to destination line<br>
28 031e20a5 2019-04-19 hi (5) put with `P` or `p`<br>
31 031e20a5 2019-04-19 hi ## Apply regex to lines
33 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
34 031e20a5 2019-04-19 hi (2) mark the last line: `ml`<br>
37 031e20a5 2019-04-19 hi :'k,'ls/<em>regex</em>/<em>power</em>/g
41 50439ce0 2019-04-22 git ## Increment / Decrement number in command mode
43 50439ce0 2019-04-22 git (1) move cursor to number<br>
44 50439ce0 2019-04-22 git (2a) increment by one: #+<br>
45 50439ce0 2019-04-22 git (2b) increment by N (5): 5#+<br>
46 50439ce0 2019-04-22 git (3a) decrement by one: #-<br>
47 50439ce0 2019-04-22 git (3b) decrement by N (9): 9#-
50 031e20a5 2019-04-19 hi ## Add # to a block
57 c57b7323 2020-05-10 git ## Remove trailing whitespace from every line
64 64a200c4 2023-06-06 mischa ## Remove tabs
67 64a200c4 2023-06-06 mischa :%s/<u><CTRL-V><TAB></u>//g
71 031e20a5 2019-04-19 hi ## Remove trailing whitespace from a block
74 75e09f64 2020-07-19 git :'k,'ls/\ *$//
78 50439ce0 2019-04-22 git ## Remove the first N-characters from every line
83 031e20a5 2019-04-19 hi :%s/^.\{0,<em>5</em>\}//
87 031e20a5 2019-04-19 hi ## Delete all lines N-character long
92 031e20a5 2019-04-19 hi :g/^.\{<em>10</em>\}$/d
96 031e20a5 2019-04-19 hi ## Delete all lines _except_ N-character long
101 031e20a5 2019-04-19 hi :g!/^.\{<em>10</em>\}$/d
105 031e20a5 2019-04-19 hi ## Search/replace paths using # as delimiter
108 031e20a5 2019-04-19 hi :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
112 9122df5c 2023-06-06 mischa ## Search/replace ^M with LF
115 9122df5c 2023-06-06 mischa :g/<u><CTRL-V><ENTER></u>/s///g
119 031e20a5 2019-04-19 hi ## Write the file as root
121 18871106 2019-04-06 git :w !doas tee %
124 031e20a5 2019-04-19 hi ## Diff the file on disk with the buffer
126 18871106 2019-04-06 git :w !diff -u % -
129 031e20a5 2019-04-19 hi ## Make a backup of the file on disk
131 6c9b71bc 2019-04-06 git :!cp % %.bak
134 031e20a5 2019-04-19 hi ## Sort all lines
139 031e20a5 2019-04-19 hi ## Sort a block
141 031e20a5 2019-04-19 hi `}` won't be shown
144 190821ba 2019-04-20 git !<u>}</u>sort
148 031e20a5 2019-04-19 hi ## Sort from the current line to EOF
150 031e20a5 2019-04-19 hi `G` won't be shown
153 031e20a5 2019-04-19 hi !<u>G</u>sort
157 031e20a5 2019-04-19 hi ## Delete duplicated lines in the file
161 031e20a5 2019-04-19 hi ## Delete duplicated lines in the block
163 031e20a5 2019-04-19 hi `}` won't be shown
166 031e20a5 2019-04-19 hi !<u>}</u>uniq
169 031e20a5 2019-04-19 hi ## Delete duplicated lines till EOF
171 031e20a5 2019-04-19 hi `G` won't be shown
174 031e20a5 2019-04-19 hi !<u>G</u>uniq
178 031e20a5 2019-04-19 hi ## Underline all lines starting with `pattern`
181 031e20a5 2019-04-19 hi :g/^<em>pattern</em> /t.|s/./=/g
185 031e20a5 2019-04-19 hi ## Search for `pattern`, print the containing function (start with `def`) and line number
188 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^ *<em>def</em> ?#
192 031e20a5 2019-04-19 hi ## Add # to paragraph containing `pattern`
195 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^$?+,//-s/^/#
199 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block
201 9541feb7 2019-04-08 git :g/{$/+,/^}/-!sort
204 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block (media queries)
206 9541feb7 2019-04-08 git :g/^[^@].*{$/+,/}/-!sort
209 031e20a5 2019-04-19 hi ## Format content of `<p>` tag to fixed width
214 031e20a5 2019-04-19 hi :g/<p>/+,/<\/p>/-!fmt -<em>40</em>
218 c57b7323 2020-05-10 git ## Format whole document
222 c57b7323 2020-05-10 git In your .nexrc
225 c57b7323 2020-05-10 git map gF :%!fmt -s<u><CTRL-V><ENTER></u>
228 031e20a5 2019-04-19 hi ## Reverse all lines, move `m` all lines to 0
233 031e20a5 2019-04-19 hi ## Swap `Lastname, Firstname` to `Firstname, Lastname`
235 0bb4466d 2019-04-08 git :%s/\(.*\), \(.*\)/\2 \1/
238 031e20a5 2019-04-19 hi ## Convert to lowercase
243 64a200c4 2023-06-06 mischa ## Surround text with pattern
245 64a200c4 2023-06-06 mischa :%s/.*/`pattern` & `pattern`/
248 031e20a5 2019-04-19 hi ## Join all lines
252 031e20a5 2019-04-19 hi ## Copy `t` or move `m` lines containing `pattern`
255 031e20a5 2019-04-19 hi :g/<em>pattern</em>/t$
256 031e20a5 2019-04-19 hi :g/<em>pattern</em>/m$
259 031e20a5 2019-04-19 hi ## Select a column of a table
260 031e20a5 2019-04-19 hi Select 3rd column separated by colon (`:`)
263 031e20a5 2019-04-19 hi :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
266 031e20a5 2019-04-19 hi ## Insert the sum of a list of numbers after an arbitrary number of lines
268 70b3c166 2019-04-20 hi (1) mark the first line: `mk`
269 70b3c166 2019-04-20 hi (2) mark the last line: `ml`
272 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
278 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
281 031e20a5 2019-04-19 hi ## Email the block
284 031e20a5 2019-04-19 hi :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
287 031e20a5 2019-04-19 hi ## Enable and use `ex` history
289 ade8cac0 2019-04-27 git (1) Set `ESC` key to enable history or add to `~/.nexrc`:
292 c2580c10 2019-04-20 hi :set cedit=<u><CTRL-V><ESC></u>
295 c2580c10 2019-04-20 hi (2) Use it with:
298 031e20a5 2019-04-19 hi :<u><ESC></u>
302 ade8cac0 2019-04-27 git ## Integrate with tmux buffer
304 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.
307 ade8cac0 2019-04-27 git !'mtmux load-buffer -
310 ade8cac0 2019-04-27 git (2) paste text from tmux buffer into vi buffer.
313 ade8cac0 2019-04-27 git :r!tmux show-buffer
316 ee74a164 2019-06-05 git (3) Map in ~/.nexrc (command mode)
319 ade8cac0 2019-04-27 git map gx !'mtmux load-buffer -<u><CTRL-V><ENTER></u>
320 ade8cac0 2019-04-27 git map gy !'mtmux load-buffer -<u><CTRL-V><ENTER></u>u
321 ade8cac0 2019-04-27 git map gp :r!tmux show-buffer<u><CTRL-V><ENTER></u>
325 ee74a164 2019-06-05 git ## Remap ESC to ALT-i in ~/.nexrc (insert mode)
328 9e2dabb0 2019-04-27 git map! <u><CTRL-V><ALT-i></u> <u><CTRL-V><ESC></u>