What are good custom keybindings in emacs?

后端 未结 5 545
醉梦人生
醉梦人生 2021-02-05 15:35

Emacs seems to have all the possible keyboard combinations already randomly distributed among it\'s commands. :p

If I am to define new keyboard shortcuts, where should I

5条回答
  •  孤街浪徒
    2021-02-05 16:22

    I generally put global keybindings in a separate file(part of my configuration) and mode specific configuration in the mode-specific configuration files. That way everything is lean, tight and easy to find. Some of the things you've mentioned like comment/uncomment already have keybindings attached. I've implemented indent-buffer with a functions indent-buffer-or-region(part of EDT) that I've bound to the standard C-M-\ indentation key. There are formal rules for what keys should be used by users for their custom keybindings and there is of course common sense. Nothing is written in stone.

    Here are some examples from 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 "") '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 "") (lambda () (interactive) (menu-bar-mode)))
    

提交回复
热议问题