How to comment a block of code in Emacs?

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.


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.


You want to select the code region and then M-;