2012-03-13 142 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-;