suggestion box – latest additions – see also – colophon
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
(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
(1) mark the first line: mk
(2) mark the last line: ml
:'k,'ls/regex/power/g
(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#-
:'k,'ls/^/#/
:%s/ *$//
:%s/<CTRL-V><TAB>//g
:'k,'ls/\ *$//
N = 5
:%s/^.\{0,5\}//
N = 10
:g/^.\{10\}$/d
N = 10
:g!/^.\{10\}$/d
:%s#/usr/local/log#/var/log#g
:g/<CTRL-V><ENTER>/s///g
:w !doas tee %
:w !diff -u % -
:!cp % %.bak
:%!sort
}
won’t be shown
!}sort
G
won’t be shown
!Gsort
:%!uniq
}
won’t be shown
!}uniq
G
won’t be shown
!Guniq
pattern
:g/^pattern /t.|s/./=/g
pattern
, print the containing function (start with def
) and line number:g/pattern/?^ *def ?#
pattern
:g/pattern/?^$?+,//-s/^/#
:g/{$/+,/^}/-!sort
:g/^[^@].*{$/+,/}/-!sort
<p>
tag to fixed widthwidth = 40
:g/<p>/+,/<\/p>/-!fmt -40
:%!fmt -s
In your .nexrc
map gF :%!fmt -s<CTRL-V><ENTER>
m
all lines to 0:g/1*/m0
Lastname, Firstname
to Firstname, Lastname
:%s/\(.*\), \(.*\)/\2 \1/
:%s/.*/\L&/
:%s/.*/`pattern` & `pattern`/
:%j
t
or move m
lines containing pattern
:g/pattern/t$ :g/pattern/m$
Select 3rd column separated by colon (:
)
:%!awk -F':' '{print $3}'
(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'
:?^$?+,//-w !mail -s "subject" email@example.com
ex
history(1) Set ESC
key to enable history or add to ~/.nexrc
:
:set cedit=<CTRL-V><ESC>
(2) Use it with:
:<ESC>
(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>
map! <CTRL-V><ALT-i> <CTRL-V><ESC>