why-vi.rocks/index.md

258 lines
3.8 KiB
Markdown

<p class="f7">
<a href="/suggest.html">suggestion box</a> &ndash;
<a href="https://git.high5.nl/why-vi.rocks/log/">latest additions</a> &ndash;
<a href="/also.html">see also</a> &ndash;
<a href="/colophon.html">colophon</a>
</p>
<img class="mt4" src="vi-sw.png">
_[vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
text editor in any Unix-like operating system._
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>
Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
## Yank/delete lines
(1) mark the first line: `mk`<br>
(2) move to last line<br>
(3a) yank: `y'k`<br>
(3b) delete: `d'k`<br>
(4) move to destination line<br>
(5) put with `P` or `p`<br>
## Apply regex to lines
(1) mark the first line: `mk`<br>
(2) mark the last line: `ml`<br>
<pre>
:'k,'ls/<em>regex</em>/<em>power</em>/g
</pre>
## Increment / Decrement number in command mode
(1) move cursor to number<br>
(2a) increment by one: #+<br>
(2b) increment by N (5): 5#+<br>
(3a) decrement by one: #-<br>
(3b) decrement by N (9): 9#-
## Add &#35; to a block
<pre>
:'k,'ls/^/#/
</pre>
## Remove trailing whitespace from a block
:'k,'ls/\ *$//
## Remove the first N-characters from every line
N = 5
<pre>
:%s/^.\{0,<em>5</em>\}//
</pre>
## Delete all lines N-character long
N = 10
<pre>
:g/^.\{<em>10</em>\}$/d
</pre>
## Delete all lines _except_ N-character long
N = 10
<pre>
:g!/^.\{<em>10</em>\}$/d
</pre>
## Search/replace paths using &#35; as delimiter
<pre>
:%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
</pre>
## Write the file as root
:w !doas tee %
## Diff the file on disk with the buffer
:w !diff -u % -
## Make a backup of the file on disk
:!cp % %.bak
## Sort all lines
:%!sort
## Sort a block
`}` won't be shown
<pre>
!<u>}</u>sort
</pre>
## Sort from the current line to EOF
`G` won't be shown
<pre>
!<u>G</u>sort
</pre>
## Delete duplicated lines in the file
:%!uniq
## Delete duplicated lines in the block
`}` won't be shown
<pre>
!<u>}</u>uniq
</pre>
## Delete duplicated lines till EOF
`G` won't be shown
<pre>
!<u>G</u>uniq
</pre>
## Underline all lines starting with `pattern`
<pre>
:g/^<em>pattern</em> /t.|s/./=/g
</pre>
## Search for `pattern`, print the containing function (start with `def`) and line number
<pre>
:g/<em>pattern</em>/?^ *<em>def</em> ?#
</pre>
## Add &#35; to paragraph containing `pattern`
<pre>
:g/<em>pattern</em>/?^$?+,//-s/^/#
</pre>
## Sort content of a multiline CSS block
:g/{$/+,/^}/-!sort
## Sort content of a multiline CSS block (media queries)
:g/^[^@].*{$/+,/}/-!sort
## Format content of `<p>` tag to fixed width
width = 40
<pre>
:g/&lt;p&gt;/+,/&lt;\/p&gt;/-!fmt -<em>40</em>
</pre>
## Reverse all lines, move `m` all lines to 0
:g/1*/m0
## Swap `Lastname, Firstname` to `Firstname, Lastname`
:%s/\(.*\), \(.*\)/\2 \1/
## Convert to lowercase
:%s/.*/\L&/
## Join all lines
:%j
## Copy `t` or move `m` lines containing `pattern`
<pre>
:g/<em>pattern</em>/t$
:g/<em>pattern</em>/m$
</pre>
## Select a column of a table
Select 3rd column separated by colon (`:`)
<pre>
:%!awk -F'<em>:</em>' '{print $<em>3</em>}'
</pre>
## Insert the sum of a list of numbers after an arbitrary number of lines
(1) mark the first line: `mk`
(2) mark the last line: `ml`
<pre>
:'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
</pre>
or
<pre>
:'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
</pre>
## Email the block
<pre>
:?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
</pre>
## Enable and use `ex` history
(1) Set `ESC` key to enable history or add to `~/.exrc`:
<pre>
:set cedit=<u>&lt;CTRL-V&gt;&lt;ESC&gt;</u>
</pre>
(2) Use it with:
<pre>
:<u>&lt;ESC&gt;</u>
</pre>