suggestion box – latest additions – see also – colophon
_[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.:'k,'ls/regex/power/g## Add # to a block
:'k,'ls/^/#/## 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## 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 `
` tag to fixed width width = 40
:g/<p>/+,/<\/p>/-!fmt -40## 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`
: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 `~/.exrc`: :set cedit=<CTRL-V><ESC> 2) Use it with: :<ESC>