你最喜欢的Emacs全局按键绑定是什么?

25

我的是:

(global-set-key [f6] 'compile-buffer)
(global-set-key [f7] 'kmacro-start-macro-or-insert-counter)
(global-set-key [f8] 'kmacro-end-and-call-macro)
(global-set-key [f9] 'call-last-kbd-macro)
(global-set-key [f10] 'name-and-insert-last-kbd-macro)
(global-set-key [f12] 'menu-bar-open)  ; originally bound to F10
(global-set-key "\C-cR" 'rename-current-file-or-buffer)
(global-set-key "\C-cD" 'Delete-current-file-or-buffer)

name-and-insert-last-keyboard-macro来自另一个Stack Overflow问题

13个回答

25

我恰好有很多这样的东西:

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

;; Use regex searches by default.
(global-set-key (kbd "C-s") 'isearch-forward-regexp)
(global-set-key (kbd "\C-r") 'isearch-backward-regexp)
(global-set-key (kbd "C-M-s") 'isearch-forward)
(global-set-key (kbd "C-M-r") 'isearch-backward)

;; 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
(global-set-key (kbd "C-x C-o") (lambda ()
                                  (interactive)
                                  (other-window 2))) ;; forward two

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

;; This is a little hacky since VC doesn't support git add internally
(eval-after-load 'vc
  (define-key vc-prefix-map "i" '(lambda () (interactive)
                                   (if (not (eq 'Git (vc-backend buffer-file-name)))
                                       (vc-register)
                                     (shell-command (format "git add %s" buffer-file-name))
                                     (message "Staged changes.")))))

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

;; Org
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)

;; program shortcuts - s stands for windows key(super)
(global-set-key (kbd "s-b") 'browse-url)          ;; Browse (W3M)
(global-set-key (kbd "s-f") 'browse-url-firefox)  ;; Firefox...
(global-set-key (kbd "s-l") 'linum-mode)          ;; show line numbers in buffer
(global-set-key (kbd "s-r") 're-builder)          ;; build regular expressions

;; Super + uppercase letter signifies a buffer/file
(global-set-key (kbd "s-S")                       ;; scratch
                (lambda()(interactive)(switch-to-buffer "*scratch*")))
(global-set-key (kbd "s-E")                       ;; .emacs
                (lambda()(interactive)(find-file "~/emacs/dot-emacs.el")))

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

;; macros
(global-set-key [f10]  'start-kbd-macro)
(global-set-key [f11]  'end-kbd-macro)
(global-set-key [f12]  'call-last-kbd-macro)

(provide 'bindings-config)

我实际上有一个完整的Emacs Lisp文件专门用于全局按键绑定 :-)


2
你应该看看我的其他配置;-) - Bozhidar Batsov
如果包含自定义函数(lambda ...)的示例,将会加分。 - Matt
你能展示一下 ;; 跳转到当前文件中的定义。 (这太棒了。) (global-set-key (kbd "C-x C-i") 'ido-imenu) ido-imenu 吗? - eugene
重复使用 C-c r - rofrol

7

我一些比较不寻常的绑定:

(global-set-key [pause] 'emms-pause)

这是很久以来使用 暂停 键的第一个好方法!

(global-set-key [(super \\)] 'find-file-at-point)

非常实用。

(global-set-key [(super s)] 'shell)
(global-set-key [(meta  p)] 'shell)

有了第二个绑定,我可以快速地键入M-p M-p RET返回到shell缓冲区并重复上次输入的命令。
接下来是解除绑定:
(global-unset-key "\C-x\C-n")

我从未真正使用set-goal-column,并且总是被它绊倒。

(when window-system (global-unset-key "\C-z"))

当我不小心按下C-z并图标化我的框架时,我很讨厌这种情况。

现在我们有点元数据:

(defmacro global-set-key* (keys &rest body)
  `(global-set-key ,keys (lambda () (interactive) ,@body)))

这是一个方便快捷的设备,可以帮助我输入以下内容:

(global-set-key* [(shift control n)] (next-line) (scroll-up 1))
(global-set-key* [(shift control p)] (previous-line) (scroll-down 1))

5
这要归功于 Steve Yegge 在 http://sites.google.com/site/steveyegge2/effective-emacs 中的贡献。
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)
(global-set-key "\C-c\C-k" 'kill-region)

有一个Ctrl-w的含义与我习惯的含义相同,真是太棒了。

(此外,还可以查看该文章获取更多信息)


4
(bind "C-t" (lookup-key global-map (kbd "C-x")))

我从不将字符转置所以当它作为前缀键使用时,我将其重新绑定为C-x。我简直受不了在Dvorak键盘上伸手去按“x”键。

今天开始我也在使用这个宏:

(defmacro bind (key fn)
  `(global-set-key (kbd ,key) ,(if (listp fn) fn `',fn)))

1
有趣的是,我觉得在Dvorak键盘上按C-x更容易。 :-) - Ken
+1 为示例展示如何创建额外的前缀键。 - Matt

4

我最喜欢的技巧之一是一个同事推荐给我的,一开始我以为我会讨厌它:

箭头键(上/下/左/右)被重新映射为滚动当前窗口。要移动光标,您仍然可以使用C-n/p/f/b(或isearch、tags或其他方式)。


3

Breadcrumb:

面包屑:
(require 'breadcrumb)
(global-set-key [(control f2)]          'bc-set)
(global-set-key [(f2)]                  'bc-previous)
(global-set-key [(shift f2)]            'bc-next)
(global-set-key [(meta f2)]             'bc-list)

2

我在第一次回答中没有列出这些内容,但回想起来,我总是发现它们很有用,而且我经常看到有人问问题,而这些问题可以很容易地通过使用 apropos 函数解决,所以我认为任何使它们更可见的东西都是好事!(我还注意到自从我第一次添加这些内容以来, apropos-library 也出现了,因此对我来说写这个答案也很有用:)

;; Make apropos searches also find unbound symbols, and
;; set new key-bindings for various other apropos commands.
(setq apropos-do-all t)
(global-set-key (kbd "C-h a") 'apropos-command)
(define-prefix-command 'Apropos-Prefix nil "Apropos (a,d,f,l,v,C-v)")
(global-set-key (kbd "C-h C-a") 'Apropos-Prefix)
(define-key Apropos-Prefix (kbd "a")   'apropos)
(define-key Apropos-Prefix (kbd "C-a") 'apropos)
(define-key Apropos-Prefix (kbd "d")   'apropos-documentation)
(define-key Apropos-Prefix (kbd "f")   'apropos-command)
(define-key Apropos-Prefix (kbd "l")   'apropos-library)
(define-key Apropos-Prefix (kbd "v")   'apropos-variable)
(define-key Apropos-Prefix (kbd "C-v") 'apropos-value)

使用这些绑定,每当我想要搜索“某些东西”时,我会输入C-h C-a,然后根据我需要的特定类型的搜索输入相应的字符(如果我记不清可能性,会有提示帮助我)。如果我不知道我在寻找什么,那么在提示符处再次按下C-a(或纯粹的a)将运行一个全面的apropos搜索。
如果我记不住提示字符的含义,那么在提示符处再次输入C-h(即C-h C-a C-h)将列出绑定。

2

(global-set-key [(control w)] 'kill-this-buffer)

我已经习惯使用 Ctrl-W 来关闭一个缓冲区 -- 自定义这个绑定让 Emacs 变得更加容易。

是的,我知道这暴露了我是一个 Windows 程序员的身份。我们都会犯错...


7
逆向问题比较严重——我经常会误关闭 Windows 应用,当我只是想剪切或删除一些文本时。 - phils

1
;; Generally useful
(global-set-key [(meta ?/)] 'hippie-expand)
(global-set-key [(super ?i)] 'imenu)

;; Emacs Lisp navigation
(global-set-key (kbd "C-c f") 'find-function)
(global-set-key [(super ?l)] 'find-library)

;; Compiling things, navigating to errors
(global-set-key [print] 'recompile)
(global-set-key [(shift print)] 'compile)
(global-set-key (kbd "M-p") 'previous-error)
(global-set-key (kbd "M-n") 'next-error)
(global-set-key (kbd "s-p") 'flymake-goto-prev-error)
(global-set-key (kbd "s-n") 'flymake-goto-next-error)

;; Open URLs in Firefox.  Still not sure which binding I like most...
(global-set-key (kbd "s-<kp-5>") 'browse-url-firefox)
(global-set-key (kbd "s-<kp-begin>") 'browse-url-firefox)
(global-set-key (kbd "s-t") 'browse-url-firefox)

;; EMMS (music player)
(global-set-key [Scroll_Lock] 'emms-pause)
(global-set-key (kbd "<S-Scroll_Lock>") 'emms-next)
(global-set-key (kbd "<C-Scroll_Lock>") 'emms-show)

;; Navigation between and within buffers
(global-set-key (kbd "C-<backspace>") 'bury-buffer)

(defun scroll-down-one-line ()
  "Scroll down one line."
  (interactive)
  (scroll-down 1))

(defun scroll-up-one-line ()
  "Scroll up one line."
  (interactive)
  (scroll-up 1))

(global-set-key [(super up)] 'scroll-down-one-line)
(global-set-key [(super down)] 'scroll-up-one-line)

(global-set-key [(super right)] 'next-buffer)
(global-set-key [(super left)] 'previous-buffer)

(defun other-window-backwards ()
  (interactive)
  (other-window -1))

(global-set-key [(control super down)] 'other-window)
(global-set-key [(control super up)] 'other-window-backwards)

1
(global-set-key "\M-n"  'next-buffer)
(global-set-key "\M-p"  'previous-buffer)
(global-set-key (kbd "C-c w") (quote copy-word))
(global-set-key (kbd "C-c l") (quote copy-line))
(global-set-key (kbd "C-c p") (quote copy-paragraph))
(global-set-key (kbd "C-c s") (quote thing-copy-string-to-mark))
(global-set-key (kbd "C-c a") (quote thing-copy-parenthesis-to-mark))

(global-set-key "\C-o"  'other-window)
(global-set-key "\M-o"  'other-window)

(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
(define-key my-keys-minor-mode-map (kbd "C-,") 'beginning-of-buffer)
(define-key my-keys-minor-mode-map (kbd "C-.") 'end-of-buffer)
(define-minor-mode my-keys-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  t " my-keys" 'my-keys-minor-mode-map)
(my-keys-minor-mode 1)

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