Emacs comment/uncomment current line [duplicate]

我的未来我决定 提交于 2019-11-28 16:12:10

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)))
Gerstmann

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.

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)

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.

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.

Drew

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-;.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!