4.9 KiB
suggestion box – latest additions – see also – colophon
![](/High5/why-vi.rocks/media/commit/7f094e3daac5d81f7acaf668528437aafad5a82b/vi-sw.png)
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
:'k,'ls/regex/power/g
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
:'k,'ls/^/#/
Remove trailing whitespace from every line
:%s/ *$//
Remove tabs
:%s/<CTRL-V><TAB>//g
Remove trailing whitespace from a block
:'k,'ls/\ *$//
Remove the first N-characters from every line
N = 5
:%s/^.\{0,5\}//
Delete all lines N-character long
N = 10
:g/^.\{10\}$/d
Delete all lines except N-character long
N = 10
:g!/^.\{10\}$/d
Search/replace paths using # as delimiter
:%s#/usr/local/log#/var/log#g
Search/replace ^M with LF
:g/<CTRL-V><ENTER>/s///g
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
!}sort
Sort from the current line to EOF
G
won't be shown
!Gsort
Delete duplicated lines in the file
:%!uniq
Delete duplicated lines in the block
}
won't be shown
!}uniq
Delete duplicated lines till EOF
G
won't be shown
!Guniq
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
:g/pattern/?^$?+,//-s/^/#
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
: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
: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
:g/pattern/t$ :g/pattern/m$
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}'
or
:'k,'l!awk 'END{print "total:", i} ++i || 1'
Email the block
:?^$?+,//-w !mail -s "subject" email@example.com
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:
:<ESC>
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>