# Why vi Rocks **[vi](https://en.wikipedia.org/wiki/Vi)** is the _de facto_ standard Unix editor, you find it in every *NIX derived OS. Here you will find a collection of commands, command sequences in [vi(1)](https://man.openbsd.org/vi.1)/[ex(1)](https://man.openbsd.org/ex.1) or with 3rd party unitilities which make **[vi](https://en.wikipedia.org/wiki/Vi)** rock \m/. These all work with at least [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode). We are always looking for [suggestions](/suggest.html) that go beyond the basics. Helpful documents: - [Roman Zolotarev: Edit text with vi(1)](https://rgz.ee/vi.html)
- [Hugo Daniel: vi is not vim](https://hugodaniel.pt/posts/vi-is-not-vim/)
- [Jeff W: vi help](http://www.jeffw.com/vi/vi_help.txt) / [Maarten Litmaath: vi reference](http://www.ungerhu.com/jxh/vi.html)
- [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)
- [The vi archive and FAQ (mirror)](http://git.larryhynes.net/vi/) #### Yank / delete an arbitrary number of lines 1) mark the first line: mk 2) move to last line 3a) yank: y'k 3b) delete: d'k 4) move to destination line 5) put with P or p #### Apply regex to an arbitrary number of lines 1) mark the first line: mk 2) mark the last line: ml :'k,'ls/regex/power/g #### Add # to block of text :'k,'ls/^/#/ #### Remove trailing whitespace of block of text :'k,'ls/\ *$// #### Remove the first N (5) characters from every line :%s/^.\{0,5\}// #### Search and replace PATHs, using different delimiter :%s#/usr/local/log#/var/log#g #### Write file as root :w !doas tee % #### Diff the file on disk with the file in the buffer :w !diff -u % - #### Make a backup of the file on disk :!cp % %.bak #### Sort all lines :%!sort #### Sort paragraph !}sort } won't be shown in the command. #### Sort from current line to EOF !Gsort G won't be shown in the command. #### Uniq all lines :%!uniq #### Uniq paragraph !}uniq } won't be shown in the command. #### Uniq from current line to EOF !Guniq G won't be shown in the command. #### Underline all lines starting with CHAPTER :g/^CHAPTER /t.|s/./=/g #### Search for "pattern", print the containing function (start with def) and line number :g/pattern/?^ *def ?# #### Add # to paragraph containing "pattern" :g/pattern/?^$?+,//-s/^/# #### Sort content of multiline CSS blocks :g/{$/+,/^}/-!sort #### Sort content of multiline CSS blocks (media queries) :g/^[^@].*{$/+,/}/-!sort #### Reformat HTML paragraphs to a fixed width (40) :g/

/+,/<\/p>/-!fmt -40 #### Swap "Lastname, Firstname" to "Firstname, Lastname" :%s/\(.*\), \(.*\)/\2 \1/ #### Change all text to lowercase :%s/.*/\L&/ #### Join all lines :%j #### Copy (t) or move (m) lines containing "pattern" :g/pattern/t$ :g/pattern/m$ #### Select a column (3rd) from formated text seperated by ':' :%!awk -F':' '{print $3}' #### Insert the sum of a list of numbers after an arbitrary number of lines 1) mark the first line: mk 2) mark the last line: ml :'k,'l!awk 'END{print "Total:", i}{i+=$1; print}' More compact version: :'k,'l!awk 'END{print "Total:", i} ++i || 1' #### Email the current paragraph :?^$?+,//-w !mail -s "" email@example.com #### Enable and use ex history 1) Set ESC key to enable history, or add to .exrc: :set cedit= 2) Use it with: :