Mac で ctrl-k を行末までカットしてクリップボードに入れる Karabiner Elements 設定
tl;dr
Karabiner Elements で以下の json を Complex Modifications に追加
{
"description": "Ctrl-K: First press Copy & Delete, Second press Delete",
"manipulators": [
{
"conditions": [
{
"name": "ctrl_k_count",
"type": "variable_if",
"value": 1
},
{
"bundle_identifiers": [
"^com\\.googlecode\\.iterm2$"
],
"type": "frontmost_application_unless"
}
],
"from": {
"key_code": "k",
"modifiers": { "mandatory": ["control"] }
},
"to": [
{ "key_code": "delete_forward" },
{
"set_variable": {
"name": "ctrl_k_count",
"value": 0
}
}
],
"type": "basic"
},
{
"conditions": [
{
"name": "ctrl_k_count",
"type": "variable_if",
"value": 0
},
{
"bundle_identifiers": [
"^com\\.googlecode\\.iterm2$"
],
"type": "frontmost_application_unless"
}
],
"from": {
"key_code": "k",
"modifiers": { "mandatory": ["control"] }
},
"to": [
{
"key_code": "right_arrow",
"modifiers": ["left_command", "left_shift"]
},
{
"key_code": "x",
"modifiers": ["left_command"]
},
{
"set_variable": {
"name": "ctrl_k_count",
"value": 1
}
}
],
"to_delayed_action": {
"to_if_canceled": [
{
"set_variable": {
"name": "ctrl_k_count",
"value": 0
}
}
],
"to_if_invoked": [
{
"set_variable": {
"name": "ctrl_k_count",
"value": 0
}
}
]
},
"type": "basic"
}
]
}
背景
Mac では、 ctrl-k を押すと行末までカットしてくれる Emacs 風の機能がある。
昔から使っていて便利なのだが、カットしたものを ctrl-y で他のところに貼り付けようと思うと不便に感じることがあった。
というのも、普段クリップボード履歴は Raycast で管理しているが、 ctrl-k ではキルリングという別の領域に入ってしまうためだ。
そこで、Karabiner Elements を用いて、 ctrl-k を「行末まで選択(cmd-shift-→)してカット(cmd-x)」に割り当てることにした。
試行錯誤の結果
ChatGPT くんに聞いて、最初からある程度動くものは出来た。
最初のバージョンは以下の通りである。
mac で ctrl-k をした場合行末までを削除しますが、かわりに行末まで削除しつつクリップボードにコピーする方法はある?
Karabiner-Elements で出来る場合、その設定ファイルを出力して
{
"title": "Ctrl-K to Copy and Delete Until End of Line",
"rules": [
{
"description": "Make Ctrl-K copy and delete until end of line",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "k",
"modifiers": {
"mandatory": ["control"]
}
},
"to": [
{ "key_code": "right_arrow", "modifiers": ["right_command", "right_shift"] },
{ "key_code": "c", "modifiers": ["right_command"] },
{ "key_code": "delete_or_backspace" }
]
}
]
}
]
}
最近はこういうのも ChatGPT が書いてくれて便利ですね。
ただ、これで運用している中でいくつか不満が生じた。
- iterm2 では cmd-shift-→ がタブ移動に割り当てられているので、不要
- 自分は何行か連続で削除したいとき、 ctrl-k を連続で打っていたが、もとのままだと連続して入力してもその行が消えることはない(deleteにならない)
それで改修したのが冒頭の通りである。
- iterm2 を除外
- 2回連続でctrl-kをした際には delete_forward を入力する
という対応をしている。
微妙な箇所
Mac では日本語IMEを使っている際に ctrl-k を入力するとカタカナになるが、Karabiner Elements では IME 変換セッションの途中であることを判断できないっぽいので、この場合にも上記の設定が有効になってしまいカタカナ変換が出来ない。
Web ブラウザでいう isComposing のようなものが使えれば便利なのだが……。
ということで一旦 issue を作った。
(ちなみにこの issue も ChatGPT に頼んで日本語を英語に変換してもらった。割と自然な感じになっておりすごい。助かる。)
なお、しかたないので一旦 ATOK の設定側でカタカナ変換を ctrl-: に変更した。いつか慣れることを期待して。
Discussion