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 031e20a5 2019-04-19 hi <img src="vi128.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._
14 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>
15 031e20a5 2019-04-19 hi Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
18 031e20a5 2019-04-19 hi ## Yank/delete lines
20 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
21 031e20a5 2019-04-19 hi (2) move to last line<br>
22 031e20a5 2019-04-19 hi (3a) yank: `y'k`<br>
23 031e20a5 2019-04-19 hi (3b) delete: `d'k`<br>
24 031e20a5 2019-04-19 hi (4) move to destination line<br>
25 031e20a5 2019-04-19 hi (5) put with `P` or `p`<br>
28 031e20a5 2019-04-19 hi ## Apply regex to lines
30 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
31 031e20a5 2019-04-19 hi (2) mark the last line: `ml`<br>
34 031e20a5 2019-04-19 hi :'k,'ls/<em>regex</em>/<em>power</em>/g
38 031e20a5 2019-04-19 hi ## Add # to a block
45 031e20a5 2019-04-19 hi ## Remove trailing whitespace from a block
47 66a0e66d 2019-04-06 git :'k,'ls/\ *$//
50 031e20a5 2019-04-19 hi ## Remove the first N characters from every line
55 031e20a5 2019-04-19 hi :%s/^.\{0,<em>5</em>\}//
59 031e20a5 2019-04-19 hi ## Delete all lines N-character long
64 031e20a5 2019-04-19 hi :g/^.\{<em>10</em>\}$/d
68 031e20a5 2019-04-19 hi ## Delete all lines _except_ N-character long
73 031e20a5 2019-04-19 hi :g!/^.\{<em>10</em>\}$/d
77 031e20a5 2019-04-19 hi ## Search/replace paths using # as delimiter
80 031e20a5 2019-04-19 hi :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
84 031e20a5 2019-04-19 hi ## Write the file as root
86 18871106 2019-04-06 git :w !doas tee %
89 031e20a5 2019-04-19 hi ## Diff the file on disk with the buffer
91 18871106 2019-04-06 git :w !diff -u % -
94 031e20a5 2019-04-19 hi ## Make a backup of the file on disk
99 031e20a5 2019-04-19 hi ## Sort all lines
104 031e20a5 2019-04-19 hi ## Sort a block
106 031e20a5 2019-04-19 hi `}` won't be shown
109 031e20a5 2019-04-19 hi <u>!</u>}sort
113 031e20a5 2019-04-19 hi ## Sort from the current line to EOF
115 031e20a5 2019-04-19 hi `G` won't be shown
118 031e20a5 2019-04-19 hi !<u>G</u>sort
122 031e20a5 2019-04-19 hi ## Delete duplicated lines in the file
126 031e20a5 2019-04-19 hi ## Delete duplicated lines in the block
128 031e20a5 2019-04-19 hi `}` won't be shown
131 031e20a5 2019-04-19 hi !<u>}</u>uniq
134 031e20a5 2019-04-19 hi ## Delete duplicated lines till EOF
136 031e20a5 2019-04-19 hi `G` won't be shown
139 031e20a5 2019-04-19 hi !<u>G</u>uniq
143 031e20a5 2019-04-19 hi ## Underline all lines starting with `pattern`
146 031e20a5 2019-04-19 hi :g/^<em>pattern</em> /t.|s/./=/g
150 031e20a5 2019-04-19 hi ## Search for `pattern`, print the containing function (start with `def`) and line number
153 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^ *<em>def</em> ?#
157 031e20a5 2019-04-19 hi ## Add # to paragraph containing `pattern`
160 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^$?+,//-s/^/#
164 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block
166 9541feb7 2019-04-08 git :g/{$/+,/^}/-!sort
169 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block (media queries)
171 9541feb7 2019-04-08 git :g/^[^@].*{$/+,/}/-!sort
174 031e20a5 2019-04-19 hi ## Format content of `<p>` tag to fixed width
179 031e20a5 2019-04-19 hi :g/<p>/+,/<\/p>/-!fmt -<em>40</em>
183 031e20a5 2019-04-19 hi ## Reverse all lines, move `m` all lines to 0
188 031e20a5 2019-04-19 hi ## Swap `Lastname, Firstname` to `Firstname, Lastname`
190 0bb4466d 2019-04-08 git :%s/\(.*\), \(.*\)/\2 \1/
193 031e20a5 2019-04-19 hi ## Convert to lowercase
198 031e20a5 2019-04-19 hi ## Join all lines
202 031e20a5 2019-04-19 hi ## Copy `t` or move `m` lines containing `pattern`
205 031e20a5 2019-04-19 hi :g/<em>pattern</em>/t$
206 031e20a5 2019-04-19 hi :g/<em>pattern</em>/m$
209 031e20a5 2019-04-19 hi ## Select a column of a table
210 031e20a5 2019-04-19 hi Select 3rd column separated by colon (`:`)
213 031e20a5 2019-04-19 hi :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
216 031e20a5 2019-04-19 hi ## Insert the sum of a list of numbers after an arbitrary number of lines
218 031e20a5 2019-04-19 hi 1) mark the first line: `mk`
219 031e20a5 2019-04-19 hi 2) mark the last line: `ml`
222 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
228 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
231 031e20a5 2019-04-19 hi ## Email the block
234 031e20a5 2019-04-19 hi :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
237 031e20a5 2019-04-19 hi ## Enable and use `ex` history
240 031e20a5 2019-04-19 hi 1) Set `ESC` key to enable history or add to `~/.exrc`:
241 031e20a5 2019-04-19 hi <b>:set cedit=<u><CTRL-V><ESC></u></b>
243 031e20a5 2019-04-19 hi 2) Use it with:
244 031e20a5 2019-04-19 hi :<u><ESC></u>