reddit / emacs
Can I use a non-modifier key as a modifier key in Emacs?
biggrben · 2022-07-12 10:13
I am experimenting with Emacs and trying to make some weird key-bindings just as a playful activity. I know that something simple like this works fine:
(global-unset-key (kbd "/"))
(global-set-key (kbd "/ s") 'save-buffer)
Now I can type `/s` to save the current buffer and I can type `C-q /` when I need to insert a literal `/`.
But can I now make a key binding where `/` is a modifier key? For example, can I somehow get this to work?
(global-unset-key (kbd "/"))
(global-set-key (kbd "/-s") 'save-buffer)
This does not work for me of course. When I type `/-s` (press and hold `/`, press `s`, release `/`) I get this error,
/ s is undefined
I see why this error happens. But is there anyway to make this work and treat `/` as a modifier key?
Zaeph · 2022-07-12 10:22
Yes, it is possible.
Look at [`define-prefix-command`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Prefix-Keys.html#index-define_002dprefix_002dcommand).
Edit: Wait, no. This would just define `/` as a prefix to further commands, so exactly the same as you’ve done. I don’t think it’s possible to define new modifiers, [according to this page](https://www.gnu.org/software/emacs/manual/html_node/emacs/Modifier-Keys.html).
_viz_ · 2022-07-12 12:18
Yes. See event-apply-alt-modifier and friends but it will behave more like modifiers with sticky keys turned on.
[deleted] · 2022-07-12 14:23
[deleted]
biggrben · 2022-07-12 14:41
Is it going to offer something that is different from my first example? In my first code example, `/ s` already feels like sticky keys where I need to first type `/` and then type `s` to save a file.
Is there a minimal example I can use to get started with `event-apply-control-modifier`? I tried this:
(global-unset-key (kbd "a"))
(global-set-key (kbd "a") (lambda () (interactive) (event-apply-control-modifier t)))
Now I type `a s` but it does nothing. How could I fix this example so that `a s` invokes `save-buffer`?
[deleted] · 2022-07-12 14:46
[deleted]
[deleted] · 2022-07-12 14:52
[deleted]
_viz_ · 2022-07-12 15:09
It is not hard to do M-? event-apply-control-modifier RET and read the results. If you do, you will see that the standard bindings for these are defined in the function-key-map keymap.
[deleted] · 2022-07-12 15:10
[deleted]
timmymayes · 2022-07-12 16:21
I setup my app key to be hyper mod using xmodmap. Are you trying to gain a mod key? I’ve added hyper and super which is great
[deleted] · 2022-07-12 17:21
[deleted]
biggrben · 2022-07-12 17:33
Sorry but I don't see how this is helping. I did this,
(global-unset-key (kbd "/"))
(define-key function-key-map (kbd "/") 'event-apply-control-modifier)
But how does this help? Now `/ s` just translates to `C-s` which invokes `isearch-forward`. So to work around this I add this,
(global-set-key (kbd "/-s") 'save-buffer)
But this behaves like my first example. I now need to type `/ s` to invoke `save-buffer` which my first example in the post already does. That brings back to my previous question.
> _viz_: Yes. See event-apply-alt-modifier and friends but it will behave more like modifiers with sticky keys turned on.
>> biggrben: Is it going to offer something that is different from my first example? In my first code example, `/ s` already feels like sticky keys where I need to first type `/` and then type `s` to save a file.
thetemp_ · 2022-07-12 21:58
Not really possible within Emacs. But there are several tools for doing it outside of Emacs.
However, the [key-chord](https://github.com/emacsorphanage/key-chord) package comes sort of close. It lets you bind a command to 2 keys that you press at the same time.
snubbyJspreaker · 2022-07-13 04:04
Unbind C-z its a dumb bind anyway, then make a new map for C-z and dump your bits and bobs there. You could have C-z \\ do save. Or just use builtin CUA mode. I think that makes C-s C-f function as File Save and File Open.
[CUA-mode](https://www.gnu.org/software/emacs/manual/html_node/emacs/CUA-Bindings.html)
Or use evil-mode and make / a leader key
biggrben · 2022-07-13 08:46
Not sure I understand. `C-z \` does not help with turning `/` into a modifier key where pressing `/-s` together can run `save-buffer`. Likewise for `C-s C-f`. And evil-mode with `/` leader key requires typing `ESC / s` for saving, not `/ s`, due to its modal nature. Again it does not make `/` a modifier key.
I think at this point it is safe to say now that it is impossible to turn a non-modifier key into a modifier key within Emacs. The most one can do is use `/ s` (both keys pressed separately) to run `save-buffer`. I think that's a good conclusion of all the discussion in this post.
_viz_ · 2022-07-13 09:03
Sorry, I got confused by the title. Your title says "modifier" but what you're after is emulating the partial keysequence "C-x C-" am I right? It is not immediately obvious what you need from the body text since no one stops anyone from binding <Some-modifier>-s to save-buffer.
Anyway, I think you can do this by looking up the relevant binding but I'm not sure how exactly.
biggrben · 2022-07-13 09:09
Yes, emulating the partial keysequence `C-x C-` would indeed be a way to do it. Thanks for the idea. That thought did not even occur to me. It's a clever thought. Thanks!
So far I was trying to figure out if there is a way in Emacs to make a keybinding for `/`+`s` where Emacs can recognize that I am pressing `/` and `s` together (holding `/` and pressing `s`). From the discussion so far, it looks like this is not possible. But maybe your idea of emulating `C-x C-` to produce the same desired effect may be possible!
snubbyJspreaker · 2022-07-13 12:22
I think you are pissin up a rope. Basically what you are trying to do is not gonna happen (I could be wrong, but you'd need a detailed understanding of emacs and emacs lisp) but what I was offering were a couple of alternatives.
I think maybe you are trying to save a few key strokes and that is why I suggested CUA mode. CUA mode gives you key bindings similar to windows gui apps, but if I remember right it costs you emacs motion keys (again, could be wrong) I think there is a god-mode that is some weird hybrid of the two.
But, why not give good old vanilla emacs keybindings a shot? Especially if you are new to emacs. Mostly (except for C-z) they are well thought out and in not too terribly difficult to learn.
biggrben · 2022-07-13 14:06
You are right that I am doing something pointless for fun and I stated as much in my post. I am not trying to save key strokes. I use good old vanilla emacs key bindings myself. As stated in my post, I am experimenting with Emacs and trying to see what is possible and what isn't by making weird key-bindings as a playful activity.
snubbyJspreaker · 2022-07-13 14:41
So sorry for the misunderstanding. I understand now.