3 **[vi](https://en.wikipedia.org/wiki/Vi)** is the _de facto_ standard
4 Unix editor, you find it in every *NIX derived OS.
6 Here you will find a collection of commands, command sequences in
7 [vi(1)](https://man.openbsd.org/vi.1)/[ex(1)](https://man.openbsd.org/ex.1)
8 or with 3rd party unitilities which make
9 **[vi](https://en.wikipedia.org/wiki/Vi)** rock. These all work
10 with at least [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and
13 Helpful documents:<br />
14 - [Roman Zolotarev: Edit text with vi(1)](https://rgz.ee/vi.html)<br />
15 - [Jeff W: vi help](http://www.jeffw.com/vi/vi_help.txt)<br />
16 - [Maarten Litmaath: vi reference](http://www.ungerhu.com/jxh/vi.html)<br />
17 - [alphanrrrd: extremely concise cheatsheet](http://www.alphanrrrd.org/vi.html)<br />
18 - [ViEmu: Graphical vi cheatsheet](http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html)
20 #### Yank / delete an arbitrary number of lines
22 1) mark the first line: mk
26 4) move to destination line
29 #### Apply regex to an arbitrary number of lines
31 1) mark the first line: mk
32 2) mark the last line: ml
35 #### Add # to block of text
39 #### Remove trailing whitespace of block of text
43 #### Search and replace PATHs, using different delimiter
45 :%s#/usr/local/log#/var/log#g
47 #### Write file as root
51 #### Diff the file on disk with the file in the buffer
55 #### Make a backup of the file on disk
67 <sub><sup>} won't be shown in the command.</sup></sub>
69 #### Sort from current line to EOF
73 <sub><sup>G won't be shown in the command.</sup></sub>
83 <sub><sup>} won't be shown in the command.</sup></sub>
85 #### Uniq from current line to EOF
89 <sub><sup>G won't be shown in the command.</sup></sub>
91 #### Underline all lines starting with CHAPTER
93 :g/^CHAPTER /t.|s/./=/g
95 #### Search for "pattern", print the containing function (start with def) and line number
99 #### Add # to paragraph containing "pattern"
101 :g/pattern/?^$?+,//-s/^/#
103 #### Sort content of multiline CSS blocks
107 #### Sort content of multiline CSS blocks (media queries)
109 :g/^[^@].*{$/+,/}/-!sort
111 #### Reformat HTML paragraphs to a fixed width (40)
113 :g/<p>/+,/<\/p>/-!fmt -40
115 #### Swap "Lastname, Firstname" to "Firstname, Lastname"
117 :%s/\(.*\), \(.*\)/\2 \1/
123 #### Copy (t) or move (m) lines containing "pattern"
128 #### Select a column (3rd) from formated text seperated by ':'
130 :%!awk -F':' '{print $3}'
132 #### Insert the sum of a list of numbers after an arbitrary number of lines
134 1) mark the first line: mk
135 2) mark the last line: ml
136 :'k,'l!awk 'END{print "Total:", i}{i+=$1; print}'
138 More compact version:
139 :'k,'l!awk 'END{print "Total:", i} ++i || 1'
141 #### Email the current paragraph
143 :?^$?+,//-w !mail -s "<subject>" email@example.com