Fast Toggling Emacs Modes

Over at Endless Parentheses, which I've mentioned before, Artur Malabarba has a really good idea. He shows how to define a custom key map that he uses to toggle modes that he uses frequently. He binds the commands to the prefix【Ctrl+x t】, an unused key sequence in stock Emacs. Then he has a third key that mnemonically suggest the mode he wants to toggle. Thus,【Ctrl+x t c】toggles column-number-mode and【Ctrl+x t r】toggles read-only-mode and so on.

Oddly, none of the shortcuts that he proposes are ones I care about but I do have a list of my own

l linum-mode
o org-mode
p paredit-mode
t text-mode
v visual-line-mode

As Malabarba says, these aren't keys I'll use everyday but they are mnemonic enough that I'll be able to remember then.

Doubtless you have your own list of modes you'd like to be able to toggle easily and Malabarba's method makes it easy to bind them to an easily remembered key.

A very useful post, I think. I'm going to implement the above list in my own init.el as soon as I finish writing this.

This entry was posted in General and tagged . Bookmark the permalink.
  • I am curious about the use case for toggling between modes.

    My use case is satisfied entirely by org-mode because I use `org-edit-special'.

    • i switch modes a lot, manually. years ago, i had key to switch between php and html mode, when working in mixed html, so i can get syntax coloring of the part am working on.

      These days i had key to switch several alt modes for the same lang... e.g. javascript, because some does something better, syntax table, and also writing my own so i had to check behaviors and features and see how others do it. Typically 3 key sequences of single keys.

    • There are some variables/minor-modes that are too obtrusive to keep turned on, but are useful on occasion. linum-mode is a good example. Debug-on-error was my example.

      • But I too am curious as to why Snader toggles org-mode or text-mode. :-)

        • eric

          I sometimes write long emails in gnus that can benefit from some level of formatting. For these, I switch the message buffer to org-mode, compose using headings, lists and maybe even text formatting. When done, I switch back to message mode, htmlize the document and send it.

          This is a very specific use case but having the easy to remember toggling commands is a winner because I don't do this often enough to warrant mode specific bindings.

    • jcs

      As Artur says, some are obvious. I sometimes want to see
      line numbers but usually not. It’s convenient to be able to turn it on when needed. When I was learning paredit I often got stuck and needed to turn it off temporarily. That doesn’t happen often anymore but Artur’s key map idea makes it easy to have it available. As for Org-mode and Text-mode I sometimes want to see and Org buffer as plain text and those two toggles Make it easy to switch back and forth.

  • bakhti

    This is a grate idea. I've also added Ctrl+x t to guide-key/guide-key-sequence.

    • jcs

      Adding Ctrl+x t to the guide-key-sequence is another good idea. The notion is that the third key is mnemonic enough to be remembered but if, like me, your memory is prone to taking extended vacations, this is a good method for documenting your choices.

      • bakhti

        This is the thing - I don't trust my memory :). I still can't belive hi cool this idea is. I went further and did the same thing for multiple-cursors (I can never remember what keys I should use for it), so now I have something like this:

        Ctrl+x m l - mc/edit-lines
        Ctrl+x m e - mc/edit-ends-of-lines
        Ctrl+x m b - mc/edit-beginnings-of-lines
        Ctrl+x m n - mc/mark-next-like-this
        Ctrl+x m p - mc/mark-previous-like-this
        Ctrl+x m a - mc/mark-all-like-this
        And of cause Ctrl+x m is also added to guide-key-sequence. I'm still thinking if using this scheme for mark-next/previous-like-this is a good idea though.

        • bakhti

          And I can describe all multi-cursor functions like this and see the option I have in guide-key menu.

        • Phil

          Don't get into the habit of binding C-x <letter>. If you instead use C-c <letter> those sequences (for all lower- and upper-case letters A-Z) are reserved for end-users, and you should never encounter conflicts with any other keymaps.