I use M-x align-regexp in emacs to prettify my Perl code, but by default it is using tabs instead of spaces, which is something one should not do according to Perl critic.

Is there a way to change the behaviour of align-regexp so that it fills in with the right amount of spaces instead of tabs?

share|improve this question
7  
Try (setq indent-tabs-mode nil). – abo-abo Mar 28 '14 at 10:22
up vote 4 down vote accepted

In general you should avoid using advice, but since align.el directly reads the value of indent-tabs-mode, it's probably the best way:

(defadvice align-regexp (around align-regexp-with-spaces activate)
  (let ((indent-tabs-mode nil))
    ad-do-it))

Here was my original version:

(defadvice align-regexp (around align-regexp-with-spaces activate)
  (let ((old-indent-tabs-mode indent-tabs-mode))
    (setq indent-tabs-mode nil)
    ad-do-it
    (setq indent-tabs-mode old-indent-tabs-mode)))

As @Phils pointed out, this is unnecessarily complex and less fool proof, so use the code at the top of the post.

share|improve this answer
2  
Uh, just let-bind indent-tabs-mode directly. Like this: stackoverflow.com/a/8129994/324105. What you're doing is almost identical, but unnecessarily long-winded, and relying (potentially unsafely) on an additional variable. – phils Aug 6 '14 at 21:24
    
@phils Derp, I really should know better by now. Thanks for the suggestion and the link. :) – Gordon Gustafson Aug 7 '14 at 2:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.