Skip to content

My Emacs keybindings

by mickey on February 28th, 2014

I figured I’d write a blog post about the keys I’ve bound but also rebound in Emacs. I think rebinding keys in Emacs — even though, in essence, the editor is built around the idea of customization — is a perilous thing to do if you are not careful: people do it without knowing why the key is bound to what it is. This is particularly true of the “core” bindings in Emacs. Rebinding keys is something you should do as a last resort: if you’re new to Emacs and your first impulse is to rebind everything — stop! Learn Emacs first and then decide.

Consider C-f. It moves forward by the character, but M-f moves forward by word, and C-M-f moves forward by an s-expression. See the similarities? The C- for character, M- for word and C-M- for s-exp is a recurring pattern in Emacs.

Having said that… there are keys I rebind and commands I explicitly bind to keys. Most of them are quality of life improvements: I want to make it easier to type things I do frequently.

New Binding Reason for Rebinding
M-o The command other-window is normally bound to C-x o but I find that way too cumbersome for what is such a frequent operation. M-o is normally bound to some rich text formatting nobody cares about.
C-<return> This I bind to a custom Helm command that calls up some of my more frequent things. This key is surprisingly unbound in most modes.
M-` This is taken from my article on fixing the mark commands. When pressed it’ll jump around in the mark ring. Very useful, but Ubuntu and Unity (in their wisdom) have decided that I am not allowed to rebind this key…. so I cannot use it any more.
C-x C-k I kill buffers all the time, and the idea of killing a buffer that is not active is just not part of my workflow at all. This key will kill the active buffer without any prompting whatsoever.
C-z I bind this to the command repeat which will repeat the last command you did. Read my article on repeating commands for more info.
C-= This I bind to CSSH’s cssh-term-remote-open. It prompts me for a remote host to SSH to using an M-x ansi-term session. It’s great.
C-x C-r By default this will run find-file-read-only, a command that finds a file but opens it as read only. Meh. That’s all I have to say about that. The few occasions I need to do this I just set it as read only. Instead I bind it to a custom command that, using IDO, gives me a list of recent files I’ve opened.
M-n / M-p I bind these to my smart scan next/previous commands. They’re part of my Smart Scan package.
S-C-<left/right/down/up> This calls shrink/enlarge-window-horizontally and enlarge/shrink-window respectively. I don’t often resize my windows any more (my high monitor resolution means I don’t have to) but it’s occasionally useful, and definitely more useful than the useless and impossibly-hard-to-type C-x ^, et al. they’re bound to by default.
F1 This runs M-x shell, my preferred one of the terminal/shell wrappers in Emacs. If you don’t know the difference, read Running shells in Emacs: an overview. It’s such a handy keybinding, too. Yes, you override the help, but it’s bound to C-h also.
F2 Runs M-x rgrep. Indispensable. I grep a lot and the key is very accessible. By default it’s bound to the 2-column commands but they’re also bound to C-x 6 ....
C-<f2> This runs my own command, multi-occur-in-this-mode. This will run M-x occur but against all buffers of the same major mode as the one point is in. Very useful. Searching buffers with occur mode will tell you all you want to know.
F6 This calls another custom command of mine, revert-this-buffer. It does exactly what the name implies: it reverts (reloads from file) the current buffer without asking any questions. It will notify you in the minibuffer area that it did it.
F10 This calls magit-status. That will open up Magit, a great Git client for Emacs. Simple, but I use it 100s of times a day.
C-x C-b This is simply bound to M-x ibuffer. Terrific feature; ibuffer’s great.

And that’s pretty much it. Not a whole lot, really. I do repurpose quite a few of the aforementioned keys as there is, to me, a lot of useless cruft I’d never use, but apart from that very few of them are remapped.

Still, there’s always room for improvement. If you have any suggestions or handy snippets — post below or e-mail me.

15 Comments
  1. Erin Keenan permalink

    I’ve use the exact same M-o and C-x C-k keybindings! It’s sort of mind-boggling how poorly thought out some of the default emacs keybindings are.

    A great trick I recently learned (maybe from this blog; if so, sorry for the repetition!) is to define your custom bindings in your own minor mode, so that way you dont have to play whackamole with other modes that want the same binding.

  2. Chris permalink

    C-x C-b This is simply bound to M-x ibuffer. Terrific feature; ibuffer’s great.

    I think “(defalias ‘list-buffers ‘ibuffer)” is good enough.

    P.S.
    I have learnt a lot from your website. Thanks for your excellent work.

  3. Oleh permalink

    Sorry, the markdown in the first reply was messed up. Let’s see if this one works:

    There’s a method to disable “M-" in Unity at https://bugs.launchpad.net/unity/+bug/874734.

    Here are a few things that I like to use:

    1. Swap "C-h" and "C-p":
    previous-line is an important command, and it's assigned to an inconvenient position
    in my opinion. So I've reassigned it to "C-h" (which did the same as ).
    And I've bound
    other-window to "C-p".

    Here's my keyfreq-show to demonstrate the priority previous-line has for me:

    618265 19.13% self-insert-command
    431476 13.35% next-line
    383339 11.86% forward-char
    358390 11.09% previous-line

    2. Bind "C-s" and "C-r" to regex versions.
    3. Bind "C-t" to
    smex.
    4. Bind "C-." to
    comment-dwim.
    5. Bind "C-," to
    lispy-kill-at-point` – my own command that kills sexp or
    string or comment at point. It also duplicates the region if it’s active.
    6. Bind “C-M-,” to do the same as above, but with marking instead of killing.
    7. Bind “C-M-.” to comment out sexp, and insert its clone below.
    It’s like poor man’s version control: when I’m hacking something,
    I use the comment for reference to the original code and as a fallback
    if I decide that I don’t want to continue.

    • mickey permalink

      Interesting key bind choices! It’s really good to see what other people do.

      I don’t think I could ever replace C-h. I use it constantly and I’ve been using for Emacs so long now I don’t know if I could (or would) untrain C-p. I also occasionally use other folks’ Emacsen and then I’d have to remember to use the default. My current layout more-or-less works OK on a default Emacs.

      C-t is just too useful for me. I transpose characters ALL the time. Ditto transpose word (M-t) and sexp (C-M-t). They’re super super useful. If you don’t use them daily I suggest you train yourself; too good to pass up. Maybe I should write an article about transpose…

      Interesting idea with your custom mark/kill sexp.. I’ll have to play around with that concept.

      I actually prefer non-regex isearch for most things; but that’s just me.

      Thanks for the ubuntu link – I’ll have a look.

      Also, I’ve never heard of keyfreq. I will give that a shot right now!

  4. Charles Comstock permalink

    I actually rebound your smartscan package to use M-s n, p for next, previous and M-s % for replace symbol. I had issues with M-n, M-p in grep, occur and a variety of other special modes. Given that M-s mode map is associated with searching, occur, and hilighting symbols, and was not using n or p it seemed a nice place to put it.

    I’ve considered the C-x C-k rebinding a couple of times, but didn’t want to override the kmacro keymap, despite rarely using it.

    • mickey permalink

      Ah yes, that’s only because the global-minor-mode overrides the local minor modes. If you global-set-key the keys instead then it won’t override your keys in Occur etc.

      (Oh, and btw, the keys M-g M-n/p will let you go to the next/prev item outside of the compilation/occur/grep buffer!

  5. Thanks for all your great posts about Emacs. I’ve been playing around quite a bit with keybindings – I really love evil-mode (and I never even used vim before! :) ), and also key-chord.

  6. Josh permalink

    I almost have your same binding for revert-buffer (+), but with it on F5. Refresh in browser turned revert in emacs. Forgive my transgressions if I have overwritten a good default binding on F5. :)

    I’m very curious to try out keyfreq too.

  7. Stefan permalink

    Nice list.

    Years ago I decided to do away with original bindings on C-v and C-b. Instead I use C-v as a map for my personal C-x-alikes, that is, they are globally available. In contrast C-b is mode-aware, just as C-c as a prefix usually is.

    With this plethora of new bindable keys, the fun begins.

    C-v +….

    C-a align
    f find-function
    C-f ffat
    C-g magit-status
    g fume-prompt-function-goto
    i imenu
    C-l locate
    C-o occur
    C-s svn-status
    C-t fdlcap-change-case-current-word
    … and lots more.

    For all window movement, use windmove. M-o is just not enough. I bind C-S+ cursor keys to move directly up, left, down, right. F8 enters windmove mode in which you can easily adjust the window’s sizes.

    S-SPC is dabbrev-expand (default is too hard on German keyboard) and C-S-SPC is helm.

    Home and end use self-written functions to go to beginning of line, beginning of window and beginning of buffer on repeated keypresses.

    C-. and C-, use self-written functions to quickly store the current location to a register and jump back again.

    S-TAB is comint-dynamic-complete giving you in-buffer filename completions most of the time.

    … just my 2ct of inspiration

    Regards,
    stefan

  8. Have you ever considered using a super key? I use the “menu key” on my (Windows-oriented) keyboard. This code is sufficient for me:

    (setq w32-pass-apps-to-system nil
    w32-apps-modifier ‘super)

  9. Howard permalink

    A little off topic, but I think in spirit…I use the following to establish a new minor mode for reading log file (I do LOTS of that), that toggles off line-wrap, and toggles on the auto tail mode.

    (define-derived-mode log-mode text-mode "log" "Major mode for log files"
    (auto-revert-tail-mode 1)
    (set-variable 'truncate-lines t))

    (add-to-list 'auto-mode-alist (cons "\\.log\\|\\.LOG" 'log-mode))

  10. Howard Melman permalink

    Once I did this,
    (global-set-key “\C-z” ctl-x-5-map)

    I put a whole bunch of frame commands on xtl-x-5-map (some std, others are obvious short extensions)

    (define-key ctl-x-5-map “-” ‘variable-pitch-mode)
    (define-key ctl-x-5-map “=” ‘toggle-frame-maximized)
    (define-key ctl-x-5-map “i” ‘iconify-frame)
    (define-key ctl-x-5-map “k” ‘kill-buffer-and-frame)
    (define-key ctl-x-5-map “l” ‘lower-frame)
    (define-key ctl-x-5-map “p” ‘select-previous-frame)
    (define-key ctl-x-5-map “n” ‘select-next-frame)

  11. Michael permalink

    Hi Mickey,

    I love your kill-this-buffer function, and I recently adapted it to be used in Dired.

    By default, Dired runs “quit-window,” which just closes the window and buries the buffer. I want the buffer to quit completely, so I remapped ‘q’ in Dired to run your kill-this-buffer function:

    (define-key dired-mode-map (kbd “q”) ‘kill-this-buffer)

    This works well in combination with Xah Lee’s tips about rebinding ^ and in Dired on this page:

    http://ergoemacs.org/emacs/emacs_dired_tips.html

  12. Jim permalink

    Hi Mickey,
    Enjoy your site.
    When you bind ‘repeat to C-z have you managed to replicate the functionality of C-x z, where after the initial invocation repeated pressing of ‘z’ repeats the action?

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS