332 lines
5.0 KiB
Markdown
332 lines
5.0 KiB
Markdown
<p class="f7">
|
|
<a href="/suggest.html">suggestion box</a> –
|
|
<a href="https://git.high5.nl/why-vi.rocks/log/">latest additions</a> –
|
|
<a href="/also.html">see also</a> –
|
|
<a href="/colophon.html">colophon</a>
|
|
</p>
|
|
|
|
<div class="logo-container">
|
|
<img id="logo" class="mt4" src="vi-sw.png" alt="Logo">
|
|
</div>
|
|
|
|
_Why? [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).
|
|
|
|
[Bill Joy's greatest gift to man - the vi editor](https://www.theregister.co.uk/2003/09/11/bill_joys_greatest_gift/)
|
|
|
|
|
|
## 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 # to a block
|
|
|
|
<pre>
|
|
:'k,'ls/^/#/
|
|
</pre>
|
|
|
|
|
|
## Remove trailing whitespace from every line
|
|
|
|
<pre>
|
|
:%s/ *$//
|
|
</pre>
|
|
|
|
|
|
## Remove tabs
|
|
|
|
<pre>
|
|
:%s/<u><CTRL-V><TAB></u>//g
|
|
</pre>
|
|
|
|
|
|
## Remove trailing whitespace from a block
|
|
|
|
<pre>
|
|
:'k,'ls/\ *$//
|
|
</pre>
|
|
|
|
|
|
## 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 # as delimiter
|
|
|
|
<pre>
|
|
:%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
|
|
</pre>
|
|
|
|
|
|
## Search/replace ^M with LF
|
|
|
|
<pre>
|
|
:g/<u><CTRL-V><ENTER></u>/s///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 # 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/<p>/+,/<\/p>/-!fmt -<em>40</em>
|
|
</pre>
|
|
|
|
|
|
## Format whole document
|
|
|
|
:%!fmt -s
|
|
|
|
In your .nexrc
|
|
|
|
<pre>
|
|
map gF :%!fmt -s<u><CTRL-V><ENTER></u>
|
|
</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&/
|
|
|
|
|
|
## Surround text with pattern
|
|
|
|
:%s/.*/`pattern` & `pattern`/
|
|
|
|
|
|
## 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 `~/.nexrc`:
|
|
|
|
<pre>
|
|
:set cedit=<u><CTRL-V><ESC></u>
|
|
</pre>
|
|
|
|
(2) Use it with:
|
|
|
|
<pre>
|
|
:<u><ESC></u>
|
|
</pre>
|
|
|
|
|
|
## Integrate with tmux buffer
|
|
|
|
(1) cut text from current position to mark 'm' into tmux buffer. Hit undo to put text back into vi buffer.
|
|
|
|
<pre>
|
|
!'mtmux load-buffer -
|
|
</pre>
|
|
|
|
(2) paste text from tmux buffer into vi buffer.
|
|
|
|
<pre>
|
|
:r!tmux show-buffer
|
|
</pre>
|
|
|
|
(3) Map in ~/.nexrc (command mode)
|
|
|
|
<pre>
|
|
map gx !'mtmux load-buffer -<u><CTRL-V><ENTER></u>
|
|
map gy !'mtmux load-buffer -<u><CTRL-V><ENTER></u>u
|
|
map gp :r!tmux show-buffer<u><CTRL-V><ENTER></u>
|
|
</pre>
|
|
|
|
|
|
## Remap ESC to ALT-i in ~/.nexrc (insert mode)
|
|
|
|
<pre>
|
|
map! <u><CTRL-V><ALT-i></u> <u><CTRL-V><ESC></u>
|
|
</pre>
|
|
|
|
|