2

I'm a newbie in Emacs configuration, I've just discovered the possibility to define my own key bindings to fit my needs. It is really cool, but Emacs has so many key bindings that some default key bindings often overlap mine.

For example, I would like to have proper keys window splitting and navigation, and I like do this with arrows.

This is what I may like :

;; Wind-split                                                                                                                                                                                                                                  
(global-set-key (kbd "C-x <C-down>") 'split-window-below)
(global-set-key (kbd "C-x <C-left>") 'split-window-right)
(global-set-key (kbd "C-x <C-up>") 'find-file-other-window)
(global-set-key (kbd "C-x <C-right>") 'switch-to-buffer-other-window)
;; Wind-move                                                                                                                                                                                                                                   
(global-set-key (kbd "C-c <left>") 'windmove-left)
(global-set-key (kbd "C-c <down>") 'windmove-down)
(global-set-key (kbd "C-c <up>") 'windmove-up)
(global-set-key (kbd "C-c <right>") 'windmove-right)
;; Wind-resize                                                                                                                                                                                                                                 
(global-set-key (kbd "C-c <C-up>") 'enlarge-window)
(global-set-key (kbd "C-c <C-down>") 'shrink-window)
(global-set-key (kbd "C-c <C-right>") 'enlarge-window-horizontally)
(global-set-key (kbd "C-c <C-left>") 'shrink-window-horizontally)
(global-set-key (kbd "C-c =") 'balance-windows)
;; Wind-arranging                                                                                                                                                                                                                              
(global-set-key (kbd "C-c C-d") 'delete-window)
(global-set-key (kbd "C-c C-c C-d") 'kill-buffer-and-window)

You'll probably notice that I have used C-c a lot and it's normal that it's overlapped since C-c is traditionally used in modes, but C-x seems less accessible and I other characters are often used for other commands.

So what easy key bindings to use for these basic operations ?

6
  • 3
    Before you go crazy with your own keybindings, do give out the Emacs default keybindings a try. In general, they're sensibly designed to begin with. I would highly recommend giving them a try for a few months before you redefine them, maybe you'll like the defaults. :)
    – PythonNut
    Nov 29, 2015 at 22:51
  • 2
    I don't like the default keybindings for arranging the windows... These are all I want to remap, for the rest I'm ok with default. I should mention that I have an azerty keyboard so default keybindings might be a little less convenient I don't know. Nov 29, 2015 at 23:13
  • I am voting to close because the question is either unclear or seeks an opinionated answer, or both. If the keybindings in your question work as-is, then the question/answer is finished in my opinion. Keybindings are a matter of personal preference -- i.e., thus opinion.
    – lawlist
    Nov 29, 2015 at 23:20
  • I'm reading this question as "How do I define a bunch of personal keybindings without getting interference from other modes", which doesn't require any opinions to answer.
    – erikstokes
    Nov 30, 2015 at 0:03
  • Thanks ericstokes you've made my point. Sorry my expression is not perfect because I am french. Nov 30, 2015 at 6:27

3 Answers 3

6

One way to deal with this is to make your own prefix key and assign your personal bindings to that. You can use any key, but most of the C-<key> bindings are taken, so you have to search for an empty one or find one you don't care about to clobber. I like using C-' as a prefix. If it's bound to anything by default, it isn't anything I've ever missed. It's also very easy to press, especially if you make your caps locks key a second ctl (then the finger movements on each hand are symmetric).

To make a prefix you first need to make a keymap to hold your bindings with define-prefix-command, then assign a key to it and finally you can start assigning functions to keys. I'm using define-key below, but global-set-key will work fine as well.

(define-prefix-command 'my-prefix-map)
(define-key global-map (kbd "C-'") 'my-prefix-map)

(define-key my-prefix-map (kbd "<C-down>") 'split-window-below)

As some other comments have mentioned, there can be issues with wholesale changing the default key bindings. For example, you'll have problems using Emacs if your configuration isn't loaded (because, for example, you're debugging your .emacs). And it can become difficult to explain how to Emacs to other people, who aren't using your configuration.

But, there are a lot of keybindings and they aren't all (or even mostly) obvious. A package like which-key can help you learn them. You'll naturally learn the ones you actually use over time and then can start changing the ones you don't care for or adding keys for the functions you use that aren't bound by default

1
  • I don't know why but C-[letter] doesn't work for me... Nov 30, 2015 at 6:34
10
  1. All C-c<letter> key sequences (for any lower or upper-case letter: [a-zA-Z]) and the function keys <f5> through <f9> are reserved for personal use, so you can always safely use those. (It's considered a bug if any third-party library uses these key sequences.)

    See C-hig (elisp) Key Binding Conventions for details.

    Consider using the easiest-to-type instances as prefix bindings, as everything you put under such a prefix will still be pretty easy to type.

  2. https://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs provides a working solution to the general problem of giving your custom bindings priority over (most) other keymaps.

  3. Be wary of just merrily clobbering all the defaults. You can still do it if you want to (some people remap almost everything, to get an entirely customised interface), but you might regret overriding what you once thought were useless bindings, only to one day realise that you've clobbered a really standard binding used by lots of modes which you now want to use :)

  4. While it might be information overload, I highly recommend the excellent write-up at https://www.masteringemacs.org/article/mastering-key-bindings-emacs so that you have an idea of the different layers of keymaps at work in Emacs, and how they are prioritised.

  5. If your keyboard has convenient extra keys ("Windows" key, "Menu" key, etc) and you can tell your OS to treat them as modifiers which Emacs understands ('super', 'hyper', etc) then you gain a vast array of key sequences which have no default bindings at all.

  6. I also find C-z a convenient prefix. It's not a prefix by default, but the following code turns it into one. You still have access to its default binding (just by typing it twice), but you can then also add anything else you want to that prefix.

    (global-unset-key (kbd "C-z")) ; suspend-frame
    (global-set-key (kbd "C-z C-z") 'suspend-frame)
    
4
  • I don't know why but C-[letter] doesn't work for me... For example, for windmove-left I have ever tried C-z <left> or C-q <left> but emacs is not happy and tell me error "Key sequence C-z <down> starts with non-prefix key C-z". This is why I have asked this question, it's the thirs thing I have tried and it has not worked, so I endend up using C-c instead. Nov 30, 2015 at 6:39
  • Thank you, with erictrokes you both give me the right answer, I didn't wan't to use C-c or C-x at first but didn't know that I have to make the prefix. Thank you for your links there really helpful. Also using the super key in emacs is not my choice beacause I use it for i3wm. Thank you again. Nov 30, 2015 at 6:51
  • 1
    suspend-frame is also bound to C-x C-z by default, so using C-z C-z for it is redundant, IMO.
    – npostavs
    Nov 30, 2015 at 23:20
  • npostavs: Ah, I didn't realise that binding existed. Thanks. I'll have to consider repurposing C-z C-z...
    – phils
    Nov 30, 2015 at 23:26
0

If you don't want to have to define everything yourself, and would rather enjoy an Emacs experience (including packages and key bindings) that has been curated by a knowledgeable Emacs user, consider using something like Emacs Prelude.

If you'd like a good way to be able to check which key sequences have already been used, I recommend the helm-descbinds package. It presents the information in a nice way, and you can search for particular key sequences using regex.

1
  • Thank you but I'm happy with my emacs ;) Nov 29, 2015 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.