I like to use vi as a kind of IDE for shell commands, because you can, for example,
!}somecommand
to pipe a paragraph through that command, or
1G!Gsomecommand
to pipe the whole editing buffer through that command, which is also equivalent to
:1,$!somecommand
which I find less obvious as a way to do that, even though it makes sense historically in terms of ed and ex.
You can also, in vim, use u and ^R to undo your pipe transformation and redo it, so you're basically then doing a series of shell pipe transformations with interactive history.
If you need to generate text with a shell command instead of pipe existing text through it, you can use
:r!somecommand
Commands that I like to use in this context include tac, rev, tr, cut, grep, and sed (although using sed this way is a little silly since the substitutions I'm doing are normally also available natively inside of vi itself).
A simple example that I like a lot is
1G!Ggrep .
which removes all empty lines from the current file. ("Line 1 go to line, pipe from here to result of movement go to end of file, command 'grep .' [print lines that match 'any character']")
The classic ed/ex way to do this is
:1,$g/^$/d
which is just as many characters, but which requires lots of explicit thought for me, while the "1G!G grep ." version seems to no longer require explicit thought at all. ("For lines 1 to last line, repeatedly match ^$ [line beginning immediately followed by line end], delete matching lines.")
!}somecommand
to pipe a paragraph through that command, or
1G!Gsomecommand
to pipe the whole editing buffer through that command, which is also equivalent to
:1,$!somecommand
which I find less obvious as a way to do that, even though it makes sense historically in terms of ed and ex.
You can also, in vim, use u and ^R to undo your pipe transformation and redo it, so you're basically then doing a series of shell pipe transformations with interactive history.
If you need to generate text with a shell command instead of pipe existing text through it, you can use
:r!somecommand
Commands that I like to use in this context include tac, rev, tr, cut, grep, and sed (although using sed this way is a little silly since the substitutions I'm doing are normally also available natively inside of vi itself).
A simple example that I like a lot is
1G!Ggrep .
which removes all empty lines from the current file. ("Line 1 go to line, pipe from here to result of movement go to end of file, command 'grep .' [print lines that match 'any character']")
The classic ed/ex way to do this is
:1,$g/^$/d
which is just as many characters, but which requires lots of explicit thought for me, while the "1G!G grep ." version seems to no longer require explicit thought at all. ("For lines 1 to last line, repeatedly match ^$ [line beginning immediately followed by line end], delete matching lines.")