emacs create key modifier - emacs

I am using emacs on mac os.
I would like to map a modifier (Meta, Control,...) to a simple key.
Basically this is what I need : (global-set-key (kbd "a") 'hyper)
Here a is just the a key, no Control-a or whatever, just "a" alone whithout modifier.
I want "a" to become hyper for example but I can't find a solution for this.
Someone have a clue ?

This approach won't make a modifier key in the sense that you press another key with the modifier held down, but it lets you use hyper bindings nevertheless.
By default you can utilise the additional modifier keys via the C-x# prefix. See C-x#C-h for the full list.
These bindings are in function-key-map, and you can use the same method to get your own OS-independent bindings. e.g.:
(define-key function-key-map (kbd "C-c H") 'event-apply-hyper-modifier)
function-key-map is the parent for all local-function-key-map instances. Note carefully:
Entries in `local-function-key-map' are ignored if they conflict with bindings made in the minor mode, local, or global keymaps. I.e. the remapping only applies if the original key sequence would otherwise not have any binding.
C-hig (elisp) Translation Keymaps RET
So ensure that you use a key sequence with no existing bindings. (In theory that's quite limiting, but YMMV.)
Edit: To clarify, this gives you a way to access any existing 'hyper' bindings when you are using a machine without a hyper modifier key, but does not actually create a new modifier key. If you have no existing bindings to access, this technique has no advantages over a regular prefix binding.
I believe it's the case that, as Peter commented, creating a genuine modifier key is an OS-level task. AFAIK when you press a modifier key on its own, Emacs does not receive any input, and when you press a non-modifier with a modifier, Emacs receives the (modified) input. Conversely when you press/hold a non-modifier key, Emacs receives input immediately, and has no way to combine that input with some other input and treat it all as a single (modified) event.
IIRC, xmodmap would be the typical mechanism for achieving this in Unix systems, so that may well be the case for OSX. The following links may help:
http://computing.fnal.gov/docs/products/xemacs/v21_1/xemacs.info,.SuperandHyperKeys.html
http://www.emacswiki.org/emacs/XModMap
http://www.emacswiki.org/emacs/MetaKeyProblems

Related

Emacs prefix justifications?

Do any old-school emacs-ers know the justification for the prefix key's C-c and C-x? I'm binding my own keys for some custom functions, and I'm wondering if there's any sort of "standard" that defines consistency for these prefixes.
So far the only thing I've noticed is that C-x is used for manipulating buffers (C-x C-f, C-x C-s, etc), but I haven't found a general theme for C-c yet. Thoughts?
If you look at the manual, C-c followed by a single key is conventionally reserved for user-defined bindings, along with some other prefixes, which see.
I think people have a lot of strategies for managing their personal bindings, for example, I remap CapsLock to F2 and root most of my personal keymaps from there.

describe binding filtering in emacs

Is there a way to filter the list of available bindings (C-h b), so it will not show all the possible bindings but just the relevant ones for the used mode?
For example on org-mode, I get all the general C-x list plus all the bindings of the modes that have general key bindings plus all the major mode bindings, followed by all the org mode ones, followed by global bindings... 1369 lines in total... I'm using this view for learning my way around, perhaps there is a way to filter so as to find my way around.
I think you are looking for C-h m which runs the command describe-mode.
You will find more goodies in C-h C-h which runs the command help-for-help.
C-h m shows the doc for the current major mode, as well as currently enabled minor modes.
Often C-h m lists some of the more important local key bindings, that is, some of the bindings made for the current major mode. But not always, and typically it does not list all of the local bindings.
If you use library help-fns+.el then you can use command describe-keymap to list all of the local key bindings:
M-: (describe-keymap (current-local-map))
If you know the name of the local keymap variable (e.g. emacs-lisp-mode) then you can invoke describe-keymap interactively using C-h M-k, providing the map name at the prompt.
If you use Icicles then you can see all of the currently available key bindings using S-TAB (key completion). By default, the local bindings (i.e, those for the current major mode) are shown first, and are highlighted specially. (You can use C-, to sort the candidate bindings in other ways (by key name, prefix keys first; by command name).
After C-h b switch into Help-buffer and call M-xoccurRETorgRET which will display all lines containing "org".

Finding all the functions in various modes assigned to a specific shortcut

I'd like to find all the modes which do assign a function to some specific shortcut.
For example if I'm not mistaken a stock Emacs simply assigns (or defaults to) newline for S-return but while in org-mode S-return does invoke org-table-copy-down.
Is there an easy way to figure out which modes (both major and minor) do map a function to a specific shortcut? I can find all the shortcuts of one major mode using describe-mode but I'd like to find those for all the various modes. I don't mind if it were to only work for all the currently loaded modes.
Basically I'd like to find "free" or "relatively rarely re-mapped" key shortcuts, which are also easy to type (i.e. I'm not after doing "C-c a" because for a start C-c is a very convoluted key to reach and then having to then hit another key is one key too many for me. I'm more after re-mapping C-o, S-return, M-/ and other combo trivial and fast to reach).
You can find the current-mode bindings using C-h b.
You can get all of the keymaps currently available, using accessible-keymaps. You can find all the features loaded via variable features. But you would have to work to find all possible bindings for all possible modes from all files that you have loaded so far.
I recommend that you do it for a particular mode, one mode at a time. It's easy to check a given mode's key bindings.
You can even check the bindings of keymaps (such as minibuffer maps or the Isearch map) that are hard to see otherwise, if you use command C-h M-k (describe-keymap) from library `help-fns+.el. I use that when I want to see what keys are still available in a given keymap etc.
You can use
M-x describe-unbound-keys
to find out the free keys.
This is from third party library as said in comments.
I don't know the answer to your specific question, but I can give you my solution to getting easy-to-type keybindings that don't conflict with other modes.
In my set up, I've remappped CAPS-LOCK to Alt. Most people map it to CTRL, but I can hit CTRL relatively easily, while ALT is difficult. With this set-up, one of the easiest key combos to hit is M-space. So I use this as my own private keymap:
(define-prefix-command 'ty-keymap)
(global-set-key "\M- " ty-keymap)
(define-key ty-keymap " " 'just-one-space)
(define-key ty-keymap "j" 'join-next-line)
(define-key ty-keymap "s" 'mark-sexp)
(define-key ty-keymap "c" 'org-capture)
...
Note: by default, M-space is bound to just-one-space, which is useful. I've moved that to M-space-space. Bouncing my thumb twice on the spacebar is only a fraction slower than hitting it once, so it's not a big loss.
Since M-space isn't a keymap by default, this setup allows me to use all the keys on the keyboard, without further modification. That's a lot of real-estate, guaranteed to be free of any conflict with other packages - since well-behaved packages won't clobber a basic Emacs keybinding.
You might prefer another key combo, but the idea is the same. You could even use a function key as your prefix-command, so you could do <f5> followed by a letter for your commands.

Avoiding overlapped keybindings in emacs?

I have done some searching around and can not quite find what I am looking for so I figured I would ask. This could be due to not using the right terms however.
I am new to emacs, having used vim for eight years or so, but I really need an editor I can morph to my will so I am switching. Now in vim there is a leader key you can define to avoid overlapping bindings with various extensions. In emacs there are supposed to be chords set aside for the user only but various modes do not adhere to this. Is there a way in emacs I can ensure my custom bindings do not overlap similar to the vim leader key? The reason I ask is I want to keep my bindings sane.
The manual details the conventions:
C-hig (elisp) Key Binding Conventions RET
Sequences reserved for the end-user are:
C-c<letter> for any (un-modified) upper or lower case letter: [A-Za-z]
Function keys F5, F6, F7, F8, F9 (again, without modifier keys)
I recommend using the easiest of these sequences as prefix bindings, as you can then follow them with any key at all, giving you a large number of options.
Someone else's recommendation that I liked was to unbind C-z if you don't commonly use suspend-frame, as that opens up another convenient prefix.
Of course, if you set up Super and Hyper modifier keys for your OS & keyboard, you would likely gain more convenient sequences than you could find uses for. This is a very reasonable option for many people with the additional modifier keys found on many modern keyboards.
Finally, the key-chord library is quite a popular way of creating new convenient and non-conflicting bindings by using pairs of (un-modified) keys typed together or in quick succession (or a single key tapped twice). This works very well in my experience, although you obviously have to be very careful to avoid binding sequences which might occur naturally.
I personally use C-' as my 'leader key' for my personal keybinding map. You can create the prefix and bind keys to it like so:
(global-set-key (kbd "C-'") ctl-quote-map)
(define-key ctl-quote-map (kbd "C-p") 'stumpwm-move-window-up)
(define-key ctl-quote-map (kbd "C-n") 'stumpwm-move-window-down)
(define-key ctl-quote-map (kbd "C-f") 'stumpwm-move-window-right)
(define-key ctl-quote-map (kbd "C-b") 'stumpwm-move-window-left)
(define-key ctl-quote-map (kbd "r") 'stumpwm-interactive-resize-window)
Nobbody steps over something that obscure, and if you use the left control it's a balanced double pinky motion. C-; is also good, which is what I use for the stumpwm escape key.
There are Emacs keybinding conventions, some of which honestly surprised me.
The relevant pieces are that C-c [a-zA-Z] and <F[5-9]> are reserved for end users.
Good answers so far:
I'd throw in free-keys as a good way to check your binding is free before creating it - to avoid overlapping with yourself. helm-descbinds is good for finding your existing bindings to help you remember them as well incidentally.

Make a key behave as another key

I want certain keys and key combinations to behave as other keys or key combinations in Emacs. For example, I want F5 to behave as a substitute for C-c for every possible combination involving it, or C-S- as C-. Is it possible to do that without manually rebinding all such key combinations?
The keys you are referring to are known as 'prefix keys'. A prefix key has its own keymap, so to make another key behave the same, you need to assign it to the same keymap. For control-c, you use the mode-specific-map:
(global-set-key (kbd "<f5>") mode-specific-map)
Control on its own isn't a prefix key, or really a key at all, since it doesn't send a keypress to Emacs[1] until you hit another key. I'm not sure how to remap C-S- to C- within Emacs. You could do it system-wide with xmodmap, but that's probably not what you want.
[1] the control key (and shift, alt) do send a keypress to the operating system, but Emacs doesn't 'see' this unless there's another key pressed at the same time
I prefer
(define-key key-translation-map [f5] (kbd "\C-c"))
Here is a good resource.
To summarize the link given above: The disadvantage of global-set-key is that, when you define a key combination to enter a symbol, it doesn't work in isearch.
key-translation-map also has a problem. Imagine you defined a symbol | to execute a command and C-| to enter the symbol |, pressing C-| will execute the command.