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