Blame


1 031e20a5 2019-04-19 hi <p class="f7">
2 031e20a5 2019-04-19 hi <a href="/suggest.html">suggestion box</a> &ndash;
3 031e20a5 2019-04-19 hi <a href="https://git.high5.nl/why-vi.rocks/log/">latest additions</a> &ndash;
4 031e20a5 2019-04-19 hi <a href="/also.html">see also</a> &ndash;
5 031e20a5 2019-04-19 hi <a href="/colophon.html">colophon</a>
6 031e20a5 2019-04-19 hi </p>
7 18871106 2019-04-06 git
8 031e20a5 2019-04-19 hi <img src="vi128.png">
9 18871106 2019-04-06 git
10 031e20a5 2019-04-19 hi _[vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
11 031e20a5 2019-04-19 hi text editor in any Unix-like operating system._
12 7b150fff 2019-04-07 git
13 449a4855 2019-04-15 git
14 031e20a5 2019-04-19 hi 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.<br>
15 031e20a5 2019-04-19 hi Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
16 7815de24 2019-04-10 git
17 af599e02 2019-04-06 git
18 031e20a5 2019-04-19 hi ## Yank/delete lines
19 18871106 2019-04-06 git
20 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
21 031e20a5 2019-04-19 hi (2) move to last line<br>
22 031e20a5 2019-04-19 hi (3a) yank: `y'k`<br>
23 031e20a5 2019-04-19 hi (3b) delete: `d'k`<br>
24 031e20a5 2019-04-19 hi (4) move to destination line<br>
25 031e20a5 2019-04-19 hi (5) put with `P` or `p`<br>
26 18871106 2019-04-06 git
27 18871106 2019-04-06 git
28 031e20a5 2019-04-19 hi ## Apply regex to lines
29 18871106 2019-04-06 git
30 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
31 031e20a5 2019-04-19 hi (2) mark the last line: `ml`<br>
32 66a0e66d 2019-04-06 git
33 031e20a5 2019-04-19 hi <pre>
34 031e20a5 2019-04-19 hi :'k,'ls/<em>regex</em>/<em>power</em>/g
35 031e20a5 2019-04-19 hi </pre>
36 66a0e66d 2019-04-06 git
37 66a0e66d 2019-04-06 git
38 031e20a5 2019-04-19 hi ## Add &#35; to a block
39 031e20a5 2019-04-19 hi
40 031e20a5 2019-04-19 hi <pre>
41 031e20a5 2019-04-19 hi :'k,'ls/^/#/
42 031e20a5 2019-04-19 hi </pre>
43 031e20a5 2019-04-19 hi
44 031e20a5 2019-04-19 hi
45 031e20a5 2019-04-19 hi ## Remove trailing whitespace from a block
46 031e20a5 2019-04-19 hi
47 66a0e66d 2019-04-06 git :'k,'ls/\ *$//
48 66a0e66d 2019-04-06 git
49 5717e66e 2019-04-15 git
50 031e20a5 2019-04-19 hi ## Remove the first N characters from every line
51 5717e66e 2019-04-15 git
52 031e20a5 2019-04-19 hi N = 5
53 f069fab6 2019-04-16 git
54 031e20a5 2019-04-19 hi <pre>
55 031e20a5 2019-04-19 hi :%s/^.\{0,<em>5</em>\}//
56 031e20a5 2019-04-19 hi </pre>
57 f069fab6 2019-04-16 git
58 f069fab6 2019-04-16 git
59 031e20a5 2019-04-19 hi ## Delete all lines N-character long
60 f069fab6 2019-04-16 git
61 031e20a5 2019-04-19 hi N = 10
62 a070b304 2019-04-08 git
63 031e20a5 2019-04-19 hi <pre>
64 031e20a5 2019-04-19 hi :g/^.\{<em>10</em>\}$/d
65 031e20a5 2019-04-19 hi </pre>
66 a070b304 2019-04-08 git
67 18871106 2019-04-06 git
68 031e20a5 2019-04-19 hi ## Delete all lines _except_ N-character long
69 031e20a5 2019-04-19 hi
70 031e20a5 2019-04-19 hi N = 10
71 031e20a5 2019-04-19 hi
72 031e20a5 2019-04-19 hi <pre>
73 031e20a5 2019-04-19 hi :g!/^.\{<em>10</em>\}$/d
74 031e20a5 2019-04-19 hi </pre>
75 031e20a5 2019-04-19 hi
76 031e20a5 2019-04-19 hi
77 031e20a5 2019-04-19 hi ## Search/replace paths using &#35; as delimiter
78 031e20a5 2019-04-19 hi
79 031e20a5 2019-04-19 hi <pre>
80 031e20a5 2019-04-19 hi :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
81 031e20a5 2019-04-19 hi </pre>
82 031e20a5 2019-04-19 hi
83 031e20a5 2019-04-19 hi
84 031e20a5 2019-04-19 hi ## Write the file as root
85 031e20a5 2019-04-19 hi
86 18871106 2019-04-06 git :w !doas tee %
87 18871106 2019-04-06 git
88 18871106 2019-04-06 git
89 031e20a5 2019-04-19 hi ## Diff the file on disk with the buffer
90 031e20a5 2019-04-19 hi
91 18871106 2019-04-06 git :w !diff -u % -
92 18871106 2019-04-06 git
93 18871106 2019-04-06 git
94 031e20a5 2019-04-19 hi ## Make a backup of the file on disk
95 031e20a5 2019-04-19 hi
96 6c9b71bc 2019-04-06 git :!cp % %.bak
97 18871106 2019-04-06 git
98 18871106 2019-04-06 git
99 031e20a5 2019-04-19 hi ## Sort all lines
100 031e20a5 2019-04-19 hi
101 18871106 2019-04-06 git :%!sort
102 18871106 2019-04-06 git
103 18871106 2019-04-06 git
104 031e20a5 2019-04-19 hi ## Sort a block
105 6c9b71bc 2019-04-06 git
106 031e20a5 2019-04-19 hi `}` won't be shown
107 f931c46b 2019-04-07 git
108 031e20a5 2019-04-19 hi <pre>
109 031e20a5 2019-04-19 hi <u>!</u>}sort
110 031e20a5 2019-04-19 hi </pre>
111 9ad69938 2019-04-07 git
112 9ad69938 2019-04-07 git
113 031e20a5 2019-04-19 hi ## Sort from the current line to EOF
114 9ad69938 2019-04-07 git
115 031e20a5 2019-04-19 hi `G` won't be shown
116 6c9b71bc 2019-04-06 git
117 031e20a5 2019-04-19 hi <pre>
118 031e20a5 2019-04-19 hi !<u>G</u>sort
119 031e20a5 2019-04-19 hi </pre>
120 031e20a5 2019-04-19 hi
121 031e20a5 2019-04-19 hi
122 031e20a5 2019-04-19 hi ## Delete duplicated lines in the file
123 031e20a5 2019-04-19 hi
124 18871106 2019-04-06 git :%!uniq
125 18871106 2019-04-06 git
126 031e20a5 2019-04-19 hi ## Delete duplicated lines in the block
127 18871106 2019-04-06 git
128 031e20a5 2019-04-19 hi `}` won't be shown
129 6c9b71bc 2019-04-06 git
130 031e20a5 2019-04-19 hi <pre>
131 031e20a5 2019-04-19 hi !<u>}</u>uniq
132 031e20a5 2019-04-19 hi </pre>
133 f931c46b 2019-04-07 git
134 031e20a5 2019-04-19 hi ## Delete duplicated lines till EOF
135 9ad69938 2019-04-07 git
136 031e20a5 2019-04-19 hi `G` won't be shown
137 9ad69938 2019-04-07 git
138 031e20a5 2019-04-19 hi <pre>
139 031e20a5 2019-04-19 hi !<u>G</u>uniq
140 031e20a5 2019-04-19 hi </pre>
141 9ad69938 2019-04-07 git
142 9541feb7 2019-04-08 git
143 031e20a5 2019-04-19 hi ## Underline all lines starting with `pattern`
144 9541feb7 2019-04-08 git
145 031e20a5 2019-04-19 hi <pre>
146 031e20a5 2019-04-19 hi :g/^<em>pattern</em> /t.|s/./=/g
147 031e20a5 2019-04-19 hi </pre>
148 9541feb7 2019-04-08 git
149 9541feb7 2019-04-08 git
150 031e20a5 2019-04-19 hi ## Search for `pattern`, print the containing function (start with `def`) and line number
151 9541feb7 2019-04-08 git
152 031e20a5 2019-04-19 hi <pre>
153 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^ *<em>def</em> ?#
154 031e20a5 2019-04-19 hi </pre>
155 9541feb7 2019-04-08 git
156 9541feb7 2019-04-08 git
157 031e20a5 2019-04-19 hi ## Add &#35; to paragraph containing `pattern`
158 031e20a5 2019-04-19 hi
159 031e20a5 2019-04-19 hi <pre>
160 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^$?+,//-s/^/#
161 031e20a5 2019-04-19 hi </pre>
162 031e20a5 2019-04-19 hi
163 031e20a5 2019-04-19 hi
164 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block
165 031e20a5 2019-04-19 hi
166 9541feb7 2019-04-08 git :g/{$/+,/^}/-!sort
167 9541feb7 2019-04-08 git
168 9541feb7 2019-04-08 git
169 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block (media queries)
170 031e20a5 2019-04-19 hi
171 9541feb7 2019-04-08 git :g/^[^@].*{$/+,/}/-!sort
172 9541feb7 2019-04-08 git
173 9541feb7 2019-04-08 git
174 031e20a5 2019-04-19 hi ## Format content of `<p>` tag to fixed width
175 9541feb7 2019-04-08 git
176 031e20a5 2019-04-19 hi width = 40
177 bf5cacf5 2019-04-19 git
178 031e20a5 2019-04-19 hi <pre>
179 031e20a5 2019-04-19 hi :g/&lt;p&gt;/+,/&lt;\/p&gt;/-!fmt -<em>40</em>
180 031e20a5 2019-04-19 hi </pre>
181 031e20a5 2019-04-19 hi
182 031e20a5 2019-04-19 hi
183 031e20a5 2019-04-19 hi ## Reverse all lines, move `m` all lines to 0
184 031e20a5 2019-04-19 hi
185 bf5cacf5 2019-04-19 git :g/1*/m0
186 bf5cacf5 2019-04-19 git
187 9541feb7 2019-04-08 git
188 031e20a5 2019-04-19 hi ## Swap `Lastname, Firstname` to `Firstname, Lastname`
189 031e20a5 2019-04-19 hi
190 0bb4466d 2019-04-08 git :%s/\(.*\), \(.*\)/\2 \1/
191 9541feb7 2019-04-08 git
192 8f839d09 2019-04-15 git
193 031e20a5 2019-04-19 hi ## Convert to lowercase
194 031e20a5 2019-04-19 hi
195 8f839d09 2019-04-15 git :%s/.*/\L&/
196 8f839d09 2019-04-15 git
197 6c9b71bc 2019-04-06 git
198 031e20a5 2019-04-19 hi ## Join all lines
199 031e20a5 2019-04-19 hi
200 6c9b71bc 2019-04-06 git :%j
201 6c9b71bc 2019-04-06 git
202 031e20a5 2019-04-19 hi ## Copy `t` or move `m` lines containing `pattern`
203 9541feb7 2019-04-08 git
204 031e20a5 2019-04-19 hi <pre>
205 031e20a5 2019-04-19 hi :g/<em>pattern</em>/t$
206 031e20a5 2019-04-19 hi :g/<em>pattern</em>/m$
207 031e20a5 2019-04-19 hi </pre>
208 9541feb7 2019-04-08 git
209 031e20a5 2019-04-19 hi ## Select a column of a table
210 031e20a5 2019-04-19 hi Select 3rd column separated by colon (`:`)
211 6c9b71bc 2019-04-06 git
212 031e20a5 2019-04-19 hi <pre>
213 031e20a5 2019-04-19 hi :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
214 031e20a5 2019-04-19 hi </pre>
215 6c9b71bc 2019-04-06 git
216 031e20a5 2019-04-19 hi ## Insert the sum of a list of numbers after an arbitrary number of lines
217 6c9b71bc 2019-04-06 git
218 031e20a5 2019-04-19 hi 1) mark the first line: `mk`
219 031e20a5 2019-04-19 hi 2) mark the last line: `ml`
220 9541feb7 2019-04-08 git
221 031e20a5 2019-04-19 hi <pre>
222 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
223 031e20a5 2019-04-19 hi </pre>
224 9541feb7 2019-04-08 git
225 031e20a5 2019-04-19 hi or
226 9541feb7 2019-04-08 git
227 031e20a5 2019-04-19 hi <pre>
228 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
229 031e20a5 2019-04-19 hi </pre>
230 f8e3820c 2019-04-10 git
231 031e20a5 2019-04-19 hi ## Email the block
232 f8e3820c 2019-04-10 git
233 031e20a5 2019-04-19 hi <pre>
234 031e20a5 2019-04-19 hi :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
235 031e20a5 2019-04-19 hi </pre>
236 f8e3820c 2019-04-10 git
237 031e20a5 2019-04-19 hi ## Enable and use `ex` history
238 031e20a5 2019-04-19 hi
239 031e20a5 2019-04-19 hi <pre>
240 031e20a5 2019-04-19 hi 1) Set `ESC` key to enable history or add to `~/.exrc`:
241 031e20a5 2019-04-19 hi <b>:set cedit=<u>&lt;CTRL-V&gt;&lt;ESC&gt;</u></b>
242 031e20a5 2019-04-19 hi
243 031e20a5 2019-04-19 hi 2) Use it with:
244 031e20a5 2019-04-19 hi :<u>&lt;ESC&gt;</u>
245 031e20a5 2019-04-19 hi </pre>