Assume you want to add a new prefix key. On a german keyboard, there is no way to enter å and Å, for example. In Emacs, you can use `C-x 8 / a’ and `C-x 8 / A’ respectively. But, if you are used to “deadkeys”, then you might expect `° a’ and `° A’ to do the right thing. Therefore, we want to use `°’ as a prefix key, and then define `° a’ to call the same command that `C-x 8 / a’ does.
The first thing to note is that we need a new command for the prefix key. To do that, we call
Next, we call
(global-set-key (kbd "°") 'ring-map)
When you test this, however, it may not work: Try `C-h k °’ – if it returns “° runs the command self-insert-command”, then it did not work. What went wrong? The problem with the character `°’ is that it is not an ASCII character; if your locale is “C”, then this character is not interpreted correctly. So what we would need instead of “°” is this (assuming a Latin-1 setup):
(encode-coding-string "°" 'iso-8859-1) => "\260"
If you have your locale setup correctly, then you might not need this. Therefore, if you are using the C locale, write the call to
‘global-set-key’ as follows:
(global-set-key (kbd "\260") 'ring-map)
The rest is easy:
(define-key ring-map (kbd "a") 'iso-transl-a-ring) (define-key ring-map (kbd "A") 'iso-transl-A-ring)
But how will we enter the real `°’? The obvious solution does not work, because a string as a function definition will call the corresponding keyboard macro!
(define-key ring-map (kbd "SPC") "°")
The next idea is to call the command that
‘C-x 8 o’ calls: iso-transl-degree-sign. Use
‘C-x 8 C-h’ to list all available commands on that keymap. Unfortunately, it does not work:
(define-key ring-map (kbd "SPC") 'iso-transl-degree-sign)
Why not? See
‘C-h f iso-transl-degree-sign’: “iso-transl-degree-sign is a keyboard macro.” And this keyboard macro probably just calls the keybinding we have been changing anyway. So we will have to write our own command.
(define-key ring-map (kbd "SPC") (lambda () (interactive) (insert "°")))
Suppose you want to extend an existing prefix key or key sequence, e.g.,
‘C-x 8’. That map (
‘iso-transl’) is very useful, but not completely populated. E.g., “out of the box,” it does not provide means for the user to input the Greek delta characters (widely used in science and math). Fortunately, one can augment or extend
‘iso-transl’ very easily. In this case (thanks to Drew Adams), put the following lines in your InitFile (or a file you load from your InitFile)
(define-key 'iso-transl-ctl-x-8-map "d" [?δ]) (define-key 'iso-transl-ctl-x-8-map "D" [?Δ])
and restart emacs. One can then do, e.g.,
‘C-x 8 D’ to input the capital delta, Δ.
The reason an error is raised if you do not quote the symbol
‘iso-transl-ctl-x-8-map’ is that that variable is not defined until library `iso-transl.el’ is loaded.
It so happens that in this case the symbol
‘iso-transl-ctl-x-8-map’ also has a function definition – it is a command – and its function definition is the keymap. That is not always the case for a prefix keymap.
Of course, you will say, the function, like the variable, is not defined before library `iso-transl.el’ is loaded. So how does it work?
Since I’m a Windows user and use CUA, I copy C-x functionality onto the <C-lwindow> key. This way I can reach C-x prefixed functions consistantly even when I’ve got text selected. The same technique could copy them to just about any other available key.
(global-set-key (kbd "<C-lwindow>") (lookup-key global-map (kbd "C-x")))
NOTE This only copies the PREFIX KEY not any followup key. Meaning the C-x C-x function can now be reached using <C-lwindow> C-x but unless C-x <C-lwindow> is mapped to something, <C-lwindow> <C-lwindow> won’t be either.
You need to use a mode hook to copy the the C-c prefixed chords because the key convention says major modes define those keys so a different keymap is usually defined by each major mode.
(defun copy-C-c-to-M-lwindow () (local-set-key (kbd " <M-lwindow>") (lookup-key (current-local-map) (kbd "C-c"))))
(add-hook 'tal-mode-hook 'copy-C-c-to-M-lwindow) (add-hook 'ddl-mode-hook 'copy-C-c-to-M-lwindow) ...
On a Windows keyboard the key to the right of the spacebar with a picture of a menu on it is the <apps> key. It should be completely available both in its plain form and the shift, alt, … modified flavors. For example:
(global-set-key (kbd "<apps>") (lookup-key global-map (kbd "C-x")))
The [START] key on Windows systems (also called the windows key) is visible to Emacs. The one on the left is called <lwindow>. Some keyboards have one on the right called <rwindow>, but it’s often missing on laptops. They can make convenient prefix keys for user functions thus making thousands of convenient unused keystrokes available to users. Unfortunately under Microsoft Windows many keychords involving the Windows key are trapped by Windows itself before they ever get to Emacs - Windows-e, for example, brings up a new Explorer window. The number of shortcuts seems to increase with each new version of Windows, but it’s possible to get rid of most of them by using a registry hack. Create a .reg file with the following contents, run it, and reboot.
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer] "NoWinKeys"=dword:00000001
This will get rid of most, but not all, of Windows’ interceptions. It doesn’t seem to be possible to get rid of Win-L (lock computer), Win-P (projector), Win-U (usability/ease of access) and Win-<arrow keys> (manipulate window placement). It may be possible to use AutoHotKey to achieve similar results, but I haven’t tried it.
Once you’ve done that then the following bit of magic in your .emacs will make both Windows keys emit
(when (equal window-system 'w32) (setq w32-pass-lwindow-to-system nil w32-lwindow-modifier 'super w32-pass-rwindow-to-system nil w32-rwindow-modifier 'super w32-pass-apps-to-system nil w32-apps-modifier 'hyper w32-pass-alt-to-system nil w32-scroll-lock-modifier nil))
Of course, you could make one of them emit
‘hyper’ if you want. I chose
‘super’ rather than
‘hyper’ because I also use this .emacs on Xubuntu, and by default the Windows key there is mapped to
Binding keys is then done in the usual way (be careful to use a lower case
‘super’, upper case
(define-key global-map (kbd "s-r") 'recentf-open-files)