Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort in vim while ignoring articles ('A' and 'The')

Tags:

vim

I have a vim file that looks like the following:

ABBA
Duran Duran
The Beatles
The Rolling Stones
Styx

Using vim's sort command results in this output:

ABBA
Duran Duran
Styx
The Beatles
The Rolling Stones

What I want to do is to ignore articles like 'A' and 'The' when sorting. So expected output would be like this

ABBA
The Beatles
Duran Duran
The Rolling Stones
Styx
like image 695
rainman_s Avatar asked Jul 01 '17 12:07

rainman_s


People also ask

How do I sort data in Vim?

Sorting text in Vim is easy! Select the text, then press : , type sort , then hit enter! It'll sort the whole document by default, but you can enter a range too.

How do I sort a specific column in vim?

column happens to be the first column with a decimal number, note that you could use simply :sort n , as the help says: "With [n] ... sorting is done on the first decimal number in the line..." This doesn't apply to your case, but might to somebody elses.


1 Answers

look at :help :sort

When /{pattern}/ is specified and there is no [r] flag the text matched with {pattern} is skipped, so that you sort on what comes after the match. Instead of the slash any non-letter can be used.

:sort /^\(A \|The \)*/

This make sort of:

(ABBA)
The (Beatles)
(Duran Duran)
The (Rolling Stones)
(Styx)
like image 114
mattn Avatar answered Oct 13 '22 20:10

mattn