Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align arguments to functions in emacs?

Say if I have the following:

func(arg1, arg2, arg3...)
func(longargarg1, longerarg2, arg3,...)
...

How do I align the arguments so that it's like following?

func(arg1       , arg2      , arg3...)
func(longargarg1, longerarg2, arg3,...)
...

[I can use M-x align-regex to align the first argument, but I cannot cook up with a suitable regex to align the rest of the arguments. Bonus point if the answer also take cares of the case when some arguments are strings with commas in them.]

like image 938
polyglot Avatar asked Jun 09 '09 15:06

polyglot


1 Answers

Select the region, then:

C-u M-x align-regexp RET ,\(\s-*\) RET RET RET y

The regexp says to align commas with spaces following them. The default value of 1 for the paren group to modify means insert spaces where the \(\s-*\) is, the default value of 1 for spaces to adjust means have one space after the longest expansion, and you want it repeated throughout the line.

like image 197
scottfrazer Avatar answered Oct 24 '22 04:10

scottfrazer