11

Normal command :sort can sort lines based on column or virtual column (\%c or \%v), could the higher level logical column be used as sorting key? Using regular expression looks a little complex for this scenario (the column is around the end of the line?) and it looks similar as what the sort utility does (sork -k), but sort with this functionality is unavailable on Windows. Vim plug-in will also help.

For example, I'd like to sort the 2 lines below according to the last column separated by comma. My real scenario has much more columns and string pattern. Specify column delimiter will simplify it a lot.

xxx,yyy,zzz,0x123
zzxz,xxxx,yyyy,0x121
11

Vim's sort allows you to either skip {pattern}, or only consider it (with the r flag). A regular expression for the last comma-delimited column is easy to formulate: Skip everything until and including the last comma in a line:

:sort/.*,/

For any other column, I would use the r flag, and skip N (here: 2) previous columns via \zs:

:sort/\([^,]*,\)\{2}\zs[^,]*/
  • 1
    Since the last column happens to be a hex number using the x flag might also be helpful. – jecxjo May 22 '15 at 19:22
  • This looks great. The last column may be a special case. Any simple representation for the middle columns? – Thomson May 23 '15 at 3:15
  • I thought you were able to come up with something for that, but here you are. Please accept my (edited) answer by clicking on the outlined checkmark next to it. Thanks! – Ingo Karkat May 23 '15 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.