Blob


1 # Why vi Rocks
3 **[vi](https://en.wikipedia.org/wiki/Vi)** is the _de facto_ standard
4 Unix editor, you find it in every *NIX derived OS.
6 Here you will find a collection of commands, command sequences in
7 [vi(1)](https://man.openbsd.org/vi.1)/[ex(1)](https://man.openbsd.org/ex.1)
8 or with 3rd party unitilities which make
9 **[vi](https://en.wikipedia.org/wiki/Vi)** rock \m/. These all work
10 with at least [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and
11 2.1.3 (unicode).
13 We are always looking for [suggestions](/suggest.html) that go beyond the basics.<br />
14 For the latest additions check the [git repo](https://git.high5.nl/why-vi.rocks).
16 Helpful documents:
18 - [Roman Zolotarev: Edit text with vi(1)](https://rgz.ee/vi.html)<br />
19 - [Hugo Daniel: vi is not vim](https://hugodaniel.pt/posts/vi-is-not-vim/)<br />
20 - [Jeff W: vi help](http://www.jeffw.com/vi/vi_help.txt) / [Maarten Litmaath: vi reference](http://www.ungerhu.com/jxh/vi.html)<br />
21 - [alphanrrrd: extremely concise cheatsheet](http://www.alphanrrrd.org/vi.html) / [ViEmu: Graphical vi cheatsheet](http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html)<br />
22 - [The vi archive and FAQ (mirror)](http://git.larryhynes.net/vi/)
24 #### Yank / delete an arbitrary number of lines
26 1) mark the first line: mk
27 2) move to last line
28 3a) yank: y'k
29 3b) delete: d'k
30 4) move to destination line
31 5) put with P or p
33 #### Apply regex to an arbitrary number of lines
35 1) mark the first line: mk
36 2) mark the last line: ml
37 :'k,'ls/regex/power/g
39 #### Add # to block of text
41 :'k,'ls/^/#/
43 #### Remove trailing whitespace of block of text
45 :'k,'ls/\ *$//
47 #### Remove the first N (5) characters from every line
49 :%s/^.\{0,5\}//
51 #### Delete all lines N (10) characters long
53 :g/^.\{10\}$/d
55 #### Delete all lines _except_ N (10) characters long
57 :g!/^.\{10\}$/d
59 #### Search and replace PATHs, using different delimiter
61 :%s#/usr/local/log#/var/log#g
63 #### Write file as root
65 :w !doas tee %
67 #### Diff the file on disk with the file in the buffer
69 :w !diff -u % -
71 #### Make a backup of the file on disk
73 :!cp % %.bak
75 #### Sort all lines
77 :%!sort
79 #### Sort paragraph
81 !}sort
83 <sub><sup>} won't be shown in the command.</sup></sub>
85 #### Sort from current line to EOF
87 !Gsort
89 <sub><sup>G won't be shown in the command.</sup></sub>
91 #### Uniq all lines
93 :%!uniq
95 #### Uniq paragraph
97 !}uniq
99 <sub><sup>} won't be shown in the command.</sup></sub>
101 #### Uniq from current line to EOF
103 !Guniq
105 <sub><sup>G won't be shown in the command.</sup></sub>
107 #### Underline all lines starting with CHAPTER
109 :g/^CHAPTER /t.|s/./=/g
111 #### Search for "pattern", print the containing function (start with def) and line number
113 :g/pattern/?^ *def ?#
115 #### Add # to paragraph containing "pattern"
117 :g/pattern/?^$?+,//-s/^/#
119 #### Sort content of multiline CSS blocks
121 :g/{$/+,/^}/-!sort
123 #### Sort content of multiline CSS blocks (media queries)
125 :g/^[^@].*{$/+,/}/-!sort
127 #### Reformat HTML paragraphs to a fixed width (40)
129 :g/<p>/+,/<\/p>/-!fmt -40
131 #### Invert the order of all lines, move (m) all lines to 0
133 :g/1*/m0
135 #### Swap "Lastname, Firstname" to "Firstname, Lastname"
137 :%s/\(.*\), \(.*\)/\2 \1/
139 #### Change all text to lowercase
141 :%s/.*/\L&/
143 #### Join all lines
145 :%j
147 #### Copy (t) or move (m) lines containing "pattern"
149 :g/pattern/t$
150 :g/pattern/m$
152 #### Select a column (3rd) from formated text seperated by ':'
154 :%!awk -F':' '{print $3}'
156 #### Insert the sum of a list of numbers after an arbitrary number of lines
158 1) mark the first line: mk
159 2) mark the last line: ml
160 :'k,'l!awk 'END{print "Total:", i}{i+=$1; print}'
162 More compact version:
163 :'k,'l!awk 'END{print "Total:", i} ++i || 1'
165 #### Email the current paragraph
167 :?^$?+,//-w !mail -s "<subject>" email@example.com
169 #### Enable and use ex history
171 1) Set ESC key to enable history, or add to .exrc:
172 :set cedit=<CTRL-V><ESC>
174 2) Use it with:
175 :<ESC>