2012-03-13 133 views
36

我知道这已经是一个Emacs的问题了,而且它已经关闭了,但我觉得它非常相关和重要。Emacs评论/取消评论当前行

基本上,我想评论/取消注释当前行。我以为这是一个宏,相当容易,但我发现它确实不是。

如果当前行被注释,则取消注释。如果未注释,请对其进行评论。而且我还要评论整个系列,而不仅仅是从光标位置。

我想这样的宏:

C-一个

'comment-dwim 

但是,这只是工作作出评论,一条线,而不是取消注释它,如果它已经评论。

我不确定它是多么容易,但如果有某种方法,我真的很喜欢它。

另外,我非常喜欢这个想法的原因是,当我使用Geany时,我只使用了C-e,它非常完美。

+1

请参阅(http://www.emacswiki.org/emacs/CommentingCode)底部的“另请参阅”。 il-debug看起来非常方便。 – ccoakley 2012-03-13 17:57:54

+3

Emacs 25具有绑定到'C-x C-;'的['comment-line'](https://www.gnu.org/software/emacs/manual/html_node/emacs/Comment-Commands.html)。 – 2016-10-16 17:41:40

回答

38

试试这个功能,并绑定到您的收藏夹键:

(defun toggle-comment-on-line() 
    "comment or uncomment current line" 
    (interactive) 
    (comment-or-uncomment-region (line-beginning-position) (line-end-position))) 
+0

谢谢你,这工作非常完美! – 2012-03-13 20:46:08

+0

完美!确切需要什么。 – 2015-01-27 18:01:52

+0

'comment-or-uncomment-region'是误导性的。它只为线路工作。 – mythicalcoder 2017-03-23 18:06:49

1

我想你误会键盘宏是如何工作的。 @Trey提供的是Emacs-Lisp命令。你可以在不理解Emacs-Lisp的情况下为自己做到这一点。

首先找出你想要的按键序列,然后将该序列记录为一个宏。

您提出这样的问题:C-a M-;(M-;是comment-dwim)。它是否执行了你的想法?如果不是,那么当您将其作为键盘宏播放时,它不会神奇地工作。

2

我很惊讶comment-region例程尚未提及。 (尽管我承认这可能表明我错过了一些东西。)在我的.emacs文件中,我已经在20年的较好部分中使用了以下行。它适用于我关心的大多数主要编程模式。

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

从 '注释的区域'

文档的文档:评论或在该区域取消注释的每一行。仅用 C-u前缀arg,取消注释区域中的每一行。数字前缀arg ARG 表示使用ARG注释字符。如果ARG是否定的,则删除多个 评论字符。每条线都会终止注释,即使 中的换行符也不会结束注释。空白行 没有得到评论。

+3

我为你的答案+1了,因为*评论区*总是很好......但是据我所知,它不像OP询问的那样便利:他想要的是,无论他在目前的哪个位置行,以便能够打开/关闭评论。对于*注释区域*,还需要:在行首开始,设置标记,在行尾,然后注释区域(比独特的快捷方式切换注释的开/关)。但后来我的Emacs-fu并不强,所以我可能会错...... – TacticalCoder 2012-03-14 00:05:52

+1

OP知道'comment-dwim',它是'comment-region'功能的超集。 – 2012-03-14 00:31:35

+0

@TacticalCoder是的,你说得对。我一直在使用与切换有关的注释区域很长时间,以至于我忽略了适当的细节而忽略了它。 – 2012-03-14 19:09:28

85

Trey的功能完美,但它不是很灵活。

试试这个:

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

它评论/取消注释当前行或区域,如果一个处于活动状态。


如果你愿意,你可以修改功能(UN)后跳转到下一行注释当前行是这样的:

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

注意,只有已经改变的事情是添加next-line命令在函数的结尾。

+2

完美的作品!谢谢! – justingordon 2012-03-24 21:02:02

+4

如果在注释一行后游标移动到下一行,这样反而会更好,所以重复按键绑定会注释连续的行。这样做的任何提示? – justingordon 2012-03-25 00:47:27

+3

@justingordon我编辑了原来的替代版本。请享用! – Gerstmann 2012-03-26 05:48:53

9

我把Trey的答案和完善它,所以它也可以当一个区域是活跃的,但随后工作是在这个区域:

(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) 
+0

感谢您也提到如何绑定到组合键。 – Zelphir 2016-02-05 17:51:10

+0

我不得不将键组合部分更改为:'(全局设置键(kbd“”)'注释或取消注释行或区域')以使其工作。 – Zelphir 2016-02-05 19:20:15

+0

当我用“C-m”代替“C- /”时,它就是鼻涕工作。为什么? – 2016-04-12 10:21:21

1

This answer适用于这里。它定义命令comment-region-lines注释或取消注释当前行或区域(如果活动)。

它与comment-or-uncomment-region类似,但它让你决定是否取消注释或评论。它可以让你嵌套注释,而不是自动取消注释该区域,如果它已被注释掉。

随着数字前缀ARG它使用许多注释开始字符(例如,;;;,​​,...用Lisp)。用一个普通的C-u前缀arg取消注释。我将它绑定到C-x C-;