Commenting out line or region in Emacs

What I always wanted: commenting or uncommenting a line or region using emacs. Of course, there is comment-region, but this is much nicer:

(defun comment-or-uncomment-line-or-region ()
"Comments or uncomments the current line or region."
(if (region-active-p)
(comment-or-uncomment-region (region-beginning) (region-end))
(comment-or-uncomment-region (line-beginning-position) (line-end-position))

(define-key c-mode-base-map (kbd "C-/") 'comment-or-uncomment-line-or-region)

See also the corresponding Stackoverflow question.

Leave a Reply

Your email address will not be published.