2 <a href="/suggest.html">suggestion box</a> –
3 <a href="https://got.high5.nl/?action=summary&path=why-vi.rocks.git">latest additions (got)</a> –
4 <a href="/also.html">see also</a> –
5 <a href="/colophon.html">colophon</a>
8 <div class="logo-container">
9 <img id="logo" class="mt4" src="vi-sw.png" alt="Logo">
12 _Why? [vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
13 text editor in any Unix-like operating system._
15 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 Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
18 [Bill Joy's greatest gift to man - the vi editor](https://www.theregister.co.uk/2003/09/11/bill_joys_greatest_gift/)
23 (1) mark the first line: `mk`<br>
24 (2) move to last line<br>
26 (3b) delete: `d'k`<br>
27 (4) move to destination line<br>
28 (5) put with `P` or `p`<br>
31 ## Apply regex to lines
33 (1) mark the first line: `mk`<br>
34 (2) mark the last line: `ml`<br>
37 :'k,'ls/<em>regex</em>/<em>power</em>/g
41 ## Increment / Decrement number in command mode
43 (1) move cursor to number<br>
44 (2a) increment by one: #+<br>
45 (2b) increment by N (5): 5#+<br>
46 (3a) decrement by one: #-<br>
47 (3b) decrement by N (9): 9#-
50 ## Add # to a block
57 ## Remove trailing whitespace from every line
67 :%s/<u><CTRL-V><TAB></u>//g
71 ## Remove trailing whitespace from a block
78 ## Remove the first N-characters from every line
83 :%s/^.\{0,<em>5</em>\}//
87 ## Delete all lines N-character long
92 :g/^.\{<em>10</em>\}$/d
96 ## Delete all lines _except_ N-character long
101 :g!/^.\{<em>10</em>\}$/d
105 ## Search/replace paths using # as delimiter
108 :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
112 ## Search/replace ^M with LF
115 :g/<u><CTRL-V><ENTER></u>/s///g
119 ## Write the file as root
124 ## Diff the file on disk with the buffer
129 ## Make a backup of the file on disk
148 ## Sort from the current line to EOF
157 ## Delete duplicated lines in the file
161 ## Delete duplicated lines in the block
169 ## Delete duplicated lines till EOF
178 ## Underline all lines starting with `pattern`
181 :g/^<em>pattern</em> /t.|s/./=/g
185 ## Search for `pattern`, print the containing function (start with `def`) and line number
188 :g/<em>pattern</em>/?^ *<em>def</em> ?#
192 ## Add # to paragraph containing `pattern`
195 :g/<em>pattern</em>/?^$?+,//-s/^/#
199 ## Sort content of a multiline CSS block
204 ## Sort content of a multiline CSS block (media queries)
206 :g/^[^@].*{$/+,/}/-!sort
209 ## Format content of `<p>` tag to fixed width
214 :g/<p>/+,/<\/p>/-!fmt -<em>40</em>
218 ## Format whole document
225 map gF :%!fmt -s<u><CTRL-V><ENTER></u>
228 ## Reverse all lines, move `m` all lines to 0
233 ## Swap `Lastname, Firstname` to `Firstname, Lastname`
235 :%s/\(.*\), \(.*\)/\2 \1/
238 ## Convert to lowercase
243 ## Surround text with pattern
245 :%s/.*/`pattern` & `pattern`/
252 ## Copy `t` or move `m` lines containing `pattern`
255 :g/<em>pattern</em>/t$
256 :g/<em>pattern</em>/m$
259 ## Select a column of a table
260 Select 3rd column separated by colon (`:`)
263 :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
266 ## Insert the sum of a list of numbers after an arbitrary number of lines
268 (1) mark the first line: `mk`
269 (2) mark the last line: `ml`
272 :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
278 :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
284 :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
287 ## Enable and use `ex` history
289 (1) Set `ESC` key to enable history or add to `~/.nexrc`:
292 :set cedit=<u><CTRL-V><ESC></u>
302 ## Integrate with tmux buffer
304 (1) cut text from current position to mark 'm' into tmux buffer. Hit undo to put text back into vi buffer.
307 !'mtmux load-buffer -
310 (2) paste text from tmux buffer into vi buffer.
316 (3) Map in ~/.nexrc (command mode)
319 map gx !'mtmux load-buffer -<u><CTRL-V><ENTER></u>
320 map gy !'mtmux load-buffer -<u><CTRL-V><ENTER></u>u
321 map gp :r!tmux show-buffer<u><CTRL-V><ENTER></u>
325 ## Remap ESC to ALT-i in ~/.nexrc (insert mode)
328 map! <u><CTRL-V><ALT-i></u> <u><CTRL-V><ESC></u>