reddit / emacs

How to bind multiple keys to a command?

konrad1977 · 2022-03-06 08:23
Hi I am trying to bind multiple keys for a toggle of bookmarks, but I cant really figure out how to do that. What I want is `M-b-b` for toggle bookmarks. `(global-set-key (kbd "M-b-b") #'bm-toggle)` but that doesnt work because: `"M- must prefix a single character, not b-b"` I tried multiple combinations, like "M-b M-b" but without any success. Is it possible to do this with global-set-key or even the :bind in use-package? M = Meta key
flexibeast · 2022-03-06 08:48
`"M-b b"` = "M-b", followed by "b" `"M-b M-b"` = "M-b", followed by "M-b"
AuroraDraco · 2022-03-06 09:55
I can't say I understand what M-b-b stands for, and emacs seems to have the same issue. As the other commenter said, if what you need is Mod-b followed by a b, you need to do (kbd "M-b b"). If that's not what you mean, please explain
SlowValue · 2022-03-06 10:57
I just explain what is happening, I'm to lazy to give you a working example: Let's assume, you want to use keypres "M-b" followed by keypres "b" to call your `bm-toggle` function. This would have the notation `M-b b`. But `M-b` is already bound to `backward-word`, so pressing `M-b b` would go back on word and then insert a character "b".... that is not what you want. But how could emacs decide, whether it should call `backward-word` and `insert "b"`, or `bm-toggle`? That is what keymaps are for. You first have to bind `M-b` to a keymap and in this keymap bind commands like `b` to `bm-toggle`. Of course you would loose the `M-b` -> `backward-word` mapping. That is one of the reasons for the convention, that users should only define `C-c <letter>` for own keybindings. Read more [on Mastering Emacs](https://www.masteringemacs.org/article/mastering-key-bindings-emacs) and [Emacs Wiki](https://www.emacswiki.org/emacs/PrefixKey) and [Emacs Manual](https://www.gnu.org/software/emacs/manual/html_node/elisp/Prefix-Keys.html) and this [reddit post](https://old.reddit.com/r/emacs/comments/3ytb6n/a_better_way_to_define_a_new_prefix/). (I just googled those links...)
konrad1977 · 2022-03-06 13:39
Sorry, M is Meta-key (cmd for me as a mac user). So I wanted to have something like `Cmd-b-b` to toggle, because I thought it would be smart to do `Cmd-b-n` for next or `Cmd-b-p` for previous bookmark. `C-c C-b` works just fine. I am new to Emacs and its not that hard to adopt this.
big_lentil · 2022-03-06 14:26
I don't think emacs supports press three keys at the same time kinda controls. Emacs keychords are essentially sequences of "individual" key presses; control and meta are just modifiers and are used to alter the meaning of other keys. So you can't do C-b-b as it's not a valid step in such a sequence, but you can do C-b b or such. Edit: You actually can have a three key step in a keychord, but only if two of them are modifiers, e.g. C-M-f Edit2: Then again even if emacs supported something like M-b-b how would you press b twice at the same time? Maybe I misunderstood
arthurno1 · 2022-03-06 15:51
(global-set-key (kbd "M-b M-b") #'bm-toggle)
thetemp_ · 2022-03-06 16:06
In Emacs key notation, only a modifier key can be followed by a dash, because dash indicates that you are holding down all keys connected by the dash at the same time. Only the last of the dash-connected keys can be a regular non-modifier key. The modifier keys are A, C, H, M, S, and s. If you want to indicate pressing Meta and "b" at the same time, followed by another press of "b", you would use "M-b b". If you want to indicate Meta and "b" pressed together 2 times in sequence, you would do "M-b M-b". But note also that "M-b" is already bound to backward-word. So if you try to use it as a prefix key, you might get a "non-prefix key error" unless you remove it's binding to backward-word first.
konrad1977 · 2022-03-06 17:17
I use `general.el` to do things like "SPC-A f u" (Application->Feed-Update). So I thought you could do something like that with global-set-key. I use Evil but I am new to both emacs and vi, so I dont know all the keys yet, thats also what I like with emacs, how I can configure it to work like I want it to.
dpoggio · 2022-03-06 22:57
This is plumbing. `(global-unset-key (kbd "M-b"))` is enough. Then any `global-set-key` using `M-b` as a prefix will automatically setup the prefix keymap. The only reason I see to manually define the prefix keymap would be to also define an alias so that which-key-mode shows the right prefix name.
oantolin · 2022-03-07 01:51
When you say Cmd-b-b what do you mean? Press and hold Cmd while you tap b twice? In Emacs's notation that would be M-b M-b (and Emacs cannot tell if you released the Cmd in between the two b's or not). If you meant something different, please describe it and I'm sure I or someone else here will be able to explain how to write that in Emacs.
konrad1977 · 2022-03-07 05:42
"M-b M-b" is what I wanted. Thanks!