0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu で、どこでも Emacs キーバインドを使う

0
Posted at

自分は、どの環境でも以下のようなキーバインドで使っています。

  • Caps Lock を Emacs キーバインドに割り当てて(C-d = 削除など)
  • 左下の Ctrl を通常のショートカットキーに割り当て(Ctrl+C = コピーなど)

ここでは、これを Xremap を使って Ubuntu で行うための手順をまとめます。

Caps Lock キーの割り当ての変更

まずは、udev を使って CapsLock キーを右Ctrlに割り当てます。

sudo tee /etc/udev/hwdb.d/90-caps-to-rctrl.hwdb <<'EOF'
evdev:input:b0003v*p*
 KEYBOARD_KEY_3a=rightctrl
 KEYBOARD_KEY_70039=rightctrl
EOF
sudo systemd-hwdb update
sudo udevadm trigger

uinput モジュールの設定

Xremap の動作に必要な uinput モジュールをロードします。

echo uinput | sudo tee /etc/modules-load.d/uinput.conf
sudo modprobe uinput

続いて、 /dev/uinput に権限を付与しておきます。 1

sudo tee /etc/udev/rules.d/99-uinput.rules <<'EOF'
KERNEL=="uinput", GROUP="input", MODE="0660"
EOF
sudo udevadm control --reload-rules
sudo udevadm trigger

ls -l /dev/uinput を打って、以下のように input グループで rw になっていればOKです。

crw-rw---- 1 root input 10, 223 Jun 20 20:26 /dev/uinput

Xreamp のインストール

Xremap を /usr/bin にインストールします。

curl -LO https://github.com/xremap/xremap/releases/latest/download/xremap-linux-x86_64-full.zip
unzip xremap-linux-x86_64-full.zip 
sudo mv xremap /usr/bin/

また、xremap ユーザを作成しておきます。

sudo useradd --no-create-home --shell /bin/false --user-group --groups input --system xremap

Xreamp の設定

https://github.com/xremap/xremap/blob/master/example/emacs.yml を参考に設定ファイルを作ります。
このとき、各割り当てには Ctrl ではなく Ctrl_R を使います。

また、ターミナルに対しては適用したくないので2、application に除外設定をしておきます。

sudo mkdir -p /etc/xremap
sudo touch /etc/xremap/config.yml
sudo chown xremap:xremap /etc/xremap/config.yml
sudo chmod 644 /etc/xremap/config.yml
/etc/xremap/config.yml
keymap:
  - name: Emacs
    application:
      not:
        - Gnome-terminal
        - org.gnome.Ptyxis
        - Emacs
    remap:
      # Cursor
      Ctrl_R-b: { with_mark: left }
      Ctrl_R-f: { with_mark: right }
      Ctrl_R-p: { with_mark: up }
      Ctrl_R-n: { with_mark: down }
      # Forward/Backward word
      M-b: { with_mark: C-left }
      M-f: { with_mark: C-right }
      # Beginning/End of line
      Ctrl_R-a: { with_mark: home }
      Ctrl_R-e: { with_mark: end }
      # Page up/down
      M-v: { with_mark: pageup }
      Ctrl_R-v: { with_mark: pagedown }
      # Beginning/End of file
      M-Shift-comma: { with_mark: C-home }
      M-Shift-dot: { with_mark: C-end }
      # Newline
      Ctrl_R-m: enter
      Ctrl_R-j: enter
      Ctrl_R-o: [enter, left]
      # Tab
      Ctrl_R-i: tab
      # Copy
      Ctrl_R-w: [C-x, { set_mark: false }]
      M-w: [C-c, { set_mark: false }]
      Ctrl_R-y: [C-v, { set_mark: false }]
      # Delete
      Ctrl_R-d: [delete, { set_mark: false }]
      Ctrl_R-h: [backspace, { set_mark: false}]
      M-d: [C-delete, { set_mark: false }]
      # Kill line
      Ctrl_R-k: [Shift-end, C-x, { set_mark: false }]
      # Undo
      Ctrl_R-slash: [C-z, { set_mark: false }]
      Ctrl_R-Shift-ro: C-z
      # Mark
      Ctrl_R-space: { set_mark: true }
      # Search
      Ctrl_R-s: F3
      Ctrl_R-r: Shift-F3
      M-Shift-5: C-h
      # Cancel
      Ctrl_R-g: [esc, { set_mark: false }]
      # C-x YYY
      Ctrl_R-x:
        remap:
          # C-x h (select all)
          h: [C-home, C-a, { set_mark: true }]
          # C-x C-f (open)
          Ctrl_R-f: C-o
          # C-x C-s (save)
          Ctrl_R-s: C-s
          # C-x k (kill tab)
          k: C-f4
          # C-x C-c (exit)
          Ctrl_R-c: C-q
          # C-x u (undo)
          u: [C-z, { set_mark: false }]

Xreamp のGNOME 拡張機能の導入

アプリケーションの除外設定を機能させるための手順です。
除外設定をしない場合は不要です。

xremap でアクティブなウィンドウを認識させるために、GNOME拡張機能を導入します。

まず、Extension Manage をインストール。

sudo apt install gnome-shell-extension-manager

続いて、アプリケーション一覧から Extension Manage を起動して、探すタブで「xremap」と検索してインストールします。

さらに、xremap との接続のためグループを作ります。

sudo groupadd --system xremap-`whoami`
sudo usermod --append --groups xremap-`whoami` `whoami`

Xreamp の自動起動設定

Running as a system service を参考に/etc/systemd/system/xremap.service に設定ファイルを作ります。
(下記の {username} は現在のログインユーザ名に、{uid} は id コマンドで確認した値に置き換えてください)

[Unit]
Description=Xremap
After=default.target

[Service]
ExecStart=/usr/bin/xremap --desktop=socket --watch=device /etc/xremap/config.yml
Restart=always
StandardOutput=journal
StandardError=journal
User=xremap
Group=xremap
SupplementaryGroups=input
RuntimeDirectory=xremap
RuntimeDirectoryMode=0755
RuntimeDirectoryPreserve=yes
ExecStartPre=install --directory --mode 2770 --owner xremap --group xremap-{username} /run/xremap/{uid}
SupplementaryGroups=xremap-{username}

[Install]
WantedBy=default.target

自動起動を設定しつつ、xremap を起動します。

sudo systemctl daemon-reload
sudo systemctl enable xremap.service
sudo systemctl start xremap.service

最後に、ログを見てエラーが出ていなければ完了です。

systemctl status xremap.service

快適な環境で Ubuntu を楽しみましょう!

  1. これをしないと、Error: Failed to prepare an output device. Permission denied (os error 13) で起動に失敗します。 ↩

  2. ターミナル自体のキーマップとぶつかるため ↩

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?