I need to comment out some blocks of code in Python. How can I do that with emacs? How can I prepend # character at the start of each line of a block?
4 Answers
You can use the comment-region command using
M-x comment-region
Edit: And as suggested by @Gilles in comment you can use M-;
which according to the help is
Call the comment command you want (Do What I Mean).
If the region is active and 'transient-mark-mode' is on, call 'comment-region' (unless it only consists of comments, in which case it calls 'uncomment-region'). Else, if the current line is empty, call 'comment-insert-comment-function' if it is defined, otherwise insert a comment and indent it. Else if a prefix arg is specified, call 'comment-kill'. Else, call 'comment-indent'.
which is probably easier on the long run. :-) Remember that this is "mode-dependant", so you need to set python-mode before you comment using M-x python-mode
Or if you want to prefix every line with any type of character, select the text you want to comment and type
C-x r t
and type the character you want to prefix with. Remember that the caret must be on the first column on the last line that you select, or your text would be replaced.
You select text by pressing C-space and moving your caret around btw.

- 17,669
- 6
- 70
- 85
-
2
-
9There's also `M-;` (`comment-dwim`), which comments the region, (except when the region is already a comment: then it uncomments). – Gilles 'SO- stop being evil' Aug 19 '10 at 21:45
-
1But comment-region inserts the comment char(s) at the first non-white-space position. What if I want to insert the comment chars at the leftmost position on each line? – Mike Makuch Oct 18 '16 at 18:18
Here's a link that describes how to do this with arbitrary characters in rectangle mode.
This is handy because it only does this for the selected region of text.

- 6,991
- 7
- 42
- 63
For those of you that want to achieve this sort of thing but perhaps for more complicated scenarios you might find the following link helpful:
One way would be to define a simple keyboard macro
C-x (
C-a
#
C-n
C-x )
then you can execute it over 100 lines with
M-1 0 0 C-x e
not necessarily the most efficient for this case, but easy to remember

- 69,903
- 20
- 143
- 156
-
or a variant which will apply it to the region: `F3 # F4` then mark the region, then `C-x C-k r` – phils Aug 19 '10 at 22:33
-
4But you'd be reinventing the wheel: 'comment-region' is one of the most fundamental commands in Emacs. I use it hundreds of times a day. – Joel J. Adamson Aug 20 '10 at 13:05
-
+1. Not sure why it's downvoted. If you just need to get the job done but forget a shortcut, macros are a great and versatile workaround. – armandino Nov 05 '13 at 19:00
-
Probably because it answers the question @OP asked, and not the question he meant to ask, which was about commenting out regions of code. And the answer to that is simply to mark the lines, and use M-; or M-x comment-region. That's clearly what he meant, and you can uncomment a region using M-; too, so it's a flat out superior solution. – Haakon Løtveit Feb 14 '16 at 14:42