deadghost
deadghost

Reputation: 5227

vim/c - How can I put functions in alphabetical order?

I have a .c file with a bunch of functions. Is there some way to put these functions in alphabetical order in vim?

Upvotes: 4

Views: 650

Answers (2)

AJ Dhaliwal
AJ Dhaliwal

Reputation: 721

If you use the vim Taglist plugin. Which is the top rated and most downloaded plugin for vim.

http://vim-taglist.sourceforge.net/

Then you can easily view the functions ordered alphabetically or in the order they are defined.

By adding the following line to your vimrc file, the default order will be alphabetical.

let Tlist_Sort_Type = "name"

You can press the 's' key in the Taglist tab to toggle the order.

search for "Tlist_Sort_Type" in the link below for the official docs:

http://vim-taglist.sourceforge.net/manual.html

Upvotes: 0

hari
hari

Reputation: 1439

If you have lots and lots of functions in one file and you want do it automatically, then we can do it one more "Long" way. Again it depends on how the code is organized. Let say if code is always organized like:

<return Type>
FunctionName(Arguments)
{
/* Code */
}
  1. Using record feature of VIM, copy all the function names (Complete Line) to a new Split Window. (q a [[ k yy Ctrl-WW p Ctrl-WW j)
  2. Sort the new split window. (:sort)
  3. Record one more macro such that delete first line of Sorted-Split window, copy as a search string in the C file, copy the complete function and paste it at the End of File. (q b yy Ctrl-WW /Ctrl-r 0 k m f jj % d'a G p Ctrl-WW) - Now run this macro number of lines in new window

i know this is not efficient way just giving one more way to do it :)

Upvotes: 2

Related Questions