Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Emacs, how to comment a block of text, i.e. how to indent a block of text rightward by n whitespace characters and put a chosen character sequence, say ##, at the beginning of each line?


bar foobar
   foo bar


# foo
# bar foobar
#    foo bar
  • I need a method that doesn't interfere with CUA mode.
  • comment-dwin doesn't allow me to choose arbitrary character sequence.
share|improve this question

2 Answers 2

up vote 10 down vote accepted

One way is to use string-rectangle (C-x r t).

Mark the block, then call string-rectangle and give ## as a string to insert.

share|improve this answer
Here are more concrete steps: 1) move cursor to beginning 2) C-M-@ 3) move cursor to end 4) C-x r t 5) type prefix character sequence, such as ## , press ENT –  qazwsx Jan 18 '12 at 23:25

Depending on what mode you are running (e.g. CC Mode, Lisp Mode) you can select the region and then use the command comment-region (usually bound to C-c C-c. If everything is set correctly, it should use the appropriate comment characters for the type of file you are editing.

share|improve this answer
How is this different from M-;? –  qazwsx Jan 19 '12 at 19:30
@duperuser M-; is a more general commenting command. When there is no active region, it inserts a comment (after any statements on the line, and lined up with the comment column). When a region is selected, it behaves like comment-region. When there is no active region, comment-region does not insert a comment. –  jwernerny Jan 19 '12 at 19:43

Your Answer


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.