Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort comma separated words in Vim

Tags:

vim

sorting

In Python code, I frequently run into import statements like this:

from foo import ppp, zzz, abc

Is there any Vim trick, like :sort for lines, to sort to this:

from foo import abc, ppp, zzz
like image 666
Ashwin Nanjappa Avatar asked Aug 30 '17 08:08

Ashwin Nanjappa


3 Answers

Yep, there is:

%s/import\s*\zs.*/\=join(sort(split(submatch(0), '\s*,\s*')),', ')

The key elements are:

  • :h :substitute
  • :h /\zs
  • :h s/\=
  • :h submatch()
  • :h sort()
  • :h join()
  • :h split()

To answer the comment, if you want to apply the substitution on a visual selection, it becomes:

'<,'>s/\%V.*\%V\@!/\=join(sort(split(submatch(0), '\s*,\s*')), ', ')

The new key elements are this time:

  • :h /\%V that says the next character matched shall belong to the visual selection
  • :h /\@! that I use, in order to express (combined with \%V), that the next character shall not belong to the visual selection. That next character isn't kept in the matched expression.

BTW, we can also use s and i_CTRL-R_= interactively, or put it in a mapping (here triggered on µ):

:xnoremap µ s<c-r>=join(sort(split(@", '\s*,\s*')), ', ')<cr><esc>
like image 98
Luc Hermitte Avatar answered Nov 13 '22 20:11

Luc Hermitte


Alternatively, you can do the following steps:

  1. Move the words you want to sort to the next line:

    from foo import 
    ppp, zzz, abc
    
  2. Add a comma at the end of the words list:

    from foo import 
    ppp, zzz, abc,
    
  3. Select the word list for example with Shift-v. Now hit : and then enter !xargs -n1 | sort | xargs. It should look like this:

    :'<,'>!xargs -n1 | sort | xargs
    

    Hit Enter.

    from foo import 
    abc, ppp, zzz,
    
  4. Now remove the trailing comma and merge the word list back to the original line (for example with Shift-j).

    from foo import abc, ppp, zzz
    

There are Vim plugins, which might be useful to you:

  • AdvancedSorters : Sorting of certain areas or by special needs.
like image 27
Mateusz Piotrowski Avatar answered Nov 13 '22 20:11

Mateusz Piotrowski


I came here looking for a fast way to sort comma separated lists, in general, e.g.

    relationships = {
        'project', 'freelancer', 'task', 'managers', 'team'
    }

My habit was to search/replace spaces with newlines and invoke shell sort but that's such a pain.

I ended up finding Chris Toomey's sort-motion plugin, which is just the ticket: https://github.com/christoomey/vim-sort-motion. Highly recommended.

like image 2
Jeremiah Boyle Avatar answered Nov 13 '22 20:11

Jeremiah Boyle