Blame


1 d739a660 2019-04-06 git # Why vi Rocks
2 18871106 2019-04-06 git
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.
5 18871106 2019-04-06 git
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).
12 7b150fff 2019-04-07 git
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)
19 af599e02 2019-04-06 git
20 6c9b71bc 2019-04-06 git #### Yank / delete an arbitrary number of lines
21 18871106 2019-04-06 git
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
28 18871106 2019-04-06 git
29 6c9b71bc 2019-04-06 git #### Apply regex to an arbitrary number of lines
30 18871106 2019-04-06 git
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
34 18871106 2019-04-06 git
35 66a0e66d 2019-04-06 git #### Add # to block of text
36 66a0e66d 2019-04-06 git
37 e5f8bb90 2019-04-08 git :'k,'ls/^/#/
38 66a0e66d 2019-04-06 git
39 66a0e66d 2019-04-06 git #### Remove trailing whitespace of block of text
40 66a0e66d 2019-04-06 git
41 66a0e66d 2019-04-06 git :'k,'ls/\ *$//
42 66a0e66d 2019-04-06 git
43 a070b304 2019-04-08 git #### Search and replace PATHs, using different delimiter
44 a070b304 2019-04-08 git
45 a070b304 2019-04-08 git :%s#/usr/local/log#/var/log#g
46 a070b304 2019-04-08 git
47 6c9b71bc 2019-04-06 git #### Write file as root
48 18871106 2019-04-06 git
49 18871106 2019-04-06 git :w !doas tee %
50 18871106 2019-04-06 git
51 ee594e64 2019-04-06 git #### Diff the file on disk with the file in the buffer
52 18871106 2019-04-06 git
53 18871106 2019-04-06 git :w !diff -u % -
54 18871106 2019-04-06 git
55 18871106 2019-04-06 git #### Make a backup of the file on disk
56 18871106 2019-04-06 git
57 6c9b71bc 2019-04-06 git :!cp % %.bak
58 18871106 2019-04-06 git
59 6c9b71bc 2019-04-06 git #### Sort all lines
60 18871106 2019-04-06 git
61 18871106 2019-04-06 git :%!sort
62 18871106 2019-04-06 git
63 6c9b71bc 2019-04-06 git #### Sort paragraph
64 18871106 2019-04-06 git
65 6c9b71bc 2019-04-06 git !}sort
66 6c9b71bc 2019-04-06 git
67 f931c46b 2019-04-07 git <sub><sup>} won't be shown in the command.</sup></sub>
68 f931c46b 2019-04-07 git
69 9ad69938 2019-04-07 git #### Sort from current line to EOF
70 9ad69938 2019-04-07 git
71 9ad69938 2019-04-07 git !Gsort
72 9ad69938 2019-04-07 git
73 9ad69938 2019-04-07 git <sub><sup>G won't be shown in the command.</sup></sub>
74 9ad69938 2019-04-07 git
75 6c9b71bc 2019-04-06 git #### Uniq all lines
76 6c9b71bc 2019-04-06 git
77 18871106 2019-04-06 git :%!uniq
78 18871106 2019-04-06 git
79 6c9b71bc 2019-04-06 git #### Uniq paragraph
80 18871106 2019-04-06 git
81 6c9b71bc 2019-04-06 git !}uniq
82 6c9b71bc 2019-04-06 git
83 f931c46b 2019-04-07 git <sub><sup>} won't be shown in the command.</sup></sub>
84 f931c46b 2019-04-07 git
85 9ad69938 2019-04-07 git #### Uniq from current line to EOF
86 9ad69938 2019-04-07 git
87 9ad69938 2019-04-07 git !Guniq
88 9ad69938 2019-04-07 git
89 9ad69938 2019-04-07 git <sub><sup>G won't be shown in the command.</sup></sub>
90 9ad69938 2019-04-07 git
91 9541feb7 2019-04-08 git #### Underline all lines starting with CHAPTER
92 9541feb7 2019-04-08 git
93 9541feb7 2019-04-08 git :g/^CHAPTER /t.|s/./=/g
94 9541feb7 2019-04-08 git
95 9541feb7 2019-04-08 git #### Search for "pattern", print the containing function (start with def) and line number
96 9541feb7 2019-04-08 git
97 9541feb7 2019-04-08 git :g/pattern/?^ *def ?#
98 9541feb7 2019-04-08 git
99 9541feb7 2019-04-08 git #### Add # to paragraph containing "pattern"
100 9541feb7 2019-04-08 git
101 9541feb7 2019-04-08 git :g/pattern/?^$?+,//-s/^/#
102 9541feb7 2019-04-08 git
103 9541feb7 2019-04-08 git #### Sort content of multiline CSS blocks
104 9541feb7 2019-04-08 git
105 9541feb7 2019-04-08 git :g/{$/+,/^}/-!sort
106 9541feb7 2019-04-08 git
107 9541feb7 2019-04-08 git #### Sort content of multiline CSS blocks (media queries)
108 9541feb7 2019-04-08 git
109 9541feb7 2019-04-08 git :g/^[^@].*{$/+,/}/-!sort
110 9541feb7 2019-04-08 git
111 9541feb7 2019-04-08 git #### Reformat HTML paragraphs to a fixed width (40)
112 9541feb7 2019-04-08 git
113 9541feb7 2019-04-08 git :g/<p>/+,/<\/p>/-!fmt -40
114 9541feb7 2019-04-08 git
115 9541feb7 2019-04-08 git #### Swap "Lastname, Firstname" to "Firstname, Lastname"
116 9541feb7 2019-04-08 git
117 0bb4466d 2019-04-08 git :%s/\(.*\), \(.*\)/\2 \1/
118 9541feb7 2019-04-08 git
119 6c9b71bc 2019-04-06 git #### Join all lines
120 6c9b71bc 2019-04-06 git
121 6c9b71bc 2019-04-06 git :%j
122 6c9b71bc 2019-04-06 git
123 9541feb7 2019-04-08 git #### Copy (t) or move (m) lines containing "pattern"
124 9541feb7 2019-04-08 git
125 9541feb7 2019-04-08 git :g/pattern/t$
126 9541feb7 2019-04-08 git :g/pattern/m$
127 9541feb7 2019-04-08 git
128 6c9b71bc 2019-04-06 git #### Select a column (3rd) from formated text seperated by ':'
129 6c9b71bc 2019-04-06 git
130 6c9b71bc 2019-04-06 git :%!awk -F':' '{print $3}'
131 6c9b71bc 2019-04-06 git
132 9541feb7 2019-04-08 git #### Insert the sum of a list of numbers after an arbitrary number of lines
133 6c9b71bc 2019-04-06 git
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}'
137 9541feb7 2019-04-08 git
138 9541feb7 2019-04-08 git More compact version:
139 9541feb7 2019-04-08 git :'k,'l!awk 'END{print "Total:", i} ++i || 1'
140 9541feb7 2019-04-08 git
141 9541feb7 2019-04-08 git #### Email the current paragraph
142 9541feb7 2019-04-08 git
143 9541feb7 2019-04-08 git :?^$?+,//-w !mail -s "<subject>" email@example.com