Avoiding overlapped keybindings in emacs? - 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.

Related

emacs key binding command affects another

Previously I used C-SPC to activate/deactivate mark, now I use expand-region package and set key binding to C-# like this:
(global-set-key (kbd "C-#") 'er/expand-region)
But this affected the C-SPC keybinding also, so it is also bound to expand-region.
What I need is C-# bind to expand-region and C-SPC to bind to old activate/deactivate mark.
Suggest you refer to:
set-mark-command not working emacs with C-SPC
and
https://www.gnu.org/software/emacs/manual/html_node/emacs/Setting-Mark.html
Quoting from the latter:
"Footnotes [1] There is no C-<SPC> character in ASCII; usually, typing C-<SPC> on a text terminal gives the character C-#. This key is also bound to set-mark-command, so unless you are unlucky enough to have a text terminal that behaves differently, you might as well think of C-# as C-<SPC>."
I think you'll find that they are not separate keys; C-SPC sends a code that's the same as C-#. I think that means you'll have to find somewhere else to bind one of the functions, (even if you have to override expand-region)
Apologies for a second answer... I think the first was wrong because I have now been able to make separate definitions for C-SPC and C-#, as described below.
This works to define C-# and C-SPC separately:
(global-set-key [?\C-#] 'beginning-of-line)
(global-set-key (kbd "C-SPC") 'end-of-line)
To give credit, I derived the answer from here: Rebind C-space in Emacs
after googling "emacs control space"
(Regarding your question, "what key should I bind this expand-region to?", I ordinarily use C-h C-k and type some key I don't think I use. Then look at the function that that key is bound to by default. If it seems useful to me, I try another key and keep looking. If I feel like I will never use the default definition, I redefine it for my own purposes.)

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.

Additional modifier keys in emacs?

I have been modifying my Emacs setup quite alot recently but I reached a problem which is starting to annoy me. I would like to be able to introduce additional modifier like keys. What I am trying to do, to make things clearer, is when I am in dired-mode (which doesn't accept textual input so normal letters can be rebound) I would like it so that when I hold down the letter s and press j or l the cursor moves to the next and previous directory line respectively. Effectively making the s key act like a modifier.
I have looked into making the s apply a modifier such as super or hyper but those are all used for global things. Is this possible? if not then that's a shame.
Edit:
There seems to be some confusion with what I'm after. If I define a normal key sequence such as
(define-key map (kbd "s j") 'dired-next-dirline)
Then I have to keep pressing the s key every time before I press j to move to the next directory line. This is not what I am looking for (not to sound angry :P) I want s to act like a modifier where I can keep the s key held down and keep tapping j to move down the lines.
I hope I have made this more clear. Thanks.
I dug around in the code and came up with this. It binds s j to my-dired-next-dirline, but right after you've done that, just j is enough to do it again. Any other key resets the temporary binding.
Note that the function set-temporary-overlay-map was added in Emacs 24.2, which at the time of this writing hasn't been released yet, so you'll need to build Emacs from git.
(defun my-dired-next-dirline ()
(interactive)
(dired-next-dirline 1)
(set-temporary-overlay-map
(let ((map (make-sparse-keymap)))
(define-key map [?j] 'my-dired-next-dirline)
map)
nil))
(eval-after-load "dired"
'(progn
(let ((prefix-map (make-sparse-keymap)))
(define-key prefix-map "j" 'my-dired-next-dirline)
(define-key dired-mode-map "s" prefix-map))))
Key chords may be what you are looking for.
This question pre-dates it, but the Hydra package allows you to do this and more.
https://github.com/abo-abo/hydra
See examples here: https://github.com/abo-abo/hydra/blob/master/hydra-examples.el and here https://github.com/abo-abo/hydra/blob/master/hydra-test.el
Wiki also has more useful examples https://github.com/abo-abo/hydra/wiki/Hydras-by-Topic
Hydra originally only had a rather opaque metaphorical syntax, which made it quite difficult to get started with. I'm not sure why such a bizarre double-layer of metaphors from colors to mythical stories and monsters with many heads being "vanquished" was used to describe features which are essentially:
do action and remain in the key-mode (effectively what you're asking for.) (a so called blue hydra)
do action and quit the key-mode (a red hydra)
and so on.
Thankfully a more literal syntax was added in Feb 2015, inspect the tests to see the color and literal syntax side by side. (also this commit https://github.com/abo-abo/hydra/commit/0a3cc60f5856eb4a38204b9075d67d058ba56bef)
See also this article by the author. http://oremacs.com/2015/02/02/colorful-hydrae/
If key chords is not what you are looking for (as you suggested on a comment to Tom's answer), then you seem to want a simple define-key that uses a key sequence. i.e.
(define-key dired-mode-map (kbd "s j") 'dired-previous-line)
This has the disadvantage of disabling the original functionality of the s key, but you can rebind that to the sequence s-s, I suppose.

Vim-like */# - (next/previous word at point) in Emacs?

I switched from Vim to Emacs, and I am so crazy for Emacs now.
But there is a very, very useful trick in Vim and I can't use conveniently in Emacs, that's find next/previous word at point. Those are very handy with */# in Vim.
The simplest way is to move to the beginning of the word at point and then C-s and C-w, use C-s/C-r to find next/previous word.
Then I found another trick from Mastering Emacs, but still some minor bug.
Today I find a plugin - vimpulse which simulate vim in Emacs. And I can use */# there just like Vim!!
But seems the vimpulse will automatically enables Viper.
So, are there any other methods to implement this requirement? Or, Can I automatically disable Viper if I use vimpulse?
First, the answer
Well, as is often the case, we can do better in Emacs. The package I use for this is highlight-symbols
Specifically, I bind a series of its commands with variations of the F3 key:
;;;;;;;;;;;;;;;;;;;;;;
;; highlight-symbol ;;
;;;;;;;;;;;;;;;;;;;;;;
(require 'highlight-symbol)
(global-set-key [f3] 'highlight-symbol-next)
(global-set-key [(shift f3)] 'highlight-symbol-prev)
(global-set-key [(control f3)] 'highlight-symbol-at-point)
(global-set-key [(control meta f3)] 'highlight-symbol-query-replace)
Next/previous symbol is nice. But I find it most helpful when reading an
algorithm to highlight a few key variables.
On symbols vs words
The distinction between words and symbols is very nice for
programming. I'm not sure if other editors offer this distinction.
An alternative
Another related tool I find very useful for programming is iedit.
Here is how I load it:
(autoload 'iedit-mode "iedit")
(global-set-key [(control \;)] 'iedit-mode)
(define-key isearch-mode-map [(control \;)] 'iedit-mode)
To do what you want, go to a symbol and press C-;, now
tab and shift tab will move amongst matching symbols.
C-' (single-quote) will show you an adhoc occur view of the
buffer.
For a bonus refactoring tool, mark a region (maybe a class) where you want to
rename the symbol (variable), and press C-; again and only matches
within that region will be edited.
Try Evil: https://gitorious.org/evil/pages/Home
*/# works nice with it.

emacs get rid of C-x

Is there a way I can stop needing C-X before any shortcut and, for example, just use C-c to leave?
Also, if possible, what disadvantages could this have?
I'm looking for a simpler way of using GNU Emacs, but not sure if I can find one.
(this is too long for a comment)
Not really an answer but I know that some people hate the "C-x anything" in Emacs not just because you have to "type a lot of keys" but also because from a touch-typing point of view C-x doesn't make that much sense.
But of course in Emacs everything is configurable. I'm using "C-," instead of C-x and I honestly find it much easier this way.
I use my left pinky to hit CTRL, which is the key physically located at the left of 'a' on a QWERTY keyboard: i.e. the key that used to be CTRL but that now often is labelled CAPS-Lock [and you can remap it to CTRL].
I then touch-type ',' with my right hand.
To this end I added this to my config:
(define-key global-map [(control ,)] ctl-x-map)
You won't have less keys to type to do, say, a C-x C-c (which you can now do both by doing C-x C-c or by doing C-, C-c), but at least you won't be distorting your fingers as much ; )
Also note that as explained here (see user "scottfrazer"'s +40+ upvoted and accepted answer) it may be better to create a minor mode for all your key mappings instead of directly define a global mapping:
Globally override key binding in Emacs
My recommendation is to first learn the standard Emacs key bindings, then, after a while change whichever ones you think will do you the most good to change.
See also CUA mode, in the Emacs docs -- I don't use or recommend CUA mode, but a lot of people (esp. newbies?) use it to keep their cut, copy, paste key habits.
After looking at the source code of boon.el, I found out about ctl-x-map. C-x is a prefix key, prefix keys often have their own maps.
To use M-m for C-x shortcuts and unbind C-x, use this
(global-set-key (kbd "M-m") 'ctl-x-map)
(global-set-key (kbd "C-x") nil)
I think it is a very bad idea, because the C-x prefix can handle such situations like C-x s vs C-x C-s. And: you are really f*cked when you read emacs tutorials and have your own keybindings
If you want some other keybindings read ErgoEmacs Keybinding or How to Set Emacs's User Interface to Modern Conventions