reddit / emacs
[May be OT]: shortcut keys based on position, not key
koalillo · 2022-12-13 19:06
Does anyone know if anyone ever tried to write a text editor (or something else, or an Emacs extension) where shortcuts were based on the physical position of a key, instead of which key it is?
The context is that most editors (including Emacs and vim) have issues when used on non-US keyboards, because stuff is in different places. Many people solve this problem by switching to a US keyboard (which has some other advantages), but I don't really like this solution.
I can see some challenges for this, like the differences between 104 and 105 key layouts, etc. And this is more from a theoretical standpoint....
mtriska · 2022-12-13 19:44
Personally, I am using Emacs with a Dvorak keyboard layout, and I find it works better than a US keyboard.
I suspect that the original developer of Emacs might also have used a Dvorak layout and found the positions to work very well.
koalillo · 2022-12-13 19:46
Oh, I was thinking more like intl layouts. For example, on a Spanish keyboard, the backtick, backslash, caret, and other keys require a modifier to hit. Or for example, <> are not neighbors- they are the same key (> is shift+<). So while learning Emacs, I did find some shortcuts that were really difficult to type.
thetemp_ · 2022-12-13 20:08
There are a lot of different layouts. There is also the bepo layout, which I believe is the French answer to Dvorak. And there are also many non-english versions of Dvorak, plus there's Colemak and its variations, and Workman, etc.
But Emacs makes it easy to change most keybindings to whatever you prefer. I don't think most people would even consider Emacs keybindings with the standard US layout to be perfect. In fact, you'll hear many complaints about it.
There are many modal editing systems for Emacs, and most of them either leave it to the user to set the bindings or are very easy to rearrange as you see fit. But I understand from your other thread that you're not interested in modal systems.
So you might want to check out [Ergo Emacs](https://ergoemacs.github.io/). It's a non-modal system that fully rearranges the Emacs keybindings to the supposedly optimal physical locations on the keyboard. You set the layout you're using, and then your keys are in the same places as they would be in any other layout. They have a large set of layouts defined, including a Spanish layout.
koalillo · 2022-12-13 20:15
Wait, ergoemacs seems like the holy grail?
Why I haven't heard about it before? Also... is it too good to be true? Maybe it has some downside?
ieure · 2022-12-13 20:52
The software has no idea what the physical layout of the keyboard is, only which key events have been received. So you can't really do this without changing the keymapping, either on the keyboard itself (if you have one that can run QMK or something like that) or making a set of per-keyboard bindings.
koalillo · 2022-12-13 21:15
Oh, ergoemacs as mentioned in a different comment ( https://www.reddit.com/r/emacs/comments/zl3xaf/may_be_ot_shortcut_keys_based_on_position_not_key/j03eaxa/ ) seems to do the latter, using a clever strategy to be "generic".
thblt · 2022-12-13 21:16
I’ve played a bit with VSCode and it seems to let you bind to keycodes instead of characters, which is the closest you can get to « binding to a physical position ». Its default keymap remains character-based, though, so paste is C-v wherever that « v »is.
koalillo · 2022-12-13 21:19
Cool. I'm trying to wean myself away from VS Code... and I never got to shortcuts there that were problematic to type on my keyboard... but it's good to know!
arthurno1 · 2022-12-14 03:37
Basically, keyboard layouts are different for different languages. Emacs works with characters, not key codes. Key codes are sent by a keyboard to the system, and translated into characters depending on language locale and layout etc. What you can do is probably remap some keys based on the layout, maybe as a minor mode that gets activated depending on system locale when Emacs starts, or manually when you switch the locale language. Sort of.
I experience this when I switch between Swedish and Croatian language, and my 'z' jumps from the bottom row of keys to the third row of keys. I use C-z as a prefix instead of M-x, and it suddenly becomes mildly annoying :). I thought to try remapping myself, but I do use Croatian layout so rarely, so I didn't bother; I just type text without extra diacritics over the letters, everyone understands anyway.
[Swedish layout](https://www.branah.com/swedish)
[Croatian layout](https://www.branah.com/croatian)
Notice the 'z' and 'y' switching place :). Quote also moves, but I don't use Croatian layout for writing the code. Since I use C-z as a prefix key, I think it would be a bit more involved to fix this for me; I don't know, I have looked at that so much.
koalillo · 2022-12-14 19:10
I think ergoemacs as mentioned in other threads fixes this with a clever trick.
arthurno1 · 2022-12-15 01:04
They seem to do exactly what I have describesd to you :-). Probably with some extra twists.
I wouldn't say it is a "trick", rebinding keys is a common operation in Emacs.
koalillo · 2022-12-15 20:36
I mean, the trick is that they have a lot of arrays on their code with keyboard layouts:
https://github.com/ergoemacs/ergoemacs-mode/blob/master/ergoemacs-layouts.el#L33
They add a layer of indirection, so they define keybindings that are layout-independent.