I usually have multiple windows open and switch between them often, so I put this in my init file:

(global-set-key (kbd "C-o") 'other-window) ; Save a keystroke when switching windows
(global-set-key (kbd "C-x o") 'open-line)

This switches the two default commands C-o and C-x o.

Having done this, C-o doesn't work when the active window is Dired or Buffer Menu, since both of these have their own commands that override my keybindings: respectively, dired-display-file and Buffer-menu-switch-other-window. It seems to me that I don't really need these, since o alone does almost exactly the same thing.

How can I make C-o always run other-window and not something else?


Local key bindings override global ones. Minor-mode key bindings override local (major mode) key bindings.

So do one of these things:

  • Define those keys in the maps of those modes so they invoke the commands you want, other-window and open-line.

  • Define your own minor mode, and define those keys in your minor mode map. And turn on your minor mode and always leave it turned on.

The resulting behaviors are different, so using one or the other depends on what you want. There is a hierarchy of keymaps that control behavior - see the Elisp manual, nodes Active Keymaps and Controlling Active Maps.

  • Is one of these options (modifying a major mode vs. adding an always-on minor mode) preferred over the other for performance or other reasons? Jun 19 '19 at 3:06
  • Edited answer to respond to your comment question.
    – Drew
    Jun 19 '19 at 15:00

I only treat your special case where you want to exchange the bindings of two key sequences.

This is one of the use-cases for key-translation-map.

The following code in your init file exchanges the bindings for C-o and C-x o (also in dired-buffers):

(define-key key-translation-map (kbd "C-o") (kbd "C-x o"))
(define-key key-translation-map (kbd "C-x o") (kbd "C-o"))

If you use this method leave the default bindings for other-window and open-line as they are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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