Sort two-line file blocks

I have an input file (c) with the following contents:

/** doxygen comment for enum member1 */ COMMON_PREFIX_name1 = 1, /** doxygen comment for enum member2 */ COMMON_PREFIX_name2 = 2, /** doxygen comment for enum member3 */ COMMON_PREFIX_name3 = 3, /** doxygen comment for enum member4 */ COMMON_PREFIX_name4 = 4, ... 

Can I use Vim to sort definitions by name1 ... name In any case, and keep comments on the definitions?

+7
source share
1 answer

I propose the following sequence of actions.

  • Connect the paired lines.

     :g/^\s*COMMON_PREFIX_/-j! 
  • It is enough to sort the sorted rows by variable names.

     :sort/\*\/\s*COMMON_PREFIX_/ 
  • Split sorted rows back.

     :g/\*\/\zs\ze\s*COMMON_PREFIX_/s//\r/ 

You can run all three commands as one:

 :exe'g/^\s*COMMON_PREFIX_/-j!' | sort/\*\/\s*COMMON_PREFIX_/ | g/\*\/\zs\ze\s*COMMON_PREFIX_/s//\r/ 
+6
source

All Articles