Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: Sort by Function name

I have a lot of functions in my menu.vim file.
Would it be possible to select them (by me) and sort them by function name?

p.e.
find lines from fun! or function!
to line endfun

find name of function and sort the whole block based on the name of function (from a-z)

Sometimes I have one line of comment above the function name.
Can it be taken with it?

like image 371
Reman Avatar asked Apr 11 '11 08:04

Reman


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 rearrange lines in Vim?

Mappings to move lines In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.


1 Answers

This could be a bit tricky but here is a tentative way to do it:

  • choose a substitution pattern that is not in your source file, like @@@
  • g/fun\%[ction]!\= /,/endf\%[unction]/ s/$\n/@@@
  • Then, select all one-liners and use '<,'>sort /fun\%[ction]!\=/
  • %s/@@@/\r/g
like image 200
Benoit Avatar answered Sep 29 '22 16:09

Benoit