Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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


A 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 ...


Gsort 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.


>Vim is one of the few tools that keeps rewarding me more and more, the longer I use it.

True :-)


Edit 2: Also, vim has a built-in sort command:

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.


Edit: 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).


M-| on emacs. I use it all the time for "diffstat".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: