Control mode
Emacs Lisp
Latest commit 72d6179 Jun 25, 2016 @stephendavidmarsh committed on GitHub Merge pull request #10 from mattiasb/control-mode-exceptions-doc
Control Mode Exceptions Doc
Permalink
Failed to load latest commit information.
.gitignore add ‘.gitignore’ file Sep 9, 2015
README.md README: Mention mode exceptions Jun 25, 2016
control-mode.el Exceptions to global mode Apr 19, 2016

README.md

Control Mode

License GPL 3 MELPA

Control Mode is a minor mode for Emacs that provides a “control” mode, similar in purpose to Vim's “normal” mode. Unlike the various Vim emulation modes, the key bindings in Control Mode are derived from the key bindings already setup, usually by making the control key unnecessary, e.g. C-f becomes f. This provides the power of a mode dedicated to controlling the editor without needing to learn or maintain new key bindings.

Installation

MELPA

If you haven't already, add the following lines to your .emacs.d/init.el and restart Emacs so that you can install packages from MELPA:

(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))

Then do M-x package-install RET control-mode RET.

Manual Installation

Download the control-mode.el file and put it in your .emacs.d directory. Add the following lines to your .emacs.d/init.el:

(add-to-list 'load-path "~/.emacs.d/")
(require 'control-mode)

Setup

Once you have the control-mode package installed, you can add the following line to your .emacs:

(control-mode-default-setup)

This will setup C-z to turn on Control Mode globally, and C-z and z to turn it off globally. If you prefer to use it on a buffer by buffer basis, use control-mode-localized-setup. If you need the usual binding for Ctrl-z to suspend Emacs, you can use Ctrl-x Ctrl-z instead (x Ctrl-z in Control Mode). It also binds x f to find-file (or whatever you had bound to Ctrl-x Ctrl-f) in Control Mode if it would otherwise be bound to set-fill-column.

If you want to run control-mode globally as above but want to disable it for some specific modes, add the mode to the global-control-mode-exceptions custom variable.

What It Does

Control Mode looks at every key binding you already have defined. For each binding that includes ⎈ Ctrl, it tries to rebind it without ⎈ Ctrl. It will only do this if the key binding it is replacing is unbound or bound to self-insert-command or org-self-insert-command, the Emacs commands for keys that simply enter themselves. It will also look at all bindings with ◆ Meta (⎇ Alt on most keyboards), and try to rebind those without the ◆ Meta. ⎈ Ctrl bindings take precedence over ◆ Meta bindings.

An exception is made for C-m and C-i, which are usually synonyms for ↵ Enter and ↹ Tab in Emacs. They will be ignored, allowing M-m and M-i to be bound to m and i.

C-M combinations also get rebound. C-M will get bound to ⎈ Ctrl if ⎈ Ctrl unbound or rebound, and to ◆ Meta if ◆ Meta was unbound or rebound. If you set the variable control-mode-rebind-to-shift to t Control Mode will also try to rebind to ⇧ Shift if that binding wouldn't already be taken over by a ⎈ Ctrl + ⇧ Shift or ◆ Meta + ⇧ Shift binding. This may interfere with the use of ⇧ Shift with movement commands to select a region however, and so is off by default.

Control Mode does the right thing when a key binding includes modifiers other than ⎈ Ctrl and ◆ Meta. For example, it will rebind ⎈ Ctrl + ⇧ Shift + ⌫ Backspace to ⇧ Shift + ⌫ Backspace if ⇧ Shift + ⌫ Backspace has a key binding it is allowed to replace, and it will try to rebind ⎈ Ctrl + ◆ Meta + Super + Hyper to ⎈ Ctrl + Super + Hyper + x.

Control mode will recurse into prefix keys' keymaps, for example C-x C-x becomes available as x C-x and x x.

Examples

Suppose C-f, M-f, and C-M-f are all bound to commands but f is either unbound or just types “f”. Control mode would create key bindings like so:

Original bindingAvailable in Control Mode as
C-ff
M-fM-f
C-M-fC-f, C-M-f

If C-% isn't bound, but M-% and C-M-% are:

Original bindingAvailable in Control Mode as
M-%%
C-M-% C-%, M-%, C-M-%

If M-n isn't bound, but C-n and C-M-n are:

Original bindingAvailable in Control Mode as
C-nn
C-M-n C-n, M-n, C-M-n

Regenerating Key Bindings

Control mode generates bindings separately for every combination of major mode and minor modes, and so will setup different bindings in each buffer as necessary. It is able to detect when the major mode changes and adapt to that, but there is no way for Control Mode to know if you have turned on a new minor mode. If this causes a problem, turn Control Mode off and back on again.

If you change the key bindings in any of the modes or in your global keymap, you may have to tell Control Mode to regenerate its key bindings. This can be done with the control-mode-reload-bindings command.

Tips

C-[ in Emacs acts like pushing ⎋ Esc or holding down ◆ Meta. In Control mode [ has this behavior. So for example, [ f will do forward-word. Also, all the number keys, -, and u are rebound in Control Mode to set arguments for following commands. So 3 k deletes the last three lines and u [ a jumps back four sentences.

C-q, quoted-insert, gives you a way to insert text while in Control Mode. This becomes q, so q t q e q x q t will enter “text”.

Keyboard macros in Emacs record the actual key presses used while creating them, and so a keyboard macro created in Control Mode may not work outside of Control Mode, and vice versa. You can start a keyboard macro with C-0 C-z to force it to turn Control Mode off, or C-1 C-z to force it to turn Control Mode on, to prevent problems with keyboard macros being executed in the wrong mode.

Customization

Besides the control-mode-rebind-to-shift variable mentioned above, Control Mode provides a keymap and a hook you can use for customization. You can create key bindings in control-mode-keymap and have them available in Control Mode. These override any automatically generated key bindings.

You can also use add-hook with control-mode-keymap-generation-functions to hook into the keymap generation system. Functions attached to this hook will be passed a single parameter, a keymap they can define bindings in to make them available in Control Mode. These functions will be called once for each combination of major mode and minor modes, and so let you customize Control Mode based on the other modes or key bindings that are present.

You can use Emacs' customization interface to customize Control Mode: M-x customize-group control-mode RET.

License

Copyright © 2013–2015 Stephen Marsh

Distributed under GNU GPL, version 3.