【macOS】キーマップとキーボードショートカットの変更設定

最近では、macOS ユーザーは、いくつもの重なって設定されたショートカット機能を「ほぼ無意識」にキーボードから操作することになっています。

当然、衝突したり、意図しない機能が発動されることも多くなります。

まずは、設定箇所をはっきり把握しておきたいです。

 

■ OS 設定 キーボードショートカット「修飾キー」

修飾キー (Modifier Keys) の、それぞれの機能を入れ替えたり、無効化することができます。


System Settings

  ↓

Keyboard

  ↓

Keyboard Shortcuts…

  ↓

Modifier Keys

👉 Macで修飾キーの動作を変更する - Apple サポート (日本) hatena-bookmark

 

■ キーマップ変更アプリ

修飾キー を含めたいろいろなショートカットを作成できます。⌘(command)⇧(shift) キーの左右個別の機能振り分けやカーソルキー、Fn キーなど細かく設定できるサードパーティのアプリです。

有名な2つを挙げておきます。ソースコードはともに GitHub で公開されています。無料です。

👉 Karabiner-Elements hatena-bookmark

👉 ⌘英かな hatena-bookmark

私は、⌘英かな を利用して、英数 ↔ かな の切り替えと カーソルキー のショートカット作成で使用しています。

👉 【macOS】IDE で 矢印 (カーソルキー) を使うと キーボード ホームポジション がずれる件 hatena-bookmark

 

■ OS 設定 キーボードショートカット「アプリショートカット」

OSの設定からアプリごとにショートカットを設定できます。


System Settings

  ↓

Keyboard

  ↓

Keyboard Shortcuts…

  ↓

App Shortcuts

  ↓

All Applications


👉 Mac のキーボードショートカット - Apple サポート (日本) hatena-bookmark

ショートカットに振り分ける機能の名称に関しては、スペースや大文字小文字などきちんと入力する必要があります。そこが面倒です。機能 - 名称 の一覧とかどこかにないでしょうかね。

👉 【macOS】キーボードショートカットの変更は言語設定別でイヤだ!! hatena-bookmark

 

■ 各アプリの設定

ブラウザ や エディタ、IDE など、インストールしたアプリ内の設定からもショートカットの設定ができます。

以下、Android Studio のデフォルトのショートカット。


👉 キーボード ショートカット  |  Android デベロッパー  |  Android Developers hatena-bookmark

Android Studio では、設定から、個別にショートカットの変更ができますが、それらの詰め合わせとなったIDEAプラグインをインストールしてまとめて変更することもできます。

👉 IdeaVim - IntelliJ IDEs Plugin | Marketplace hatena-bookmark
👉 VSCode Keymap - IntelliJ IDEs Plugin | Marketplace hatena-bookmark
👉 macOS For All - IntelliJ IDEs Plugin | Marketplace hatena-bookmark
👉 Emacs Keymap - IntelliJ IDEs Plugin | Marketplace hatena-bookmark

以下、Chrome のデフォルトのショートカット。

👉 Chrome のキーボード ショートカット - パソコン - Google Chrome ヘルプ hatena-bookmark

Chrome では、設定 - エクステンション - ショートカット


chrome://extensions/shortcuts

から、個別のアプリに対してのショートカットを変更できますが、これも以下のような Chrome エクステンションをインストールすると設定しやすいです。

👉 AutoControl: Keyboard shortcut, Mouse gesture - Chrome Web Store hatena-bookmark
👉 Shortkeys (Custom Keyboard Shortcuts) - Chrome Web Store hatena-bookmark
👉 Disable keyboard shortcuts - Chrome Web Store hatena-bookmark

 

■ まとめ

不必要にショートカットをカスタムするのはやめたほうが良さげ。

最小限のカスタムで今の段階でメモ。


1. os settings keyboard modifier keys

  caps lock → control


2. ⌘英かな (os app)

  left command → 英数
  right command → かな
  control + p → arrow up
  control + n → arrow down
  control + f → arrow right
  control + b → arrow left


3. os settings keyboard app shortcuts

  command + v → Paste and Match Style
  shift + command + v → Paste

  ※ Notes.app 向けに paste 2つを入れ替える。


4. app settings

  - Android Studio
    Settings… - Keymap
    → macOS のままにする

  - Chrome
    disable shortcut (chrome extension)
      command + p → Do nothing
      command + d → Do nothing
      command + s → Do nothing

    ※ 英かな切り替え直後の意図しない動作を防ぐ。

指がホームポジションから離れることがずっと気になっていました。

