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
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).
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
30 4) move to destination line
33 #### Apply regex to an arbitrary number of lines
35 1) mark the first line: mk
36 2) mark the last line: ml
39 #### Add # to block of text
43 #### Remove trailing whitespace of block of text
47 #### Remove the first N (5) characters from every line
51 #### Delete all lines N (10) characters long
55 #### Delete all lines _except_ N (10) characters long
59 #### Search and replace PATHs, using different delimiter
61 :%s#/usr/local/log#/var/log#g
63 #### Write file as root
67 #### Diff the file on disk with the file in the buffer
71 #### Make a backup of the file on disk
83 <sub><sup>} won't be shown in the command.</sup></sub>
85 #### Sort from current line to EOF
89 <sub><sup>G won't be shown in the command.</sup></sub>
99 <sub><sup>} won't be shown in the command.</sup></sub>
101 #### Uniq from current line to EOF
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
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
135 #### Swap "Lastname, Firstname" to "Firstname, Lastname"
137 :%s/\(.*\), \(.*\)/\2 \1/
139 #### Change all text to lowercase
147 #### Copy (t) or move (m) lines containing "pattern"
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>