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.
Emacs also allows you to pass the number of lines you need to comment or uncomment. To do this: Start by pressing the CTRL + U key, followed by the number of lines to comment out. As you can see from the screenshot above, Emacs comments out 10 lines from the current cursor position.
You can mark at the beginning of the region that you want to comment out, move to the end of the region, then do C-c C-c or M-x comment-region.
To comment out multiple code lines right-click and select Source > Add Block Comment. ( CTRL+SHIFT+/ ) To uncomment multiple code lines right-click and select Source > Remove Block Comment.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With