问题
This question already has an answer here:
- Eclipse-like Line Commenting in Emacs 7 answers
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.
回答1:
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)))
回答2:
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.
回答3:
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:
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.
回答5:
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.
回答6:
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-;.
来源:https://stackoverflow.com/questions/9688748/emacs-comment-uncomment-current-line