キーマップを自由にカスタマイズできる ⌘英かなKarabiner-Elements を使うことで カーソルキーを使った範囲選択が shift + control + p/n/f/b でできるようになったことで IDEA/Android Studio 上でエディタの Keymap を デフォルトである macOS のまま利用できるようになったことがうれしい。

衝突の警告が多少出ているが、追って調整していけば問題ないでしょう。



【Intellij IDEA / AndroidStudio】コードを縦に揃えて、あるカラムでソートできるプラグイン - String Manipulation

どちらがいいか、考えたことありますよね。


int robert_age = 32;
int annalouise_age = 25;
int bob_age = 250;
int dorothy_age = 56;


int robert_age     = 32;
int annalouise_age = 25;
int bob_age        = 250;
int dorothy_age    = 56;

意見が分かれることは私も知ってます。

コーディングは、自分自身の考えを表現する創造的な方法です。表現したアイデアを難しく見せてしまうようなツールであるのなら、インデントではなく、ツール自体を改善すべきでしょう。

👉 私がコーディングで垂直方向にそろえるインデントをとる理由 | POSTD hatena-bookmark

縦位置は揃っていた方が良いが、別に揃っていなくても我慢できなくはありません。むしろ、縦位置なんて諦めた方が総合的には幸せになれると思います。

👉 変数や配列の縦位置を揃えてはいけない!!(ほとんどの場合は) - Qiita hatena-bookmark

Android アプリ開発時 Gradle Version Catalog で TOML を使う場合に、見づらいので、縦に揃えたくなることありますよね?

idmodule でソートしたくなることありますよね?


そんな AndroidStudio (Intellij IDEA) プラグインがあります。

【AndroidStudio】コードを縦に揃えて、あるカラムでソートできるプラグイン - String Manipulation



Separators として、" " (半角スペース) のみを指定するのがおすすめです。

右上の column index を見ながら、id でソートできます。

【AndroidStudio】コードを縦に揃えて、あるカラムでソートできるプラグイン - String Manipulation

実際にファイルに反映するのも良し、見てチェックだけするのも良し。

便利です。他にも色々できます。

👉 String Manipulation - IntelliJ IDEs Plugin | Marketplace hatena-bookmark
👉 krasa/StringManipulation: IntelliJ plugin - https://plugins.jetbrains.com/plugin/2162 hatena-bookmark


CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute)

PyCharm や Intellij IDEA Ultimate の 「Debug」 で見かけました。


/path/to/python3.11 /path/to/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/222.4345.14/IntelliJ IDEA.app.plugins/python/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 59014 --file /path/to/test.py 
-------------------------------------------------------------------------------
pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute)
pydev debugger: The debugger may still function, but it will work slower and may miss breakpoints.
pydev debugger: Related bug: http://bugs.python.org/issue1666807
-------------------------------------------------------------------------------
Connected to pydev debugger (build 222.4345.14)
pydev debugger: Unable to find real location for: <frozen codecs>
pydev debugger: Unable to find real location for: <frozen importlib._bootstrap>
pydev debugger: Unable to find real location for: <frozen importlib._bootstrap_external>
pydev debugger: Unable to find real location for: <frozen zipimport>
pydev debugger: Unable to find real location for: <string>
pydev debugger: Unable to find real location for: <frozen posixpath>
pydev debugger: Unable to find real location for: <frozen _collections_abc>
pydev debugger: Unable to find real location for: <frozen os>
pydev debugger: Unable to find real location for: <frozen abc>
pydev debugger: Unable to find real location for: <__array_function__ internals>
pydev debugger: Unable to find real location for: <frozen genericpath>
pydev debugger: Unable to find real location for: <frozen io>

以下を Edit Configrations から Interpreter options に追加すれば消える。


-Xfrozen_modules=off

CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute)

「Run」 や OS の Termnal から実行すると出ない。

IDE に同梱されてる pydevd が古くて、

Debug するスクリプト(インターラプタ) と pydev の バージョン間の相性がイマイチ

という感じか。

非stable な 3.11.x なども、コマンドのオプションとしてつけると消える。

👉 課題 1666807: Incorrect file path reported by inspect.getabsfile() - Python tracker hatena-bookmark
👉 "CRITICAL WARNING" error debugging Python 3.11 code : PY-56939 hatena-bookmark
👉 fabioz/PyDev.Debugger: Sources for the debugger used in PyDev, PyCharm and VSCode Python hatena-bookmark


Gradle Version Catalog 見づらい TOML インラインテーブル を別フォーマットで表示してみる

