44

I know there's already an Emacs question on this, and that it was closed, but I find it quite relevant and important.

Basically, I want to comment/uncomment the current line. I was expecting this to be fairly easy with a macro, but I found that it really isn't.

If the current line is commented, uncomment. If it is uncommented, comment it. And I would also to comment out the whole line, not just from cursor position.

I tried a macro like this:

C-a

'comment-dwim

But this only work to comment a line, not to uncomment it, if it's already commented.

I'm not sure of how easy it is, but if there's some way, I'd really like it.

Also, the reason I love this idea so much is that when I used Geany, I just used C-e and it was perfect.

3
  • 2
    See the "see also" at the bottom of (emacswiki.org/emacs/CommentingCode). il-debug looks pretty handy.
    – ccoakley
    Mar 13, 2012 at 17:57
  • 9
    Emacs 25 has comment-line bound to C-x C-;. Oct 16, 2016 at 17:41
  • If you like Geany comment / uncomment, you should select some lines with shift and arrows then use M-; the combinations C-x C-; or C-a C-; does not work.
    – F.Tamy
    Jul 23, 2018 at 7:58

6 Answers 6

99

Trey's function works perfectly, but it isn't very flexible.

Try this instead:

(defun comment-or-uncomment-region-or-line ()
    "Comments or uncomments the region or the current line if there's no active region."
    (interactive)
    (let (beg end)
        (if (region-active-p)
            (setq beg (region-beginning) end (region-end))
            (setq beg (line-beginning-position) end (line-end-position)))
        (comment-or-uncomment-region beg end)))

It comments/uncomments the current line or the region if one is active.


If you prefer, you can modify the function to jump to the next line after (un)commenting the current line like this:

(defun comment-or-uncomment-region-or-line ()
    "Comments or uncomments the region or the current line if there's no active region."
    (interactive)
    (let (beg end)
        (if (region-active-p)
            (setq beg (region-beginning) end (region-end))
            (setq beg (line-beginning-position) end (line-end-position)))
        (comment-or-uncomment-region beg end)
        (next-line)))

Note that only thing that's changed is the added next-line command at the end of the function.

9
  • 4
    This could actually be better if after commenting a line, the cursor moved to the next line, so repeatedly hitting a key binding would comment consecutive lines. Any tips for doing this? Mar 25, 2012 at 0:47
  • 3
    @justingordon I edited an alternative version under the original. Enjoy!
    – Gerstmann
    Mar 26, 2012 at 5:48
  • 2
    This is great! It should be the default behavior.
    – Ari
    Nov 21, 2012 at 21:32
  • 6
    It might make more sense to use next-logical-line rather than next-line so that in case the line is long, the user won't comment it then uncomment it again afterwards.
    – zw324
    Mar 27, 2013 at 15:47
  • 1
    Would be nice if this was included in a package or standalone package
    – unohoo
    Jul 9, 2014 at 12:55
40

Try this function, and bind to your favorite key:

(defun toggle-comment-on-line ()
  "comment or uncomment current line"
  (interactive)
  (comment-or-uncomment-region (line-beginning-position) (line-end-position)))
3
  • 1
    comment-or-uncomment-region is misleading. It is working only for line. Mar 23, 2017 at 18:06
  • 1
    @Maven It is working on a region, and the region passed in is from the beginning of the line to the end... Mar 24, 2017 at 3:57
  • Hm!! Maybe I tested it in hurry. I will check again. If I select a region partially on a line, will it include the whole line ? Mar 24, 2017 at 6:52
14

I took Trey's answer and refined it, so that it also works when a region is active, but then works on that region:

(defun comment-or-uncomment-line-or-region ()
  "Comments or uncomments the current line or region."
  (interactive)
  (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)
4
  • Thanks for also mentioning how to bind to a key combo. Feb 5, 2016 at 17:51
  • I had to change the key combination part to this: (global-set-key (kbd "<C-kp-divide>") 'comment-or-uncomment-line-or-region) to make it work. Feb 5, 2016 at 19:20
  • When I replace "C-/" by "C-m" it doe snot work. Why? Apr 12, 2016 at 10:21
  • C-m is hardwired to RET, similar to how C-g is hardwired. I think you cannot use it for anything else.
    – Arne
    Apr 12, 2016 at 10:32
3

I'm surprised the comment-region routine hasn't been mentioned. (Though I concede it may indicate I've missed something.) I've had the following line in my .emacs file for the better part of 20 years. It works well in most major programming modes I care about.

(global-set-key "\C-c\C-c" 'comment-region)

From the docs of 'comment-region'

Documentation: Comment or uncomment each line in the region. With just C-u prefix arg, uncomment each line in region. Numeric prefix arg ARG means use ARG comment characters. If ARG is negative, delete that many comment characters instead. Comments are terminated on each line, even for syntax in which newline does not end the comment. Blank lines do not get comments.

4
  • 4
    I +1'ed your answer because comment-region is always nice... However as far as I can see it's not anywhere near as convenient as what OP is asking: he wants, no matter where he's on at the current line, to be able to toggle the comments on/off. With comment-region one still has to: go at the beginning of the line, set the mark, go at the end of the line, and then comment-region (that's quite more work than a unique shortcut toggling comments on/off). But then my Emacs-fu is not strong so I may be wrong on that... Mar 14, 2012 at 0:05
  • 1
    The OP is aware of comment-dwim which is the super-set of comment-region functionality.
    – event_jr
    Mar 14, 2012 at 0:31
  • @TacticalCoder Yeah, you're right. I've been using comment-region tied to a toggle for so long that I glossed right over the appropriate details without seeing it. Mar 14, 2012 at 19:09
  • @event_jr: No, comment-dwim is definitely not a superset of comment-region functionality. This is a common misconception.
    – Drew
    Jul 19, 2014 at 22:08
1

I think you misunderstand how keyboard-macros work. What @Trey provided is a Emacs-Lisp command. You could have accomplished this for yourself without understanding Emacs-Lisp.

First figure out the sequence of keys that does what you want and then record that sequence as a macro.

You proposed this: C-a M-; (M-; is comment-dwim). Does it do what you had in mind? If not then it's not going to magically work when you play it back as a keyboard macro.

1

This answer applies here. It defines command comment-region-lines that comments or uncomments the current line, or the region if active.

It is similar to comment-or-uncomment-region, but it lets you decide whether to uncomment or comment. It lets you nest comments, instead of automatically uncommenting the region if it is already commented out.

With a numeric prefix arg it uses that many comment-start chars (e.g., ;, ;;, ;;;,... in Lisp). With a plain C-u prefix arg it uncomments. I bind it to C-x C-;.

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