suggestion boxlatest additionssee alsocolophon

Why? vi is the de facto standard text editor in any Unix-like operating system.

Here is a collection of vi(1)/ex(1) commands and command sequences.
Tested with nvi 1.79 and 2.1.3 (unicode).

Bill Joy’s greatest gift to man - the vi editor

Yank/delete lines

(1) mark the first line: mk
(2) move to last line
(3a) yank: y'k
(3b) delete: d'k
(4) move to destination line
(5) put with P or p

Apply regex to lines

(1) mark the first line: mk
(2) mark the last line: ml


Increment / Decrement number in command mode

(1) move cursor to number
(2a) increment by one: #+
(2b) increment by N (5): 5#+
(3a) decrement by one: #-
(3b) decrement by N (9): 9#-

Add # to a block


Remove trailing whitespace from every line

:%s/ *$//

Remove tabs


Remove trailing whitespace from a block

:'k,'ls/\ *$//

Remove the first N-characters from every line

N = 5


Delete all lines N-character long

N = 10


Delete all lines except N-character long

N = 10


Search/replace paths using # as delimiter


Search/replace ^M with LF


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 a block

} won’t be shown


Sort from the current line to EOF

G won’t be shown


Delete duplicated lines in the file


Delete duplicated lines in the block

} won’t be shown


Delete duplicated lines till EOF

G won’t be shown


Underline all lines starting with pattern

:g/^pattern /t.|s/./=/g

Search for pattern, print the containing function (start with def) and line number

:g/pattern/?^ *def ?#

Add # to paragraph containing pattern


Sort content of a multiline CSS block


Sort content of a multiline CSS block (media queries)


Format content of <p> tag to fixed width

width = 40

:g/<p>/+,/<\/p>/-!fmt -40

Format whole document

:%!fmt -s

In your .nexrc

map gF :%!fmt -s<CTRL-V><ENTER>

Reverse all lines, move m all lines to 0


Swap Lastname, Firstname to Firstname, Lastname

:%s/\(.*\), \(.*\)/\2 \1/

Convert to lowercase


Surround text with pattern

:%s/.*/`pattern` & `pattern`/

Join all lines


Copy t or move m lines containing pattern


Select a column of a table

Select 3rd column separated by colon (:)

:%!awk -F':' '{print $3}'

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

:'k,'l!awk 'END{print "total:", i}{i+=$1; print}'


:'k,'l!awk 'END{print "total:", i} ++i || 1'

Email the block

:?^$?+,//-w !mail -s "subject"

Enable and use ex history

(1) Set ESC key to enable history or add to ~/.nexrc:

:set cedit=<CTRL-V><ESC>

(2) Use it with:


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.

!'mtmux load-buffer -

(2) paste text from tmux buffer into vi buffer.

:r!tmux show-buffer

(3) Map in ~/.nexrc (command mode)

map gx !'mtmux load-buffer -<CTRL-V><ENTER>
map gy !'mtmux load-buffer -<CTRL-V><ENTER>u
map gp :r!tmux show-buffer<CTRL-V><ENTER>

Remap ESC to ALT-i in ~/.nexrc (insert mode)

map! <CTRL-V><ALT-i> <CTRL-V><ESC>