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).
16 [Bill Joy's greatest gift to man - the vi editor](https://www.theregister.co.uk/2003/09/11/bill_joys_greatest_gift/)
21 (1) mark the first line: `mk`<br>
22 (2) move to last line<br>
24 (3b) delete: `d'k`<br>
25 (4) move to destination line<br>
26 (5) put with `P` or `p`<br>
29 ## Apply regex to lines
31 (1) mark the first line: `mk`<br>
32 (2) mark the last line: `ml`<br>
35 :'k,'ls/<em>regex</em>/<em>power</em>/g
39 ## Increment / Decrement number in command mode
41 (1) move cursor to number<br>
42 (2a) increment by one: #+<br>
43 (2b) increment by N (5): 5#+<br>
44 (3a) decrement by one: #-<br>
45 (3b) decrement by N (9): 9#-
48 ## Add # to a block
55 ## Remove trailing whitespace from every line
65 :%s/<u><CTRL-V><TAB></u>//g
69 ## Remove trailing whitespace from a block
76 ## Remove the first N-characters from every line
81 :%s/^.\{0,<em>5</em>\}//
85 ## Delete all lines N-character long
90 :g/^.\{<em>10</em>\}$/d
94 ## Delete all lines _except_ N-character long
99 :g!/^.\{<em>10</em>\}$/d
103 ## Search/replace paths using # as delimiter
106 :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
110 ## Search/replace ^M with LF
113 :g/<u><CTRL-V><ENTER></u>/s///g
117 ## Write the file as root
122 ## Diff the file on disk with the buffer
127 ## Make a backup of the file on disk
146 ## Sort from the current line to EOF
155 ## Delete duplicated lines in the file
159 ## Delete duplicated lines in the block
167 ## Delete duplicated lines till EOF
176 ## Underline all lines starting with `pattern`
179 :g/^<em>pattern</em> /t.|s/./=/g
183 ## Search for `pattern`, print the containing function (start with `def`) and line number
186 :g/<em>pattern</em>/?^ *<em>def</em> ?#
190 ## Add # to paragraph containing `pattern`
193 :g/<em>pattern</em>/?^$?+,//-s/^/#
197 ## Sort content of a multiline CSS block
202 ## Sort content of a multiline CSS block (media queries)
204 :g/^[^@].*{$/+,/}/-!sort
207 ## Format content of `<p>` tag to fixed width
212 :g/<p>/+,/<\/p>/-!fmt -<em>40</em>
216 ## Format whole document
223 map gF :%!fmt -s<u><CTRL-V><ENTER></u>
226 ## Reverse all lines, move `m` all lines to 0
231 ## Swap `Lastname, Firstname` to `Firstname, Lastname`
233 :%s/\(.*\), \(.*\)/\2 \1/
236 ## Convert to lowercase
241 ## Surround text with pattern
243 :%s/.*/`pattern` & `pattern`/
250 ## Copy `t` or move `m` lines containing `pattern`
253 :g/<em>pattern</em>/t$
254 :g/<em>pattern</em>/m$
257 ## Select a column of a table
258 Select 3rd column separated by colon (`:`)
261 :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
264 ## Insert the sum of a list of numbers after an arbitrary number of lines
266 (1) mark the first line: `mk`
267 (2) mark the last line: `ml`
270 :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
276 :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
282 :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
285 ## Enable and use `ex` history
287 (1) Set `ESC` key to enable history or add to `~/.nexrc`:
290 :set cedit=<u><CTRL-V><ESC></u>
300 ## Integrate with tmux buffer
302 (1) cut text from current position to mark 'm' into tmux buffer. Hit undo to put text back into vi buffer.
305 !'mtmux load-buffer -
308 (2) paste text from tmux buffer into vi buffer.
314 (3) Map in ~/.nexrc (command mode)
317 map gx !'mtmux load-buffer -<u><CTRL-V><ENTER></u>
318 map gy !'mtmux load-buffer -<u><CTRL-V><ENTER></u>u
319 map gp :r!tmux show-buffer<u><CTRL-V><ENTER></u>
323 ## Remap ESC to ALT-i in ~/.nexrc (insert mode)
326 map! <u><CTRL-V><ALT-i></u> <u><CTRL-V><ESC></u>