在Emacs中重新定义主模式前缀键

5
我一直在尝试自学emacs,因为我使用的是dvorak键盘布局,所以我愚蠢地将C-c重新绑定为移动键,并且习惯了这样做。现在我真正开始使用它进行编程,我加载了一个Python文件并注意到C-c是所有python-mode中特殊命令的前缀。
我能否在init.el文件中重新绑定前缀键并一次性更改所有Python命令?如果不能,我应该逐个重新绑定所有Python命令吗?
2个回答

3

如果你想让Ctrl+C作为leftF12作为C-c

(define-key key-translation-map [f12] "\C-c")
(define-key key-translation-map "\C-c" [left])

请注意,这也会影响多键绑定,例如,您现在需要输入Ctrl+X F12来退出Emacs。这个硬币的另一面是,C-c C-c被输入为F12 F12

2
如果您查看python.el的源代码,您会发现这些命令是逐个使用完整规范添加的,例如:(define-key map "\C-c\C-r" 'python-send-region)
因此,您需要自己重新做所有这些工作。话虽如此,这很简单。根据您的评论,您想将前缀键更改为C-',获取正确转义的技巧是不使用转义,而是使用宏kbd文档链接)。
有了这个,您可以更新mode-map定义如下:
(defvar python-mode-map
  (let ((map (make-sparse-keymap)))
    ;; Mostly taken from python-mode.el.
    (define-key map ":" 'python-electric-colon)
    (define-key map "\177" 'python-backspace)
    (define-key map (kbd "C-' <") 'python-shift-left)
    (define-key map (kbd "C-' >") 'python-shift-right)
    (define-key map (kbd "C-' C-k") 'python-mark-block)
    (define-key map (kbd "C-' C-d") 'python-pdbtrack-toggle-stack-tracking)
    (define-key map (kbd "C-' C-n") 'python-next-statement)
    (define-key map (kbd "C-' C-p") 'python-previous-statement)
    (define-key map (kbd "C-' C-u") 'python-beginning-of-block)
    (define-key map (kbd "C-' C-f") 'python-describe-symbol)
    (define-key map (kbd "C-' C-w") 'python-check)
    (define-key map (kbd "C-' C-v") 'python-check) ; a la sgml-mode
    (define-key map (kbd "C-' C-s") 'python-send-string)
    (define-key map (kbd "C-\\ M-x") 'python-send-defun)
    (define-key map (kbd "C-' C-r") 'python-send-region)
    (define-key map (kbd "C-' M-r") 'python-send-region-and-go)
    (define-key map (kbd "C-' C-c") 'python-send-buffer)
    (define-key map (kbd "C-' C-z") 'python-switch-to-python)
    (define-key map (kbd "C-' C-m") 'python-load-file)
    (define-key map (kbd "C-' C-l") 'python-load-file) ; a la cmuscheme
    (substitute-key-definition 'complete-symbol 'symbol-complete
                               map global-map)
    (define-key map (kbd "C-' C-i") 'python-find-imports)
    (define-key map (kbd "C-' C-t") 'python-expand-template)
    (easy-menu-define python-menu map "Python Mode menu"
      `("Python"
        :help "Python-specific Features"
        ["Shift region left" python-shift-left :active mark-active
         :help "Shift by a single indentation step"]
        ["Shift region right" python-shift-right :active mark-active
         :help "Shift by a single indentation step"]
        "-"
        ["Mark block" python-mark-block
         :help "Mark innermost block around point"]
        ["Mark def/class" mark-defun
         :help "Mark innermost definition around point"]
        "-"
        ["Start of block" python-beginning-of-block
         :help "Go to start of innermost definition around point"]
        ["End of block" python-end-of-block
         :help "Go to end of innermost definition around point"]
        ["Start of def/class" beginning-of-defun
         :help "Go to start of innermost definition around point"]
        ["End of def/class" end-of-defun
         :help "Go to end of innermost definition around point"]
        "-"
        ("Templates..."
         :help "Expand templates for compound statements"
         :filter (lambda (&rest junk)
                   (abbrev-table-menu python-mode-abbrev-table)))
        "-"
        ["Start interpreter" python-shell
         :help "Run `inferior' Python in separate buffer"]
        ["Import/reload file" python-load-file
         :help "Load into inferior Python session"]
        ["Eval buffer" python-send-buffer
         :help "Evaluate buffer en bloc in inferior Python session"]
        ["Eval region" python-send-region :active mark-active
         :help "Evaluate region en bloc in inferior Python session"]
        ["Eval def/class" python-send-defun
         :help "Evaluate current definition in inferior Python session"]
        ["Switch to interpreter" python-switch-to-python
         :help "Switch to inferior Python buffer"]
        ["Set default process" python-set-proc
         :help "Make buffer's inferior process the default"
         :active (buffer-live-p python-buffer)]
        ["Check file" python-check :help "Run pychecker"]
        ["Debugger" pdb :help "Run pdb under GUD"]
        "-"
        ["Help on symbol" python-describe-symbol
         :help "Use pydoc on symbol at point"]
        ["Complete symbol" symbol-complete
         :help "Complete (qualified) symbol before point"]
        ["Find function" python-find-function
         :help "Try to find source definition of function at point"]
        ["Update imports" python-find-imports
         :help "Update list of top-level imports for completion"]))
    map))

如果我想使用 C-',该如何转义单引号?(我得到了错误,所以我认为这是问题所在) - colinmarc
@colinmarc 更新了答案,使用了该前缀。 - Trey Jackson
啊,我的移动键又回来了,但是不知道为什么按下 C-' 键只会输入一个撇号!=( - colinmarc
是的,它仍然做着同样的事情。 - colinmarc
不行,问题依旧。我正在使用23.3.1版本。感谢您的帮助,我将开一个新的问题。 - colinmarc
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接