Monday, November 17, 2025

Re: sort in .tsv file

On 17.11.25 17:30, Tim Chase wrote:
> On 2025-11-17 21:54, Vim Users wrote:
> > the /pattern/ feature of sort doesn't work if the recognized filetype
> > is .tsv (which gives alternate colours for successive fields).
> >
> > Is there a workaround, apart from copying it to a plain file and
> > working on it there?
>
> I'm not sure if you have some plugin interfering, but when I open a TSV
> file with the following contents:
>
>     $ cat test.tsv
>     h1 h2 h3 h4 h5
>     d  c  b  a  1
>     c  d  a  b  30
>     b  a  d  c  4
>     a  b  c  d  200
>
> I can issue the following commands to sort by the various columns:
>
>   :2,$sort  " sort by the first column
>   :2,$sor /[^^I]*^I/   " sort by the second column
>   :2,$sor /\([^^I]*^I\)\{2}/ " sort by third column
>   :2,$sor /\([^^I]*^I\)\{3}/ " sort by the fourth column
>   :2,$sor n /\([^^I]*^I\)\{4}/ " sort numerically by the fifth column
>

Despite having used Vim for several decades now, I'm still allergic to the effort to make Vim into Emacs, by doing stuff in-house in cases where it's much easier to just use what's already provided on any *nix distro. To perform those four sorts in Vim, with fewer keystrokes, this works for me:
Place the cursor on the first line after the column headings, then:
!}sort -k 1
Then hit 'u' for Vim undo, and:
!}sort -k 3
or whatever field you now want.
And without the undo, we've done a compound sort.
The "sort" manpage describes the various available sort criteria: dictionary, numeric, month, etc.
A "man -k sort" in another shell reveals alternative sorts you might have on hand.
Equally important, I feel, is that only one sort syntax and behaviour needs to be learnt for all common uses on the computer, whether in Vim, in commandline pipes, or shell scripts. Why buy into unnecessary brain fragmentation?

Erik

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/a42ef4ad-c7e4-4ed3-9bba-0394d692af6a%40localhost.

No comments: