I see this done a lot. Don't know what it's called. Don't know how to do it.

See how the comments are all lined up?

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f;    // Floating point number
char myLetter = 'D';         // Character
boolean myBool = true;       // Boolean
String myText = "Hello";     // String

I am positive people aren't holding down the space key to get these comments lined up. What magic are they using?

2 Answers 2


align-regexp is the interactive function that you can use. Here's how you do it

  1. Select the region that you want to apply the indent
  2. M-x align-regexp which will prompt you for a regexp
  3. Enter // and press enter

Language modes usually work with the comment-* functions innewcomment.el to allow such things (and more).

E.g. assume that the above is a snippet of Javascript code and that the buffer is indeed in Javascript mode (check the mode line to see if that is indeed the case). Then go to some line and type M-;: that will align the comment on a fixed column (the global default is column 32, but language modes are free to set up their own value - do C-h v comment-column to see the doc string of the variable that controls this).

The Emacs manual contains a chapter on Editing Programs which contains a section on Manipulating comments. I highly recommend that you go through this section for this particular question and the whole chapter if you are going to be using Emacs as a programming editor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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