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).
17 ## Yank/delete lines
19 (1) mark the first line: `mk`<br>
20 (2) move to last line<br>
21 (3a) yank: `y'k`<br>
22 (3b) delete: `d'k`<br>
23 (4) move to destination line<br>
24 (5) put with `P` or `p`<br>
27 ## Apply regex to lines
29 (1) mark the first line: `mk`<br>
30 (2) mark the last line: `ml`<br>
32 <pre>
33 :'k,'ls/<em>regex</em>/<em>power</em>/g
34 </pre>
37 ## Add &#35; to a block
39 <pre>
40 :'k,'ls/^/#/
41 </pre>
44 ## Remove trailing whitespace from a block
46 :'k,'ls/\ *$//
49 ## Remove the first N characters from every line
51 N = 5
53 <pre>
54 :%s/^.\{0,<em>5</em>\}//
55 </pre>
58 ## Delete all lines N-character long
60 N = 10
62 <pre>
63 :g/^.\{<em>10</em>\}$/d
64 </pre>
67 ## Delete all lines _except_ N-character long
69 N = 10
71 <pre>
72 :g!/^.\{<em>10</em>\}$/d
73 </pre>
76 ## Search/replace paths using &#35; as delimiter
78 <pre>
79 :%s#<em>/usr/local/log</em>#<em>/var/log</em>#g
80 </pre>
83 ## Write the file as root
85 :w !doas tee %
88 ## Diff the file on disk with the buffer
90 :w !diff -u % -
93 ## Make a backup of the file on disk
95 :!cp % %.bak
98 ## Sort all lines
100 :%!sort
103 ## Sort a block
105 `}` won't be shown
107 <pre>
108 <u>!</u>}sort
109 </pre>
112 ## Sort from the current line to EOF
114 `G` won't be shown
116 <pre>
117 !<u>G</u>sort
118 </pre>
121 ## Delete duplicated lines in the file
123 :%!uniq
125 ## Delete duplicated lines in the block
127 `}` won't be shown
129 <pre>
130 !<u>}</u>uniq
131 </pre>
133 ## Delete duplicated lines till EOF
135 `G` won't be shown
137 <pre>
138 !<u>G</u>uniq
139 </pre>
142 ## Underline all lines starting with `pattern`
144 <pre>
145 :g/^<em>pattern</em> /t.|s/./=/g
146 </pre>
149 ## Search for `pattern`, print the containing function (start with `def`) and line number
151 <pre>
152 :g/<em>pattern</em>/?^ *<em>def</em> ?#
153 </pre>
156 ## Add &#35; to paragraph containing `pattern`
158 <pre>
159 :g/<em>pattern</em>/?^$?+,//-s/^/#
160 </pre>
163 ## Sort content of a multiline CSS block
165 :g/{$/+,/^}/-!sort
168 ## Sort content of a multiline CSS block (media queries)
170 :g/^[^@].*{$/+,/}/-!sort
173 ## Format content of `<p>` tag to fixed width
175 width = 40
177 <pre>
178 :g/&lt;p&gt;/+,/&lt;\/p&gt;/-!fmt -<em>40</em>
179 </pre>
182 ## Reverse all lines, move `m` all lines to 0
184 :g/1*/m0
187 ## Swap `Lastname, Firstname` to `Firstname, Lastname`
189 :%s/\(.*\), \(.*\)/\2 \1/
192 ## Convert to lowercase
194 :%s/.*/\L&/
197 ## Join all lines
199 :%j
201 ## Copy `t` or move `m` lines containing `pattern`
203 <pre>
204 :g/<em>pattern</em>/t$
205 :g/<em>pattern</em>/m$
206 </pre>
208 ## Select a column of a table
209 Select 3rd column separated by colon (`:`)
211 <pre>
212 :%!awk -F'<em>:</em>' '{print $<em>3</em>}'
213 </pre>
215 ## Insert the sum of a list of numbers after an arbitrary number of lines
217 1) mark the first line: `mk`
218 2) mark the last line: `ml`
220 <pre>
221 :'k,'l!awk 'END{print "<em>total:</em>", i}{i+=$1; print}'
222 </pre>
224 or
226 <pre>
227 :'k,'l!awk 'END{print "<em>total:</em>", i} ++i || 1'
228 </pre>
230 ## Email the block
232 <pre>
233 :?^$?+,//-w !mail -s "<em>subject</em>" <em>email@example.com</em>
234 </pre>
236 ## Enable and use `ex` history
238 <pre>
239 1) Set `ESC` key to enable history or add to `~/.exrc`:
240 <b>:set cedit=<u>&lt;CTRL-V&gt;&lt;ESC&gt;</u></b>
242 2) Use it with:
243 :<u>&lt;ESC&gt;</u>
244 </pre>