Blob


1 <p class="f7">
2 <a href="/suggest.html">suggestion box</a> &ndash;
3 <a href="https://got.high5.nl/?action=summary&path=why-vi.rocks.git">latest additions (got)</a> &ndash;
4 <a href="/also.html">see also</a> &ndash;
5 <a href="/colophon.html">colophon</a>
6 </p>
8 <div class="logo-container">
9 <img id="logo" class="mt4" src="vi-sw.png" alt="Logo">
10 </div>
12 _Why? [vi](https://en.wikipedia.org/wiki/Vi) is the **de facto** standard
13 text editor in any Unix-like operating system._
15 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>
16 Tested with [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
18 [Bill Joy's greatest gift to man - the vi editor](https://www.theregister.co.uk/2003/09/11/bill_joys_greatest_gift/)
21 ## Yank/delete lines
23 (1) mark the first line: `mk`<br>
24 (2) move to last line<br>
25 (3a) yank: `y'k`<br>
26 (3b) delete: `d'k`<br>
27 (4) move to destination line<br>
28 (5) put with `P` or `p`<br>
31 ## Apply regex to lines
33 (1) mark the first line: `mk`<br>
34 (2) mark the last line: `ml`<br>
36 <pre>
37 :'k,'ls/<em>regex</em>/<em>power</em>/g
38 </pre>
41 ## Increment / Decrement number in command mode
43 (1) move cursor to number<br>
44 (2a) increment by one: #+<br>
45 (2b) increment by N (5): 5#+<br>
46 (3a) decrement by one: #-<br>
47 (3b) decrement by N (9): 9#-
50 ## Add &#35; to a block
52 <pre>
53 :'k,'ls/^/#/
54 </pre>
57 ## Remove trailing whitespace from every line
59 <pre>
60 :%s/ *$//
61 </pre>
64 ## Remove tabs
66 <pre>
67 :%s/<u>&lt;CTRL-V&gt;&lt;TAB&gt;</u>//g
68 </pre>
71 ## Remove trailing whitespace from a block
73 <pre>
74 :'k,'ls/\ *$//
75 </pre>
78 ## Remove the first N-characters from every line
80 N = 5
82 <pre>
83 :%s/^.\{0,<em>5</em>\}//
84 </pre>
87 ## Delete all lines N-character long
89 N = 10
91 <pre>
92 :g/^.\{<em>10</em>\}$/d
93 </pre>
96 ## Delete all lines _except_ N-character long
98 N = 10
100 <pre>
101 :g!/^.\{<em>10</em>\}$/d
102 </pre>
105 ## Search/replace paths using &#35; as delimiter
107 <pre>
108 :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
109 </pre>
112 ## Search/replace &#94;M with LF
114 <pre>
115 :g/<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>/s///g
116 </pre>
119 ## Write the file as root
121 :w !doas tee %
124 ## Diff the file on disk with the buffer
126 :w !diff -u % -
129 ## Make a backup of the file on disk
131 :!cp % %.bak
134 ## Sort all lines
136 :%!sort
139 ## Sort a block
141 `}` won't be shown
143 <pre>
144 !<u>}</u>sort
145 </pre>
148 ## Sort from the current line to EOF
150 `G` won't be shown
152 <pre>
153 !<u>G</u>sort
154 </pre>
157 ## Delete duplicated lines in the file
159 :%!uniq
161 ## Delete duplicated lines in the block
163 `}` won't be shown
165 <pre>
166 !<u>}</u>uniq
167 </pre>
169 ## Delete duplicated lines till EOF
171 `G` won't be shown
173 <pre>
174 !<u>G</u>uniq
175 </pre>
178 ## Underline all lines starting with `pattern`
180 <pre>
181 :g/^<em>pattern</em> /t.|s/./=/g
182 </pre>
185 ## Search for `pattern`, print the containing function (start with `def`) and line number
187 <pre>
188 :g/<em>pattern</em>/?^ *<em>def</em> ?#
189 </pre>
192 ## Add &#35; to paragraph containing `pattern`
194 <pre>
195 :g/<em>pattern</em>/?^$?+,//-s/^/#
196 </pre>
199 ## Sort content of a multiline CSS block
201 :g/{$/+,/^}/-!sort
204 ## Sort content of a multiline CSS block (media queries)
206 :g/^[^@].*{$/+,/}/-!sort
209 ## Format content of `<p>` tag to fixed width
211 width = 40
213 <pre>
214 :g/&lt;p&gt;/+,/&lt;\/p&gt;/-!fmt -<em>40</em>
215 </pre>
218 ## Format whole document
220 :%!fmt -s
222 In your .nexrc
224 <pre>
225 map gF :%!fmt -s<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
226 </pre>
228 ## Reverse all lines, move `m` all lines to 0
230 :g/1*/m0
233 ## Swap `Lastname, Firstname` to `Firstname, Lastname`
235 :%s/\(.*\), \(.*\)/\2 \1/
238 ## Convert to lowercase
240 :%s/.*/\L&/
243 ## Surround text with pattern
245 :%s/.*/`pattern` & `pattern`/
248 ## Join all lines
250 :%j
252 ## Copy `t` or move `m` lines containing `pattern`
254 <pre>
255 :g/<em>pattern</em>/t$
256 :g/<em>pattern</em>/m$
257 </pre>
259 ## Select a column of a table
260 Select 3rd column separated by colon (`:`)
262 <pre>
263 :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
264 </pre>
266 ## Insert the sum of a list of numbers after an arbitrary number of lines
268 (1) mark the first line: `mk`
269 (2) mark the last line: `ml`
271 <pre>
272 :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
273 </pre>
275 or
277 <pre>
278 :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
279 </pre>
281 ## Email the block
283 <pre>
284 :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
285 </pre>
287 ## Enable and use `ex` history
289 (1) Set `ESC` key to enable history or add to `~/.nexrc`:
291 <pre>
292 :set cedit=<u>&lt;CTRL-V&gt;&lt;ESC&gt;</u>
293 </pre>
295 (2) Use it with:
297 <pre>
298 :<u>&lt;ESC&gt;</u>
299 </pre>
302 ## Integrate with tmux buffer
304 (1) cut text from current position to mark 'm' into tmux buffer. Hit undo to put text back into vi buffer.
306 <pre>
307 !'mtmux load-buffer -
308 </pre>
310 (2) paste text from tmux buffer into vi buffer.
312 <pre>
313 :r!tmux show-buffer
314 </pre>
316 (3) Map in ~/.nexrc (command mode)
318 <pre>
319 map gx !'mtmux load-buffer -<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
320 map gy !'mtmux load-buffer -<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>u
321 map gp :r!tmux show-buffer<u>&lt;CTRL-V&gt;&lt;ENTER&gt;</u>
322 </pre>
325 ## Remap ESC to ALT-i in ~/.nexrc (insert mode)
327 <pre>
328 map! <u>&lt;CTRL-V&gt;&lt;ALT-i&gt;</u> <u>&lt;CTRL-V&gt;&lt;ESC&gt;</u>
329 </pre>