すべてインラインテーブルで記述したもののなんか見づらい。

なんかいい方法はないかな、と模索。

Python 3.11 で同梱された tomllib を使って別フォーマットで整形して表示してみます。

👉 How to Use Python 3.11's New TOML Parser, tomllib - The Invent with Python Blog hatena-bookmark

記事にするには大きすぎるので [plugins] だけで。


import tomllib
import pprint
import json
import yaml
import pandas
import tabulate

file = "/path/to/libs.versions.toml"

with open(file, "rb") as fp:
  data = tomllib.load(fp)["plugins"] # only plugins

# raw (dic)
print(data)

# pretty print
pprint.pprint(data)

# json
print(json.dumps(data, indent=2))

# yaml
print(yaml.dump(data))

# pandas + tabulate
df = pandas.DataFrame.from_dict(data, orient='index')
print(tabulate.tabulate(df, headers='keys'))

以下、出力結果。

 

■ raw (dic)


{'android-application': {'id': 'com.android.application', 'version': {'ref': 'agp'}}, 'kotlin-android': {'id': 'org.jetbrains.kotlin.android', 'version': {'ref': 'kotlin'}}, 'kotlin-kapt': {'id': 'org.jetbrains.kotlin.kapt', 'version': {'ref': 'kotlin'}}, 'kotlin-plugin-serialization': {'id': 'org.jetbrains.kotlin.plugin.serialization', 'version': {'ref': 'kotlin'}}, 'hilt': {'id': 'com.google.dagger.hilt.android', 'version': {'ref': 'hilt'}}, 'sqldelight': {'id': 'app.cash.sqldelight', 'version': {'ref': 'sqldelight'}}, 'google-services': {'id': 'com.google.gms.google-services', 'version': {'ref': 'google-services'}}, 'firebase-crashlytics': {'id': 'com.google.firebase.crashlytics', 'version': {'ref': 'firebase-crashlytics'}}, 'ben-manes-versions': {'id': 'com.github.ben-manes.versions', 'version': '0.44.0'}}

 

■ pretty print


{'android-application': {'id': 'com.android.application',
                         'version': {'ref': 'agp'}},
 'ben-manes-versions': {'id': 'com.github.ben-manes.versions',
                        'version': '0.44.0'},
 'firebase-crashlytics': {'id': 'com.google.firebase.crashlytics',
                          'version': {'ref': 'firebase-crashlytics'}},
 'google-services': {'id': 'com.google.gms.google-services',
                     'version': {'ref': 'google-services'}},
 'hilt': {'id': 'com.google.dagger.hilt.android', 'version': {'ref': 'hilt'}},
 'kotlin-android': {'id': 'org.jetbrains.kotlin.android',
                    'version': {'ref': 'kotlin'}},
 'kotlin-kapt': {'id': 'org.jetbrains.kotlin.kapt',
                 'version': {'ref': 'kotlin'}},
 'kotlin-plugin-serialization': {'id': 'org.jetbrains.kotlin.plugin.serialization',
                                 'version': {'ref': 'kotlin'}},
 'sqldelight': {'id': 'app.cash.sqldelight', 'version': {'ref': 'sqldelight'}}}

 

■ json


{
  "android-application": {
    "id": "com.android.application",
    "version": {
      "ref": "agp"
    }
  },
  "kotlin-android": {
    "id": "org.jetbrains.kotlin.android",
    "version": {
      "ref": "kotlin"
    }
  },
  "kotlin-kapt": {
    "id": "org.jetbrains.kotlin.kapt",
    "version": {
      "ref": "kotlin"
    }
  },
  "kotlin-plugin-serialization": {
    "id": "org.jetbrains.kotlin.plugin.serialization",
    "version": {
      "ref": "kotlin"
    }
  },
  "hilt": {
    "id": "com.google.dagger.hilt.android",
    "version": {
      "ref": "hilt"
    }
  },
  "sqldelight": {
    "id": "app.cash.sqldelight",
    "version": {
      "ref": "sqldelight"
    }
  },
  "google-services": {
    "id": "com.google.gms.google-services",
    "version": {
      "ref": "google-services"
    }
  },
  "firebase-crashlytics": {
    "id": "com.google.firebase.crashlytics",
    "version": {
      "ref": "firebase-crashlytics"
    }
  },
  "ben-manes-versions": {
    "id": "com.github.ben-manes.versions",
    "version": "0.44.0"
  }
}

 

■ yaml


android-application:
  id: com.android.application
  version:
    ref: agp
