1

I've got some text like:

...many papers due to their interpretability and relatively low cost
\footnote{\citealt{
    alviPakistanSignLanguage2007,
    atzoriNinaproDatabaseResource2015,
    baudelCharadeRemoteControl1993,
    feinerVisualizingDimensionalVirtual1990,
    felsBuildingAdaptiveInterfaces1990,
    felsGloveTalkIIaNeuralnetworkInterface1998,
    frankhofmannSensorGloveAnthropomorphicRobot1995,
    heumerGraspRecognitionUncalibrated2007,
    immersioncorporationCyberGlove2001,
    kadousGRASPRecognitionAustralian1995,
    kesslerEvaluationCyberGloveWholehand1995,
    laviolaSurveyHandPosture1999,
    leeDeepLearningBased2020,
    leeSmartWearableHand2018,
    liangSignLanguageRecognition1996,
    mardiyantoDevelopmentHandGesture2017,
    murakamiGestureRecognitionUsing1991,
    rung-hueiliangRealtimeContinuousGesture1998,
    takahashiHandGestureCoding1991,
    wenMachineLearningGlove2020,
    wiseEvaluationFiberOptic1990,
    yuanHandGestureRecognition2020
}}.

Accelerometers measure linear acceleration in up to three axes. Inertial...

And I'd like to sort all those citations by their year in increasing order. I don't want to change the citation keys to start with the year. Is there some clever way to sort them in vim?

2 Answers 2

3

Ignore anything that's not a number

You can use :sort n that will ignore anything until the first (decimal) number as described in :help sort:

vi{
:'<,'>sort n

I noticed this will even sort the citations belonging to the same year alphabetically so this is clearly the best solution.

It was found by beyarkay and posted as a comment. I include it in my answer for completeness.

Ignore by pattern

It's also possible to give :sort a pattern to be ignored.

To ignore letters and the hyphen, you could do something like my original answer below:

:'<,'>sort /[a-zA-Z-]\+/

or the much more elegant pattern proposed by D. Ben Knoble:

:'<,'>sort /\D\+/

Finishing touches

No matter which approach you use, you may end up with a result somewhat like below. As :sort orders lines, the line not terminated by a comma may end up in the middle of the list. I'd just manually double-check and fix it.

...many papers due to their interpretability and relatively low cost
\footnote{\citealt{
    feinerVisualizingDimensionalVirtual1990,
    felsBuildingAdaptiveInterfaces1990,
    wiseEvaluationFiberOptic1990,
    takahashiHandGestureCoding1991,
    murakamiGestureRecognitionUsing1991,
    baudelCharadeRemoteControl1993,
    kadousGRASPRecognitionAustralian1995,
    kesslerEvaluationCyberGloveWholehand1995,
    frankhofmannSensorGloveAnthropomorphicRobot1995,
    liangSignLanguageRecognition1996,
    felsGloveTalkIIaNeuralnetworkInterface1998,
    rung-hueiliangRealtimeContinuousGesture1998,
    laviolaSurveyHandPosture1999,
    immersioncorporationCyberGlove2001,
    heumerGraspRecognitionUncalibrated2007,
    alviPakistanSignLanguage2007,
    atzoriNinaproDatabaseResource2015,
    mardiyantoDevelopmentHandGesture2017,
    leeSmartWearableHand2018,
    yuanHandGestureRecognition2020      % <--- add a comma
    leeDeepLearningBased2020,
    wenMachineLearningGlove2020,        % <--- remove a comma
}}.

Accelerometers measure linear acceleration in up to three axes. Inertial...
6
  • 1
    :D this is exactly what I was hoping for, thanks so much! You can see my horribly inelegant attempt, I'm really glad I decided to ask (and learn something new) instead of sticking with what I'm comfortable with.
    – beyarkay
    Nov 21, 2023 at 12:26
  • 1
    Glad I could help. BTW, I learned something new as well.
    – Friedrich
    Nov 21, 2023 at 12:31
  • 2
    Looking through :h :sort, it seems like Bram gave us something even better: :sort n will ignore everything before the first number (which is exactly what we want). There's also options for floats, hexadecimals, octals, etc
    – beyarkay
    Nov 21, 2023 at 12:35
  • 1
    Oops, I must have skipped that :) Feel free to post an answer and accept it.
    – Friedrich
    Nov 21, 2023 at 12:37
  • 2
    @D.BenKnoble yes, there's one: "rung-hueiliang..." and I tripped on it on my first attempt. I'm guilty of not having spent any effort optimizing my regex, though. Your /\D\+/ works great. I'll edit my answer.
    – Friedrich
    Nov 22, 2023 at 7:10
1

So I think I found an answer, but I'd be grateful to anybody with better vim-foo if they can find something better

After the section in {} is selected:

'<,'>s/\(.\+\(\d\d\d\d\)\)/\2\1/|'<,'>sort|'<,'>norm 4x

To explain:

  • '<,'>s/\(.\+\(\d\d\d\d\)\)/\2\1/ copy the year and paste it to the front
  • '<,'>sort sort the citations
  • '<,'>norm 4x delete the leading 4 digits which are the year.

It could be simplified a bit but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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