Blob


1 # Why vi Rocks
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
11 2.1.3 (unicode).
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
23 2) move to last line
24 3a) yank: y'k
25 3b) delete: d'k
26 4) move to destination line
27 5) put with P or p
29 #### Apply regex to an arbitrary number of lines
31 1) mark the first line: mk
32 2) mark the last line: ml
33 :'k,'ls/regex/power/g
35 #### Add # to block of text
37 :'k,'ls/^/#/
39 #### Remove trailing whitespace of block of text
41 :'k,'ls/\ *$//
43 #### Search and replace PATHs, using different delimiter
45 :%s#/usr/local/log#/var/log#g
47 #### Write file as root
49 :w !doas tee %
51 #### Diff the file on disk with the file in the buffer
53 :w !diff -u % -
55 #### Make a backup of the file on disk
57 :!cp % %.bak
59 #### Sort all lines
61 :%!sort
63 #### Sort paragraph
65 !}sort
67 <sub><sup>} won't be shown in the command.</sup></sub>
69 #### Sort from current line to EOF
71 !Gsort
73 <sub><sup>G won't be shown in the command.</sup></sub>
75 #### Uniq all lines
77 :%!uniq
79 #### Uniq paragraph
81 !}uniq
83 <sub><sup>} won't be shown in the command.</sup></sub>
85 #### Uniq from current line to EOF
87 !Guniq
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
97 :g/pattern/?^ *def ?#
99 #### Add # to paragraph containing "pattern"
101 :g/pattern/?^$?+,//-s/^/#
103 #### Sort content of multiline CSS blocks
105 :g/{$/+,/^}/-!sort
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/
119 #### Join all lines
121 :%j
123 #### Copy (t) or move (m) lines containing "pattern"
125 :g/pattern/t$
126 :g/pattern/m$
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