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 c95d1e65 2019-04-20 hi <img class="mt4" src="vi-sw.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 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>
14 031e20a5 2019-04-19 hi Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
15 7815de24 2019-04-10 git
16 c57b7323 2020-05-10 git [Bill Joy's greatest gift to man - the vi editor](https://www.theregister.co.uk/2003/09/11/bill_joys_greatest_gift/)
17 af599e02 2019-04-06 git
18 c57b7323 2020-05-10 git
19 031e20a5 2019-04-19 hi ## Yank/delete lines
20 18871106 2019-04-06 git
21 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
22 031e20a5 2019-04-19 hi (2) move to last line<br>
23 031e20a5 2019-04-19 hi (3a) yank: `y'k`<br>
24 031e20a5 2019-04-19 hi (3b) delete: `d'k`<br>
25 031e20a5 2019-04-19 hi (4) move to destination line<br>
26 031e20a5 2019-04-19 hi (5) put with `P` or `p`<br>
27 18871106 2019-04-06 git
28 18871106 2019-04-06 git
29 031e20a5 2019-04-19 hi ## Apply regex to lines
30 18871106 2019-04-06 git
31 031e20a5 2019-04-19 hi (1) mark the first line: `mk`<br>
32 031e20a5 2019-04-19 hi (2) mark the last line: `ml`<br>
33 66a0e66d 2019-04-06 git
34 031e20a5 2019-04-19 hi <pre>
35 031e20a5 2019-04-19 hi :'k,'ls/<em>regex</em>/<em>power</em>/g
36 031e20a5 2019-04-19 hi </pre>
37 66a0e66d 2019-04-06 git
38 66a0e66d 2019-04-06 git
39 50439ce0 2019-04-22 git ## Increment / Decrement number in command mode
40 50439ce0 2019-04-22 git
41 50439ce0 2019-04-22 git (1) move cursor to number<br>
42 50439ce0 2019-04-22 git (2a) increment by one: #+<br>
43 50439ce0 2019-04-22 git (2b) increment by N (5): 5#+<br>
44 50439ce0 2019-04-22 git (3a) decrement by one: #-<br>
45 50439ce0 2019-04-22 git (3b) decrement by N (9): 9#-
46 50439ce0 2019-04-22 git
47 50439ce0 2019-04-22 git
48 031e20a5 2019-04-19 hi ## Add &#35; to a block
49 031e20a5 2019-04-19 hi
50 031e20a5 2019-04-19 hi <pre>
51 031e20a5 2019-04-19 hi :'k,'ls/^/#/
52 031e20a5 2019-04-19 hi </pre>
53 031e20a5 2019-04-19 hi
54 031e20a5 2019-04-19 hi
55 c57b7323 2020-05-10 git ## Remove trailing whitespace from every line
56 c57b7323 2020-05-10 git
57 75e09f64 2020-07-19 git <pre>
58 75e09f64 2020-07-19 git :%s/ *$//
59 75e09f64 2020-07-19 git </pre>
60 c57b7323 2020-05-10 git
61 c57b7323 2020-05-10 git
62 64a200c4 2023-06-06 mischa ## Remove tabs
63 64a200c4 2023-06-06 mischa
64 64a200c4 2023-06-06 mischa <pre>
65 64a200c4 2023-06-06 mischa :%s/<u>&lt;CTRL-V&gt;&lt;TAB&gt;</u>//g
66 64a200c4 2023-06-06 mischa </pre>
67 64a200c4 2023-06-06 mischa
68 64a200c4 2023-06-06 mischa
69 031e20a5 2019-04-19 hi ## Remove trailing whitespace from a block
70 031e20a5 2019-04-19 hi
71 75e09f64 2020-07-19 git <pre>
72 75e09f64 2020-07-19 git :'k,'ls/\ *$//
73 75e09f64 2020-07-19 git </pre>
74 66a0e66d 2019-04-06 git
75 5717e66e 2019-04-15 git
76 50439ce0 2019-04-22 git ## Remove the first N-characters from every line
77 5717e66e 2019-04-15 git
78 031e20a5 2019-04-19 hi N = 5
79 f069fab6 2019-04-16 git
80 031e20a5 2019-04-19 hi <pre>
81 031e20a5 2019-04-19 hi :%s/^.\{0,<em>5</em>\}//
82 031e20a5 2019-04-19 hi </pre>
83 f069fab6 2019-04-16 git
84 f069fab6 2019-04-16 git
85 031e20a5 2019-04-19 hi ## Delete all lines N-character long
86 f069fab6 2019-04-16 git
87 031e20a5 2019-04-19 hi N = 10
88 a070b304 2019-04-08 git
89 031e20a5 2019-04-19 hi <pre>
90 031e20a5 2019-04-19 hi :g/^.\{<em>10</em>\}$/d
91 031e20a5 2019-04-19 hi </pre>
92 a070b304 2019-04-08 git
93 18871106 2019-04-06 git
94 031e20a5 2019-04-19 hi ## Delete all lines _except_ N-character long
95 031e20a5 2019-04-19 hi
96 031e20a5 2019-04-19 hi N = 10
97 031e20a5 2019-04-19 hi
98 031e20a5 2019-04-19 hi <pre>
99 031e20a5 2019-04-19 hi :g!/^.\{<em>10</em>\}$/d
100 031e20a5 2019-04-19 hi </pre>
101 031e20a5 2019-04-19 hi
102 031e20a5 2019-04-19 hi
103 031e20a5 2019-04-19 hi ## Search/replace paths using &#35; as delimiter
104 031e20a5 2019-04-19 hi
105 031e20a5 2019-04-19 hi <pre>
106 031e20a5 2019-04-19 hi :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
107 031e20a5 2019-04-19 hi </pre>
108 031e20a5 2019-04-19 hi
109 031e20a5 2019-04-19 hi
110 9122df5c 2023-06-06 mischa ## Search/replace &#94;M with LF
111 64a200c4 2023-06-06 mischa
112 9122df5c 2023-06-06 mischa <pre>
113 9122df5c 2023-06-06 mischa :g/<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>/s///g
114 9122df5c 2023-06-06 mischa </pre>
115 64a200c4 2023-06-06 mischa
116 64a200c4 2023-06-06 mischa
117 031e20a5 2019-04-19 hi ## Write the file as root
118 031e20a5 2019-04-19 hi
119 18871106 2019-04-06 git :w !doas tee %
120 18871106 2019-04-06 git
121 18871106 2019-04-06 git
122 031e20a5 2019-04-19 hi ## Diff the file on disk with the buffer
123 031e20a5 2019-04-19 hi
124 18871106 2019-04-06 git :w !diff -u % -
125 18871106 2019-04-06 git
126 18871106 2019-04-06 git
127 031e20a5 2019-04-19 hi ## Make a backup of the file on disk
128 031e20a5 2019-04-19 hi
129 6c9b71bc 2019-04-06 git :!cp % %.bak
130 18871106 2019-04-06 git
131 18871106 2019-04-06 git
132 031e20a5 2019-04-19 hi ## Sort all lines
133 031e20a5 2019-04-19 hi
134 18871106 2019-04-06 git :%!sort
135 18871106 2019-04-06 git
136 18871106 2019-04-06 git
137 031e20a5 2019-04-19 hi ## Sort a block
138 6c9b71bc 2019-04-06 git
139 031e20a5 2019-04-19 hi `}` won't be shown
140 f931c46b 2019-04-07 git
141 031e20a5 2019-04-19 hi <pre>
142 190821ba 2019-04-20 git !<u>}</u>sort
143 031e20a5 2019-04-19 hi </pre>
144 9ad69938 2019-04-07 git
145 9ad69938 2019-04-07 git
146 031e20a5 2019-04-19 hi ## Sort from the current line to EOF
147 9ad69938 2019-04-07 git
148 031e20a5 2019-04-19 hi `G` won't be shown
149 6c9b71bc 2019-04-06 git
150 031e20a5 2019-04-19 hi <pre>
151 031e20a5 2019-04-19 hi !<u>G</u>sort
152 031e20a5 2019-04-19 hi </pre>
153 031e20a5 2019-04-19 hi
154 031e20a5 2019-04-19 hi
155 031e20a5 2019-04-19 hi ## Delete duplicated lines in the file
156 031e20a5 2019-04-19 hi
157 18871106 2019-04-06 git :%!uniq
158 18871106 2019-04-06 git
159 031e20a5 2019-04-19 hi ## Delete duplicated lines in the block
160 18871106 2019-04-06 git
161 031e20a5 2019-04-19 hi `}` won't be shown
162 6c9b71bc 2019-04-06 git
163 031e20a5 2019-04-19 hi <pre>
164 031e20a5 2019-04-19 hi !<u>}</u>uniq
165 031e20a5 2019-04-19 hi </pre>
166 f931c46b 2019-04-07 git
167 031e20a5 2019-04-19 hi ## Delete duplicated lines till EOF
168 9ad69938 2019-04-07 git
169 031e20a5 2019-04-19 hi `G` won't be shown
170 9ad69938 2019-04-07 git
171 031e20a5 2019-04-19 hi <pre>
172 031e20a5 2019-04-19 hi !<u>G</u>uniq
173 031e20a5 2019-04-19 hi </pre>
174 9ad69938 2019-04-07 git
175 9541feb7 2019-04-08 git
176 031e20a5 2019-04-19 hi ## Underline all lines starting with `pattern`
177 9541feb7 2019-04-08 git
178 031e20a5 2019-04-19 hi <pre>
179 031e20a5 2019-04-19 hi :g/^<em>pattern</em> /t.|s/./=/g
180 031e20a5 2019-04-19 hi </pre>
181 9541feb7 2019-04-08 git
182 9541feb7 2019-04-08 git
183 031e20a5 2019-04-19 hi ## Search for `pattern`, print the containing function (start with `def`) and line number
184 9541feb7 2019-04-08 git
185 031e20a5 2019-04-19 hi <pre>
186 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^ *<em>def</em> ?#
187 031e20a5 2019-04-19 hi </pre>
188 9541feb7 2019-04-08 git
189 9541feb7 2019-04-08 git
190 031e20a5 2019-04-19 hi ## Add &#35; to paragraph containing `pattern`
191 031e20a5 2019-04-19 hi
192 031e20a5 2019-04-19 hi <pre>
193 031e20a5 2019-04-19 hi :g/<em>pattern</em>/?^$?+,//-s/^/#
194 031e20a5 2019-04-19 hi </pre>
195 031e20a5 2019-04-19 hi
196 031e20a5 2019-04-19 hi
197 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block
198 031e20a5 2019-04-19 hi
199 9541feb7 2019-04-08 git :g/{$/+,/^}/-!sort
200 9541feb7 2019-04-08 git
201 9541feb7 2019-04-08 git
202 031e20a5 2019-04-19 hi ## Sort content of a multiline CSS block (media queries)
203 031e20a5 2019-04-19 hi
204 9541feb7 2019-04-08 git :g/^[^@].*{$/+,/}/-!sort
205 9541feb7 2019-04-08 git
206 9541feb7 2019-04-08 git
207 031e20a5 2019-04-19 hi ## Format content of `<p>` tag to fixed width
208 9541feb7 2019-04-08 git
209 031e20a5 2019-04-19 hi width = 40
210 bf5cacf5 2019-04-19 git
211 031e20a5 2019-04-19 hi <pre>
212 031e20a5 2019-04-19 hi :g/&lt;p&gt;/+,/&lt;\/p&gt;/-!fmt -<em>40</em>
213 031e20a5 2019-04-19 hi </pre>
214 031e20a5 2019-04-19 hi
215 031e20a5 2019-04-19 hi
216 c57b7323 2020-05-10 git ## Format whole document
217 c57b7323 2020-05-10 git
218 c57b7323 2020-05-10 git :%!fmt -s
219 c57b7323 2020-05-10 git
220 c57b7323 2020-05-10 git In your .nexrc
221 c57b7323 2020-05-10 git
222 c57b7323 2020-05-10 git <pre>
223 c57b7323 2020-05-10 git map gF :%!fmt -s<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
224 c57b7323 2020-05-10 git </pre>
225 c57b7323 2020-05-10 git
226 031e20a5 2019-04-19 hi ## Reverse all lines, move `m` all lines to 0
227 031e20a5 2019-04-19 hi
228 bf5cacf5 2019-04-19 git :g/1*/m0
229 bf5cacf5 2019-04-19 git
230 9541feb7 2019-04-08 git
231 031e20a5 2019-04-19 hi ## Swap `Lastname, Firstname` to `Firstname, Lastname`
232 031e20a5 2019-04-19 hi
233 0bb4466d 2019-04-08 git :%s/\(.*\), \(.*\)/\2 \1/
234 9541feb7 2019-04-08 git
235 8f839d09 2019-04-15 git
236 031e20a5 2019-04-19 hi ## Convert to lowercase
237 031e20a5 2019-04-19 hi
238 70b3c166 2019-04-20 hi :%s/.*/\L&/
239 8f839d09 2019-04-15 git
240 6c9b71bc 2019-04-06 git
241 64a200c4 2023-06-06 mischa ## Surround text with pattern
242 64a200c4 2023-06-06 mischa
243 64a200c4 2023-06-06 mischa :%s/.*/`pattern` & `pattern`/
244 64a200c4 2023-06-06 mischa
245 64a200c4 2023-06-06 mischa
246 031e20a5 2019-04-19 hi ## Join all lines
247 031e20a5 2019-04-19 hi
248 6c9b71bc 2019-04-06 git :%j
249 6c9b71bc 2019-04-06 git
250 031e20a5 2019-04-19 hi ## Copy `t` or move `m` lines containing `pattern`
251 9541feb7 2019-04-08 git
252 031e20a5 2019-04-19 hi <pre>
253 031e20a5 2019-04-19 hi :g/<em>pattern</em>/t$
254 031e20a5 2019-04-19 hi :g/<em>pattern</em>/m$
255 031e20a5 2019-04-19 hi </pre>
256 9541feb7 2019-04-08 git
257 031e20a5 2019-04-19 hi ## Select a column of a table
258 031e20a5 2019-04-19 hi Select 3rd column separated by colon (`:`)
259 6c9b71bc 2019-04-06 git
260 031e20a5 2019-04-19 hi <pre>
261 031e20a5 2019-04-19 hi :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
262 031e20a5 2019-04-19 hi </pre>
263 6c9b71bc 2019-04-06 git
264 031e20a5 2019-04-19 hi ## Insert the sum of a list of numbers after an arbitrary number of lines
265 6c9b71bc 2019-04-06 git
266 70b3c166 2019-04-20 hi (1) mark the first line: `mk`
267 70b3c166 2019-04-20 hi (2) mark the last line: `ml`
268 9541feb7 2019-04-08 git
269 031e20a5 2019-04-19 hi <pre>
270 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
271 031e20a5 2019-04-19 hi </pre>
272 9541feb7 2019-04-08 git
273 031e20a5 2019-04-19 hi or
274 9541feb7 2019-04-08 git
275 031e20a5 2019-04-19 hi <pre>
276 031e20a5 2019-04-19 hi :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
277 031e20a5 2019-04-19 hi </pre>
278 f8e3820c 2019-04-10 git
279 031e20a5 2019-04-19 hi ## Email the block
280 f8e3820c 2019-04-10 git
281 031e20a5 2019-04-19 hi <pre>
282 031e20a5 2019-04-19 hi :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
283 031e20a5 2019-04-19 hi </pre>
284 f8e3820c 2019-04-10 git
285 031e20a5 2019-04-19 hi ## Enable and use `ex` history
286 031e20a5 2019-04-19 hi
287 ade8cac0 2019-04-27 git (1) Set `ESC` key to enable history or add to `~/.nexrc`:
288 c2580c10 2019-04-20 hi
289 031e20a5 2019-04-19 hi <pre>
290 c2580c10 2019-04-20 hi :set cedit=<u>&lt;CTRL-V&gt;&lt;ESC&gt;</u>
291 c2580c10 2019-04-20 hi </pre>
292 031e20a5 2019-04-19 hi
293 c2580c10 2019-04-20 hi (2) Use it with:
294 c2580c10 2019-04-20 hi
295 c2580c10 2019-04-20 hi <pre>
296 031e20a5 2019-04-19 hi :<u>&lt;ESC&gt;</u>
297 ade8cac0 2019-04-27 git </pre>
298 ade8cac0 2019-04-27 git
299 ade8cac0 2019-04-27 git
300 ade8cac0 2019-04-27 git ## Integrate with tmux buffer
301 ade8cac0 2019-04-27 git
302 ade8cac0 2019-04-27 git (1) cut text from current position to mark 'm' into tmux buffer. Hit undo to put text back into vi buffer.
303 ade8cac0 2019-04-27 git
304 ade8cac0 2019-04-27 git <pre>
305 ade8cac0 2019-04-27 git !'mtmux load-buffer -
306 031e20a5 2019-04-19 hi </pre>
307 ade8cac0 2019-04-27 git
308 ade8cac0 2019-04-27 git (2) paste text from tmux buffer into vi buffer.
309 ade8cac0 2019-04-27 git
310 ade8cac0 2019-04-27 git <pre>
311 ade8cac0 2019-04-27 git :r!tmux show-buffer
312 ade8cac0 2019-04-27 git </pre>
313 ade8cac0 2019-04-27 git
314 ee74a164 2019-06-05 git (3) Map in ~/.nexrc (command mode)
315 ade8cac0 2019-04-27 git
316 ade8cac0 2019-04-27 git <pre>
317 ade8cac0 2019-04-27 git map gx !'mtmux load-buffer -<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
318 ade8cac0 2019-04-27 git map gy !'mtmux load-buffer -<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>u
319 ade8cac0 2019-04-27 git map gp :r!tmux show-buffer<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
320 ade8cac0 2019-04-27 git </pre>
321 9e2dabb0 2019-04-27 git
322 9e2dabb0 2019-04-27 git
323 ee74a164 2019-06-05 git ## Remap ESC to ALT-i in ~/.nexrc (insert mode)
324 9e2dabb0 2019-04-27 git
325 9e2dabb0 2019-04-27 git <pre>
326 9e2dabb0 2019-04-27 git map! <u>&lt;CTRL-V&gt;&lt;ALT-i&gt;</u> <u>&lt;CTRL-V&gt;&lt;ESC&gt;</u>
327 9e2dabb0 2019-04-27 git </pre>
328 9e2dabb0 2019-04-27 git
329 9e2dabb0 2019-04-27 git