why-vi.rocks/index.md

73 lines
1.5 KiB
Markdown
Raw Normal View History

# Why vi Rocks
2019-04-06 16:45:57 +02:00
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.<br />
These all work with at least [nvi](https://en.wikipedia.org/wiki/Nvi) 1.79 and 2.1.3 (unicode).
2019-04-06 23:43:20 +02:00
Helpful documents:<br />
- [Roman Zolotarev: Edit text with vi(1)](https://rgz.ee/vi.html)<br />
2019-04-06 23:50:55 +02:00
- [Jeff W: vi help](http://www.jeffw.com/vi/vi_help.txt)<br />
2019-04-07 09:26:42 +02:00
- [Maarten Litmaath: vi reference](http://www.ungerhu.com/jxh/vi.html)<br />
- [alphanrrrd: extremely concise cheatsheet](http://www.alphanrrrd.org/vi.html)<br />
2019-04-06 23:50:55 +02:00
- [ViEmu: Graphical vi cheatsheet](http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html)
2019-04-06 18:21:54 +02:00
#### Yank / delete an arbitrary number of lines
2019-04-06 16:45:57 +02:00
1) mark the first line: mk
2) move to last line
3a) yank: y'k
3b) delete: d'k
4) move to new line
5) paste with P or p
2019-04-06 18:21:54 +02:00
#### Apply regex to an arbitrary number of lines
2019-04-06 16:45:57 +02:00
2019-04-06 18:21:54 +02:00
1) mark the first line: mk
2) mark the last line: ml
:'k,'ls/regex/power/g
2019-04-06 16:45:57 +02:00
2019-04-06 23:00:29 +02:00
#### Add # to block of text
:'k,'ls/.*/#&/
#### Remove trailing whitespace of block of text
:'k,'ls/\ *$//
2019-04-06 18:21:54 +02:00
#### Write file as root
2019-04-06 16:45:57 +02:00
:w !doas tee %
2019-04-06 21:51:17 +02:00
#### Diff the file on disk with the file in the buffer
2019-04-06 16:45:57 +02:00
:w !diff -u % -
#### Make a backup of the file on disk
2019-04-06 18:21:54 +02:00
:!cp % %.bak
2019-04-06 16:45:57 +02:00
2019-04-06 18:21:54 +02:00
#### Sort all lines
2019-04-06 16:45:57 +02:00
:%!sort
2019-04-06 18:21:54 +02:00
#### Sort paragraph
!}sort
#### Uniq all lines
2019-04-06 16:45:57 +02:00
:%!uniq
2019-04-06 18:21:54 +02:00
#### Uniq paragraph
!}uniq
#### Join all lines
:%j
#### Select a column (3rd) from formated text seperated by ':'
:%!awk -F':' '{print $3}'
2019-04-06 16:45:57 +02:00