Reman
Reman

Reputation: 8119

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?

Upvotes: 7

Views: 2018

Answers (1)

Benoit
Benoit

Reputation: 79215

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

Upvotes: 10

Related Questions