Say you have some text like this:
## countries, primary
United States,Washington,North America
Canada,Ottawa,North America
Australia,Canberra,Oceania
## countries, secondary
France,Paris,Europe
Japan,Tokyo,Asia
United Kingdom,London,Europe
And you want to filter only the non-comment lines through an external program, say column -ts,
, to align the comma-separated fields. In other words, the expected output is:
## countries, primary
United States Washington North America
Canada Ottawa North America
Australia Canberra Oceania
## countries, secondary
France Paris Europe
Japan Tokyo Asia
United Kingdom London Europe
Is it possible to do this in vim with a single command?
I thought maybe combining :g
with :range!
would work, but it doesn't:
g!/^\s*#/!column -ts,
The above just runs :!column -ts,
once for each line that matched the :g
command. In other words, it doesn't actually filter anything, it just runs a system command with stdin connected to the terminal, once for each matching line.
So, the general question is, how can we filter discontinuous line sets through an external program, passing all the lines as one block to the stdin of the external program, and returning the output lines back to their corresponding line numbers in the buffer?
:g
works line by line. You can however filter the range throughsed '/^#/d' | columns -ts,
.