xremap 拯救我的小拇指

注意
本文最后更新于 2024-10-20,文中内容可能已过时。

我常用的编辑器是 vim,最近开始入坑 Emacs,觉得使用 M-x 导致小拇指极其难受。于是在谷歌搜索一番后,发现可以使用 xremap 重映射功能键,把 CtrlAlt 映射到更加方便的键盘位。

下载 xremap

可以到网站下载,选择 x11。解压后即可看到 xremap 可执行文件。

配置

可以编辑 ~/.config/xremap.yaml,具体的键位名称可以参考:keys

注意
For KEY_XXX and KEY_YYY, use these names. You can skip KEY_ and the name is case-insensitive. So KEY_CAPSLOCK, CAPSLOCK, and CapsLock are the same thing

modmap

注意
modmap is for key-to-key remapping like xmodmap. Note that remapping a key to a modifier key, e.g. CapsLock to Control_L, is supported only in modmap since keymap handles modifier keys differently.
  • modmap一一对应的关系,不像 keymap 可以使用组合方式。比如,我把 CapsLock 当成 Esc 使用。

  • 同时,我们还可以通过定义一个触发规则,把一个键位对应到多个事件,这个是通过定义 heldalone 组合来实现。比如我这里把 Ctrl_L 映射为两个行为

    • 如果是单独触发,在时间 alone_timeout_millis 内没有触发其他的按键,则对应 Esc
    • 如果是组合触发,比如我使用 Ctrl-k 来切换 tmux window
注意
If you specify a map containing held and alone, you can use the key for two purposes. The key is considered alone if it’s pressed and released within alone_timeout_millis (default: 1000) before any other key is pressed. Otherwise it’s considered held.

keymap

keymap 可以实现组合键。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
## https://github.com/emberian/evdev/blob/1d020f11b283b0648427a2844b6b980f1a268221/src/scancodes.rs#L26-L572
modmap:
  - name: Cap as Esc # Optional
    application: # Optional
      not: Google-chrome
      # or
      # only: [vim, nvim, neovim]
    remap: # Required
      CapsLock:
        # held: CapsLock
        held: Ctrl_L
        alone: Esc
        alone_timeout_millis: 1000
      Ctrl_L:
        held: Ctrl_L
        alone: Esc
        alone_timeout_millis: 1000 ## default:1000
keymap:
  - name: Right Arrow to complete in zsh
    application: # Optional
      not: Google-chrome
      # or
      # only: [vim, nvim, neovim]
    remap:
      # C-i: [Ctrl_L-Right]
      Alt-i: [Ctrl_L-Right]
      Alt-DOT: [Ctrl_L-Right]
      C-DOT: [Right, Right]
      C-COMMA: [Ctrl_L-Right]
      # Ctrl_L-SEMICOLON: [SPACE, Shift-KEY_BACKSLASH]
      # Ctrl_L-APOSTROPHE: [SPACE, Shift-KEY_BACKSLASH]
      C-SEMICOLON: [SPACE, Shift-KEY_BACKSLASH]
      C-APOSTROPHE: [SPACE, Shift-KEY_BACKSLASH]
      C-MINUS: [SPACE, Shift-KEY_MINUS]
  - name: Arrow
    remap:
      Alt-c: [Ctrl_L-c]
      Alt-v: [Shift-Insert]
      Alt-SPACE: Shift-Insert
      Shift-SPACE: Shift-Insert
      # Ctrl_L-SPACE: Shift-Insert
      Alt-h: [Ctrl_L-Left]
      Alt-j: Down
      Alt-k: Up
      Alt-l: [Ctrl_L-Right]

这里需要区分 modmapkeymap

  • modmap is for key-to-key remapping like xmodmap. Note that remapping a key to a modifier key, e.g. CapsLock to Control_L, is supported only in modmap since keymap handles modifier keys differently.
  • keymap is for remapping a sequence of key combinations to another sequence of key combinations or other actions.

运行

使用 root 权限运行即可

1
2
## 需要 root 权限执行
sudo /home/william/xremap /home/william/.config/xremap.yaml

如果不使用 root 执行,则需要设置

1
2
3
4
5
6
## 需要获取 input 执行权限
sudo gpasswd -a william input
echo 'KERNEL=="uinput", GROUP="input", TAG+="uaccess"' | sudo tee /etc/udev/rules.d/input.rules

## 需要重启生效,下次就可以使用普通用户执行了
reboot

ref

william 支付宝支付宝
william 微信微信
0%