Question

I have a simple table of data in VIM consisting of two sets of variable-length strings. For example:

test bob
check mike
review tom
test2 tom
presentation mike

I have been able to make it "prettier" by piping it through column, by selecting all five lines in VIM using CTRL+V, selecting all five entire lines visually, then hitting the SHIFT+: key combination to enter command mode, and then issue the command '<,'>!column -t, which makes it look like:

test          bob
check         mike
review        tom
test2         tom
presentation  mike

Now, I would like to finally sort by the second column. I tried doing a visual block selection, and using '<,'>!sort -n -k2, but I just get:

check         mike
presentation  mike
review        tom
test          bob
test2         tom

So, it sorts, but only by the first column. Thinking this uses perhaps the literal column number rather than assuming SPACE as a delimiter, and accounting for consecutive delimiters, I tried again with '<,'>!sort -n -k15, but I still get the same undesired behavior.

How can I get this to sort by the second column, either by the literal column number (ie: 15), or by the visual column number (ie: 2)?

Thank you.

Était-ce utile?

La solution

this command:

%sor r /\S\+$/

generates:

test          bob
check         mike
presentation  mike
review        tom
test2         tom

check :h :sort for details

your block-wise visual selection won't help sorting (either built-in or external). because it passed a range, which is line based, even though the selection looks in block/column wise. :h range for detail. If you want to use external sort, you should do exactly same as in shell.

Autres conseils

Try setting the separator like this:

'<,'>!sort -t' ' -k2

(not that there is no -n flag)

From the manual (man sort):

-t, --field-separator=SEP use SEP instead of non-blank to blank transition

For me it makes:

check         mike
presentation  mike
review        tom
test2         tom
test          bob

Become:

test          bob
presentation  mike
check         mike
review        tom
test2         tom
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top