为什么我的C-c C-c键盘不能在C ++中工作?

时间:2013-07-25 23:34:03

标签: emacs emacs24

我是emacs的新手,并使用emacs 24并试图将C-c C-c绑定到一个函数来注释掉一行。我在init.el文件中有以下内容,但它似乎不适用于c ++。

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

 (global-set-key (kbd "C-c C-c") 'toggle-comment-on-line)

当我在临时页面中玩游戏时,它工作正常,当我查看C-h k C-c C-c时,它会显示正确的功能,但当我在C ++中时,相同的命令会显示文本:

C-c C-c runs the command comment-region, which is an interactive
compiled Lisp function in `newcomment.el'.

It is bound to C-c C-c, <menu-bar> <C++> <Comment Out Region>.

(comment-region BEG END &optional ARG)

Comment or uncomment each line in the region.
With just C-u prefix arg, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.

The strings used as comment starts are built from `comment-start'
and `comment-padding'; the strings used as comment ends are built
from `comment-end' and `comment-padding'.

By default, the `comment-start' markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments).  This can be
changed with `comment-style'.

我假设其他一些东西覆盖了C ++键绑定,但我不知道是什么或如何解决它?有没有人有任何想法?

3 个答案:

答案 0 :(得分:3)

是的,c ++模式有自己的键映射,它覆盖了全局映射。请改用以下内容:

 (define-key c++-mode-map (kbd "C-c C-c") 'toggle-comment-on-line)

答案 1 :(得分:1)

我已经改进了你的代码,下面还有 代码绑定密钥没有错误 (它发生的原因是你试图定义一个键 c++-mode-map在定义之前)

(defun toggle-comment-on-line ()
  "comment or uncomment current line"
  (interactive)
  (let ((beg (if (region-active-p)
                (region-beginning)
              (line-beginning-position)))
        (end (if (region-active-p)
                (region-end)
              (line-end-position))))
    (comment-or-uncomment-region beg end)
    (next-line)))

(add-hook 'c++-mode-hook
      (lambda()
            (define-key c++-mode-map (kbd "C-c C-c") 'moo-complete)))

作为旁注,我强烈建议不要绑定 C-c C-c , 因为这是一种非常流行的模式特定绑定,在每种模式下都不同, 但通常意味着确认

  • org-mode中评估一个babel代码块
  • message-mode中发送电子邮件
  • python-mode中,它将缓冲区发送到进程
  • wdired中确认您对文件名的修改

如果你绑定它,你真的会头痛,除非你是 使用Emacs 代表c++-mode

我已经使用Emacs 3年了,我有comment-dwim C - 。。到目前为止,我对它很满意。

答案 2 :(得分:0)

如果您愿意使用其他键绑定,可以使用以下代码:

;; Nothing to see here.

之后你可以C-a C-SPC C-n M-;