Blob


1 <p class="f7">
2 <a href="/suggest.html">suggestion box</a> &ndash;
3 <a href="https://git.high5.nl/why-vi.rocks/log/">latest additions</a> &ndash;
4 <a href="/also.html">see also</a> &ndash;
5 <a href="/colophon.html">colophon</a>
6 </p>
8 <img class="mt4" src="vi-sw.png">
10 _[vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
11 text editor in any Unix-like operating system._
13 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 Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
16 [Bill Joy's greatest gift to man - the vi editor](https://www.theregister.co.uk/2003/09/11/bill_joys_greatest_gift/)
19 ## Yank/delete lines
21 (1) mark the first line: `mk`<br>
22 (2) move to last line<br>
23 (3a) yank: `y'k`<br>
24 (3b) delete: `d'k`<br>
25 (4) move to destination line<br>
26 (5) put with `P` or `p`<br>
29 ## Apply regex to lines
31 (1) mark the first line: `mk`<br>
32 (2) mark the last line: `ml`<br>
34 <pre>
35 :'k,'ls/<em>regex</em>/<em>power</em>/g
36 </pre>
39 ## Increment / Decrement number in command mode
41 (1) move cursor to number<br>
42 (2a) increment by one: #+<br>
43 (2b) increment by N (5): 5#+<br>
44 (3a) decrement by one: #-<br>
45 (3b) decrement by N (9): 9#-
48 ## Add &#35; to a block
50 <pre>
51 :'k,'ls/^/#/
52 </pre>
55 ## Remove trailing whitespace from every line
57 <pre>
58 :%s/ *$//
59 </pre>
62 ## Remove tabs
64 <pre>
65 :%s/<u>&lt;CTRL-V&gt;&lt;TAB&gt;</u>//g
66 </pre>
69 ## Remove trailing whitespace from a block
71 <pre>
72 :'k,'ls/\ *$//
73 </pre>
76 ## Remove the first N-characters from every line
78 N = 5
80 <pre>
81 :%s/^.\{0,<em>5</em>\}//
82 </pre>
85 ## Delete all lines N-character long
87 N = 10
89 <pre>
90 :g/^.\{<em>10</em>\}$/d
91 </pre>
94 ## Delete all lines _except_ N-character long
96 N = 10
98 <pre>
99 :g!/^.\{<em>10</em>\}$/d
100 </pre>
103 ## Search/replace paths using &#35; as delimiter
105 <pre>
106 :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
107 </pre>
110 ## Search/replace &#94;M with LF
112 <pre>
113 :g/<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>/s///g
114 </pre>
117 ## Write the file as root
119 :w !doas tee %
122 ## Diff the file on disk with the buffer
124 :w !diff -u % -
127 ## Make a backup of the file on disk
129 :!cp % %.bak
132 ## Sort all lines
134 :%!sort
137 ## Sort a block
139 `}` won't be shown
141 <pre>
142 !<u>}</u>sort
143 </pre>
146 ## Sort from the current line to EOF
148 `G` won't be shown
150 <pre>
151 !<u>G</u>sort
152 </pre>
155 ## Delete duplicated lines in the file
157 :%!uniq
159 ## Delete duplicated lines in the block
161 `}` won't be shown
163 <pre>
164 !<u>}</u>uniq
165 </pre>
167 ## Delete duplicated lines till EOF
169 `G` won't be shown
171 <pre>
172 !<u>G</u>uniq
173 </pre>
176 ## Underline all lines starting with `pattern`
178 <pre>
179 :g/^<em>pattern</em> /t.|s/./=/g
180 </pre>
183 ## Search for `pattern`, print the containing function (start with `def`) and line number
185 <pre>
186 :g/<em>pattern</em>/?^ *<em>def</em> ?#
187 </pre>
190 ## Add &#35; to paragraph containing `pattern`
192 <pre>
193 :g/<em>pattern</em>/?^$?+,//-s/^/#
194 </pre>
197 ## Sort content of a multiline CSS block
199 :g/{$/+,/^}/-!sort
202 ## Sort content of a multiline CSS block (media queries)
204 :g/^[^@].*{$/+,/}/-!sort
207 ## Format content of `<p>` tag to fixed width
209 width = 40
211 <pre>
212 :g/&lt;p&gt;/+,/&lt;\/p&gt;/-!fmt -<em>40</em>
213 </pre>
216 ## Format whole document
218 :%!fmt -s
220 In your .nexrc
222 <pre>
223 map gF :%!fmt -s<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
224 </pre>
226 ## Reverse all lines, move `m` all lines to 0
228 :g/1*/m0
231 ## Swap `Lastname, Firstname` to `Firstname, Lastname`
233 :%s/\(.*\), \(.*\)/\2 \1/
236 ## Convert to lowercase
238 :%s/.*/\L&/
241 ## Surround text with pattern
243 :%s/.*/`pattern` & `pattern`/
246 ## Join all lines
248 :%j
250 ## Copy `t` or move `m` lines containing `pattern`
252 <pre>
253 :g/<em>pattern</em>/t$
254 :g/<em>pattern</em>/m$
255 </pre>
257 ## Select a column of a table
258 Select 3rd column separated by colon (`:`)
260 <pre>
261 :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
262 </pre>
264 ## Insert the sum of a list of numbers after an arbitrary number of lines
266 (1) mark the first line: `mk`
267 (2) mark the last line: `ml`
269 <pre>
270 :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
271 </pre>
273 or
275 <pre>
276 :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
277 </pre>
279 ## Email the block
281 <pre>
282 :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
283 </pre>
285 ## Enable and use `ex` history
287 (1) Set `ESC` key to enable history or add to `~/.nexrc`:
289 <pre>
290 :set cedit=<u>&lt;CTRL-V&gt;&lt;ESC&gt;</u>
291 </pre>
293 (2) Use it with:
295 <pre>
296 :<u>&lt;ESC&gt;</u>
297 </pre>
300 ## Integrate with tmux buffer
302 (1) cut text from current position to mark 'm' into tmux buffer. Hit undo to put text back into vi buffer.
304 <pre>
305 !'mtmux load-buffer -
306 </pre>
308 (2) paste text from tmux buffer into vi buffer.
310 <pre>
311 :r!tmux show-buffer
312 </pre>
314 (3) Map in ~/.nexrc (command mode)
316 <pre>
317 map gx !'mtmux load-buffer -<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
318 map gy !'mtmux load-buffer -<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>u
319 map gp :r!tmux show-buffer<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
320 </pre>
323 ## Remap ESC to ALT-i in ~/.nexrc (insert mode)
325 <pre>
326 map! <u>&lt;CTRL-V&gt;&lt;ALT-i&gt;</u> <u>&lt;CTRL-V&gt;&lt;ESC&gt;</u>
327 </pre>