3 ms·
I find "!" fun. Mark a region (or spell it out in the : prompt), and use it to run the region through any external script/binary which takes STDIN and emits to
by csirac2 12y ago
I find "!" fun. Mark a region (or spell it out in the : prompt), and use it to run the region through any external script/binary which takes STDIN and emits to STDOUT.
Eg.:
:%! perltidy # runs whole buffer through perltidy
:123,456! jsontidy # run lines 123..456 through jsontidy
!} htmltidy # htmltidy to end of para
- vram22 12y agoA nice use of the ! command in vim, is if you have a text files with lines and you want to sort it; do this, in command mode: !Gsort The ! command can also sort just a range of lines (in place, in the file), which can be even more useful. The range can be selected either by start and end line numbers, as in the parent comment's 2nd example, or by start and end marks (set by ma, mc, etc.) or even by a combination of the two, e.g. starting line number, end mark: :'a,'b!sort :123,'b!sort Another example - turn a list of constant definitions in your code, into uppercase: !Gtr [a-z] [A-Z] And so on ...
- vram22 12y agoEdit: After reading the OP, I realized that my example of uppercasing constant definitions, can be simpler with this vim command, which doesn't shell out to the tr command: gUG (uppercase U in the command, as opposed to the OP's use of guG - lowercase u).
- vram22 12y agoEdit 2: Also, vim has a built-in sort command: http://vim.wikia.com/wiki/Sort_lines http://vim.wikia.com/wiki/Sort_lines , so for the sort example I gave, too, you may not need to shell out to the external sort command. On the other hand, you may want to, since it has more functionality. Plus, vim doesn't have every filter command that Unix does, so the ! command is still very useful.
- csirac2 12y agoGsort is cool! I usually defer to my blunt instrument approach of just doing ! sort. Vim is one of the few tools that keeps rewarding me more and more, the longer I use it.
- vram22 12y ago>Vim is one of the few tools that keeps rewarding me more and more, the longer I use it. True :-)
- __david__ 12y agoM-| on emacs. I use it all the time for "diffstat".