emacs中有哪些好的自定义键绑定?

时间:2011-04-15 21:40:26

标签: emacs keyboard keyboard-shortcuts emacs23

Emacs似乎已经在其命令之间随机分配了所有可能的键盘组合。 :P

如果我要定义新的键盘快捷键,我应该在哪里放置它们?我应该使用哪些前缀?

例如:我想为以下函数定义快捷方式:

  • 缩进缓冲区(C-c i,我得到答案后)
  • comment-or-uncomment-region(C-c C)
  • rdebug(ruby调试器)(C-c R)
  • rsense-complete(ruby autocomplete)(C-c e)

你会把这些放在哪里?为什么呢?

5 个答案:

答案 0 :(得分:19)

Emacs实际上对其绑定有一个非常明确的模式,this answer显示了一些。

至于你应该定义键的位置,如果你看一下documentation for conventions,你会看到 Cc a (其中 a 是任何小写或大写字符都是为用户保留的。

另外,如果您定义的密钥在特定模式下才真正有用,那么您应该在该密钥映射中定义它。

例如, M - / 绑定到dabbrev-expand,这是一种自动填充您正在键入的单词的便捷方式。使用rsense-complete代替它可能是有意义的,但只有才能使用ruby 。在这种情况下,您可以这样做:

(add-hook 'ruby-mode-hook
     (lambda () (define-key ruby-mode-map (kbd "M-/") 'rsense-complete)))

只有当你在ruby-mode时才会覆盖 M - / 的绑定,并在其余模式下保持不变(或可用)。

答案 1 :(得分:4)

我通常将全局键绑定放在单独的文件(我的配置的一部分)和特定于模式的配置文件中的模式特定配置中。这样一切都很精简,很容易找到。你提到的评论/取消注释的一些东西已经附加了键绑定。我已经使用函数indent-buffer-or-region(EDT的一部分)实现了indent-buffer,我将其绑定到标准的C-M- \缩进键。用户对其自定义键绑定应使用哪些键有正式规则,当然有常识。没有什么是一成不变的。

以下是Emacs Prelude的一些示例:

;; You know, like Readline.
(global-set-key (kbd "C-M-h") 'backward-kill-word)

;; Align your code in a pretty way.
(global-set-key (kbd "C-x \\") 'align-regexp)

;; Perform general cleanup.
(global-set-key (kbd "C-c n") 'cleanup-buffer)

;; Font size
(define-key global-map (kbd "C-+") 'text-scale-increase)
(define-key global-map (kbd "C--") 'text-scale-decrease)

;; Jump to a definition in the current file. (This is awesome.)
(global-set-key (kbd "C-x C-i") 'ido-imenu)

;; File finding
(global-set-key (kbd "C-x M-f") 'ido-find-file-other-window)
(global-set-key (kbd "C-x C-M-f") 'find-file-in-project)
(global-set-key (kbd "C-x f") 'recentf-ido-find-file)
(global-set-key (kbd "C-c r") 'bury-buffer)
(global-set-key (kbd "M-`") 'file-cache-minibuffer-complete)

;; Window switching. (C-x o goes to the next window)
(global-set-key (kbd "C-x O") (lambda ()
                                (interactive)
                                (other-window -1))) ;; back one

;; Indentation help
(global-set-key (kbd "C-x ^") 'join-line)
(global-set-key (kbd "C-M-\\") 'indent-region-or-buffer)

;; Start proced in a similar manner to dired
(global-set-key (kbd "C-x p") 'proced)

;; Start eshell or switch to it if it's active.
(global-set-key (kbd "C-x m") 'eshell)

;; Start a new eshell even if one is active.
(global-set-key (kbd "C-x M") (lambda () (interactive) (eshell t)))

;; Start a regular shell if you prefer that.
(global-set-key (kbd "C-x M-m") 'shell)

;; If you want to be able to M-x without meta
(global-set-key (kbd "C-x C-m") 'execute-extended-command)

;; Fetch the contents at a URL, display it raw.
(global-set-key (kbd "C-x C-h") 'view-url)

;; Help should search more than just commands
(global-set-key (kbd "C-h a") 'apropos)

;; Should be able to eval-and-replace anywhere.
(global-set-key (kbd "C-c e") 'eval-and-replace)

;; Magit rules!
(global-set-key (kbd "C-x g") 'magit-status)

;; Activate occur easily inside isearch
(define-key isearch-mode-map (kbd "C-o")
  (lambda () (interactive)
    (let ((case-fold-search isearch-case-fold-search))
      (occur (if isearch-regexp
                 isearch-string
               (regexp-quote isearch-string))))))

;; cycle through buffers
(global-set-key (kbd "<C-tab>") 'bury-buffer)

;; use hippie-expand instead of dabbrev
(global-set-key (kbd "M-/") 'hippie-expand)

;; spell check Bulgarian text
(global-set-key (kbd "C-c B")
                (lambda()(interactive)
                  (ispell-change-dictionary "bulgarian")
                  (flyspell-buffer)))

;; replace buffer-menu with ibuffer
(global-set-key (kbd "C-x C-b") 'ibuffer)

;; interactive text replacement
(global-set-key (kbd "C-c C-r") 'iedit-mode)

;; swap windows
(global-set-key (kbd "C-c s") 'swap-windows)

;; duplicate the current line or region
(global-set-key (kbd "C-c d") 'duplicate-current-line-or-region)

;; rename buffer & visited file
(global-set-key (kbd "C-c r") 'rename-file-and-buffer)

;; open an ansi-term buffer
(global-set-key (kbd "C-x t") 'visit-term-buffer)

;; toggle input method
(global-set-key (kbd "C-\\") 'toggle-bulgarian-input-method)

;; search with google
(global-set-key (kbd "C-c g") 'google)

;; toggle menu-bar visibility
(global-set-key (kbd "<f12>") (lambda () (interactive) (menu-bar-mode)))

答案 2 :(得分:3)

默认情况下,C-x h标记整个缓冲区,C-M-\运行indent-region,因此将这两个一个接一个地缩进整个缓冲区。

comment-dwim默认已绑定到M-;,可能比comment-or-uncomment-region更好。 (“DWIM”代表“do-what-what-mean”)

为了完成,我建议设置auto-complete-mode,它与rsense互操作。自动完成具有完成所有完成的标准键绑定。

至于rdebug,我可能只是将它贴在其中一个F键上,或者以C-c为前缀,因为那是指定的“自定义”前缀。也许C-c d代表“调试”。要确保密钥尚未绑定,请转到ruby缓冲区并按C-h k,然后按下您的组合键,并确保Emacs告诉您这是未定义的。

答案 3 :(得分:3)

我有一种非传统的方法,我非常推荐。 我已经重新定义了C-l('ell')键作为前缀键,我用它来 前缀我最喜欢的命令。这个键非常容易打字和 它被绑定到一个没有那么多使用的函数('recenter)。

好吧,我没有使用'重新定位,但即使你这样做,也可以分配给它 C-l C-l几乎同样容易打字,价格也很便宜 Ctrl-L-map打开了各种可能性。 (实际上我更喜欢'重绘显示'重新定位,所以我给了它 荣誉之地。)

我不记得我在哪里得到了使它起作用的神奇咒语:

(global-unset-key "\C-l")
(defvar ctl-l-map (make-keymap)
     "Keymap for local bindings and functions, prefixed by (^L)")
(define-key global-map "\C-l" 'Control-L-prefix)
(fset 'Control-L-prefix ctl-l-map)

然后,您可以定义心脏内容的关键字。 C-l是为您自己的命令添加绑定的好地方, 以及未绑定到键的函数, 或者被绑在你不记得或难以打字的钥匙上。 以下是标准函数的一些示例绑定:

(define-key ctl-l-map "\C-l"  'redraw-display)
(define-key ctl-l-map "l"  'recenter)

(define-key ctl-l-map "g"  'goto-line)
(define-key ctl-l-map "r"  'replace-string)
(define-key ctl-l-map "R"  'replace-regexp)
(define-key ctl-l-map "q"  'query-replace)
(define-key ctl-l-map "Q"  'query-replace-regexp)
(define-key ctl-l-map "o"  'overwrite-mode)
(define-key ctl-l-map "\C-w"  'kill-rectangle)
(define-key ctl-l-map "\C-y"  'yank-rectangle)
(define-key ctl-l-map "h"  'command-history)
(define-key ctl-l-map "w"  'write-region)
(define-key ctl-l-map "r" 'electric-replace-string)
(define-key ctl-l-map "\er" 'replace-string)
(define-key ctl-l-map "T"  'delete-trailing-whitespace)
(define-key ctl-l-map "C"  'describe-char)
(define-key ctl-l-map "W"  'ediff-regions-wordwise)
(define-key ctl-l-map "L"  'ediff-regions-linewise)
(define-key ctl-l-map "\C-f" 'facemenu-remove-all)
(define-key ctl-l-map "\C-c" 'calendar)
(define-key ctl-l-map "m"  'not-modified)   ;; already at M-~
(define-key ctl-l-map "\C-q" 'fill-region)
(define-key ctl-l-map "u" 'set-buffer-file-coding-system)
(define-key ctl-l-map [?\C-2] 'transient-mark-mode)
(define-key ctl-l-map "\C-@"  'transient-mark-mode)
(define-key ctl-l-map "\C-n"  'linum-mode)
(define-key ctl-l-map "\C-s" 'isearch-forward-regexp)
(define-key ctl-l-map "\C-r" 'isearch-backward-regexp)a
(define-key ctl-l-map "U" 'browse-url)
(define-key ctl-l-map "F" 'browse-url-of-file)
(define-key ctl-l-map "\C-u" 'undo-only)

答案 4 :(得分:1)

这是我的。

注意我重新映射了移动键。这是因为我使用MS NS4K,这使得这些特定的键移动非常容易。

;;;;;;;; remapping of keys ;;;;;;;;;;;;;;;

(global-set-key "\C-cg" 'goto-line)
;C-c u untabifies...yuck!
(global-set-key "\C-cu" 'untabify)
;Regex replace
(global-set-key "\C-x%" `query-replace-regexp)
;;Backward-kill-line is defined later on in this file.
(global-set-key "\M-k" `backward-kill-line)

(global-set-key "\C-x\C-j" 'eval-print-last-sexp)
;;M-up/down -> start/end of buffer. Yay!
(global-set-key (kbd "M-<up>") 'beginning-of-buffer)
(global-set-key (kbd "M-<down>") 'end-of-buffer)
(global-set-key (kbd "M-[") 'beginning-of-buffer)
(global-set-key (kbd "M-]") 'end-of-buffer)

;;remap movement keys to get rid of emacs pinky
;Jump back and forth by 5.
(global-set-key "\C-n" '(lambda () (interactive) (forward-line 5)))
(global-set-key "\C-p" '(lambda () (interactive) (forward-line -5)))
(global-set-key "\M-a" 'move-beginning-of-line)
(global-set-key "\M-e" 'move-end-of-line)
(global-set-key "\M-n" 'next-line)
(global-set-key "\M-p" 'previous-line)

;bookmark
(global-set-key (kbd "<M-f2>") 'bm-toggle)
(global-set-key (kbd "<f2>")   'bm-next)
(global-set-key (kbd "<f3>") 'bm-previous)
(setq bm-cycle-all-buffers t)

(global-set-key (kbd "<f5>") 'revert-buffer)
(global-set-key (kbd "<f12>") 'delete-trailing-whitespace)
(global-set-key (kbd "<f11>") 'comment-or-uncomment-region)

(global-set-key (kbd "M-RET") 'newline-and-indent)
(global-set-key "\C-m" 'newline-and-indent)

;;Jump "backwards"
(global-set-key "\C-xp" '(lambda () (interactive) (other-window -1)))

;;copy
(global-set-key "\M-y" 'yank)

;;insert line
(global-set-key "\M-o" '(lambda () (interactive) (open-line 1)))

;zap to char backwards
(global-set-key "\C-z" 'minimize)
;;suspend-emacs is the send-to-background command on linux

;simplify register usage
(global-set-key "\M-s" 'copy-to-register)
(global-set-key "\M-i" 'insert-register)

;;autocomplete hacks
(global-set-key [?\M-/] 'hippie-expand)
(global-set-key [?\C-/] 'dabbrev-expand)


(global-set-key [?\C-.] 'find-tag)
;(global-set-key "\C-.\C-s" 'find-tag)
(global-unset-key [?\M-.])

;;this lets us have long lines go off the side of the screen instead of hosing up the ascii art
(global-set-key "\C-t" 'toggle-truncate-lines)

;comment/uncomment region
(global-set-key "\C-c\C-c" 'comment-or-uncomment-region)

;;Buffer-move
(global-set-key (kbd "<C-S-left>")   'buf-move-left)
(global-set-key (kbd "<C-S-right>")  'buf-move-right)