ben-manes-versions:
  id: com.github.ben-manes.versions
  version: 0.44.0
firebase-crashlytics:
  id: com.google.firebase.crashlytics
  version:
    ref: firebase-crashlytics
google-services:
  id: com.google.gms.google-services
  version:
    ref: google-services
hilt:
  id: com.google.dagger.hilt.android
  version:
    ref: hilt
kotlin-android:
  id: org.jetbrains.kotlin.android
  version:
    ref: kotlin
kotlin-kapt:
  id: org.jetbrains.kotlin.kapt
  version:
    ref: kotlin
kotlin-plugin-serialization:
  id: org.jetbrains.kotlin.plugin.serialization
  version:
    ref: kotlin
sqldelight:
  id: app.cash.sqldelight
  version:
    ref: sqldelight

 

■ pandas + tabulate


                             id                                         version
---------------------------  -----------------------------------------  -------------------------------
android-application          com.android.application                    {'ref': 'agp'}
kotlin-android               org.jetbrains.kotlin.android               {'ref': 'kotlin'}
kotlin-kapt                  org.jetbrains.kotlin.kapt                  {'ref': 'kotlin'}
kotlin-plugin-serialization  org.jetbrains.kotlin.plugin.serialization  {'ref': 'kotlin'}
hilt                         com.google.dagger.hilt.android             {'ref': 'hilt'}
sqldelight                   app.cash.sqldelight                        {'ref': 'sqldelight'}
google-services              com.google.gms.google-services             {'ref': 'google-services'}
firebase-crashlytics         com.google.firebase.crashlytics            {'ref': 'firebase-crashlytics'}
ben-manes-versions           com.github.ben-manes.versions              0.44.0

 

■ まとめ

Python って、簡単にいろんなことできるんですね!


Gradle Version Catalog への書き換えツールを作る【python】

素晴らしいツールを公開されています。

👉 takahirom/gradle-version-catalog-converter: Convert `implementation 'androidx.core:core-ktx:1.7.0'` into `androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidxCore" }` hatena-bookmark

結果は以下。

gradle-version-catalog-converter


implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
implementation 'com.google.accompanist:accompanist-swiperefresh:0.26.5-rc'


[versions]
comGoogleAccompanist = "0.17.0"

[libraries]
com-google-accompanist-systemuicontroller = { module = "com.google.accompanist:accompanist-systemuicontroller", version.ref = "comGoogleAccompanist" }
com-google-accompanist-swiperefresh = { module = "com.google.accompanist:accompanist-swiperefresh", version.ref = "comGoogleAccompanist" }

build.gradle
implementation libs.com.google.accompanist.systemuicontroller
implementation libs.com.google.accompanist.swiperefresh

build.gradle.kts
implementation(libs.com.google.accompanist.systemuicontroller)
implementation(libs.com.google.accompanist.swiperefresh)

高機能で便利です。

作った動機はだれもが同感できるでしょう。

ということで、

私も python の勉強がてら雑魚ツールを作ります。

クリップボードにコピーした


implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
implementation 'com.google.accompanist:accompanist-swiperefresh:0.26.5-rc'

を python スクリプト実行後すると


* source
  implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
  implementation 'com.google.accompanist:accompanist-swiperefresh:0.26.5-rc'

* gradle/libs.versions.toml
[libraries]
accompanist-systemuicontroller = { module = "com.google.accompanist:accompanist-systemuicontroller", version = "0.17.0"" }
accompanist-swiperefresh = { module = "com.google.accompanist:accompanist-swiperefresh", version = "0.26.5-rc" }

* build.gradle
implementation libs.accompanist.systemuicontroller
implementation libs.accompanist.swiperefresh

と表示します。

まずは、ただそれだけです。

[versions] を設定するのもだるい implementation 単独バージョンのライブラリ用。

幼稚なスクリプトなので拡張や変更、削除しやすいです。

重複に注意です。

このへんの変換処理は、

きっと、Android Studio プラグインが登場して、

そのあと、Android Studio に取り込まれいく

のだろうと妄想しています。

それまでのつなぎで。

※ このページは gist を更新しながら更新していきます。

※ 追記: こんなのあったんですね!


👉 「⚠ This project uses Gradle Version Catalogs: this tool may not behave as expected.」→ 今現在、Gradle Version Catalog には gradle-versions-plugin が必須では? hatena-bookmark

👉 【Gradle Version Catalog】libs.versions.toml キー名の形式 camelCase vs kebab-case hatena-bookmark