2 <a href="/suggest.html">suggestion box</a> –
3 <a href="https://git.high5.nl/why-vi.rocks/log/">latest additions</a> –
4 <a href="/also.html">see also</a> –
5 <a href="/colophon.html">colophon</a>
8 <img class="mt4" src="vi-sw.png">
10 _[vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
11 text editor in any Unix-like operating system._
13 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 Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
19 (1) mark the first line: `mk`<br>
20 (2) move to last line<br>
22 (3b) delete: `d'k`<br>
23 (4) move to destination line<br>
24 (5) put with `P` or `p`<br>
27 ## Apply regex to lines
29 (1) mark the first line: `mk`<br>
30 (2) mark the last line: `ml`<br>
33 :'k,'ls/<em>regex</em>/<em>power</em>/g
37 ## Add # to a block
44 ## Remove trailing whitespace from a block
49 ## Remove the first N characters from every line
54 :%s/^.\{0,<em>5</em>\}//
58 ## Delete all lines N-character long
63 :g/^.\{<em>10</em>\}$/d
67 ## Delete all lines _except_ N-character long
72 :g!/^.\{<em>10</em>\}$/d
76 ## Search/replace paths using # as delimiter
79 :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
83 ## Write the file as root
88 ## Diff the file on disk with the buffer
93 ## Make a backup of the file on disk
112 ## Sort from the current line to EOF
121 ## Delete duplicated lines in the file
125 ## Delete duplicated lines in the block
133 ## Delete duplicated lines till EOF
142 ## Underline all lines starting with `pattern`
145 :g/^<em>pattern</em> /t.|s/./=/g
149 ## Search for `pattern`, print the containing function (start with `def`) and line number
152 :g/<em>pattern</em>/?^ *<em>def</em> ?#
156 ## Add # to paragraph containing `pattern`
159 :g/<em>pattern</em>/?^$?+,//-s/^/#
163 ## Sort content of a multiline CSS block
168 ## Sort content of a multiline CSS block (media queries)
170 :g/^[^@].*{$/+,/}/-!sort
173 ## Format content of `<p>` tag to fixed width
178 :g/<p>/+,/<\/p>/-!fmt -<em>40</em>
182 ## Reverse all lines, move `m` all lines to 0
187 ## Swap `Lastname, Firstname` to `Firstname, Lastname`
189 :%s/\(.*\), \(.*\)/\2 \1/
192 ## Convert to lowercase
201 ## Copy `t` or move `m` lines containing `pattern`
204 :g/<em>pattern</em>/t$
205 :g/<em>pattern</em>/m$
208 ## Select a column of a table
209 Select 3rd column separated by colon (`:`)
212 :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
215 ## Insert the sum of a list of numbers after an arbitrary number of lines
217 1) mark the first line: `mk`
218 2) mark the last line: `ml`
221 :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
227 :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
233 :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
236 ## Enable and use `ex` history
239 1) Set `ESC` key to enable history or add to `~/.exrc`:
240 <b>:set cedit=<u><CTRL-V><ESC></u></b>