MacOS defaults コマンドをマスターする


Posted on Sun, Oct 23, 2016
Tags mac, tips, defaults

背景

更新: 2019/09/16

mac には defaults コマンドという様々な環境設定を行える便利コマンドがあります。 よく挙げられる例としては、隠しファイルを表示したり、マウスの動きを速くするというのがあります。

例えば、新しいMacを使い始めるとき、まず始めに行う作業はこの設定だったりして、結構時間が取られます。 4, 5年に一度くらいの頻度だったら良いのですが、新しい Mac が出るたびに乗り換える方は面倒に感じると思います。

どんな設定が可能か確認する

そんな便利な defaults コマンドですが、調べた限りではAppleの方でドキュメントを用意しておりません。

そこで、defaults read で現在のアプリケーションが設定しているプロパティを確認することができます。

$ defaults read | less

ここからさらに絞り込むことができ、Calendar の設定を確認する場合は、

$ defaults read com.apple.iCal

{
    AccountDisplayOrder =     (
        Birthday,
        Local,
    );
    AllDayAreaHeight = 2;
    CalDefaultCalendar = UseLastSelectedAsDefaultCalendar;
    CalFirstVisibleDate = "2019-10-13 09:45:00 +0000";
    CalendarSidebarShown = 1;
    CalendarSidebarWidth = 165;
    CollapsedTopLevelNodes =     {
        MainWindow =         (
        );
    };
    "Default duration in minutes for new event" = 15;
    DelegatesInSeparateWindows =     {
        iCal =         {
        };
    };
    LastSaveDate = "2019-09-16 05:45:42 +0000";
    NSNavLastRootDirectory = "~/Desktop";
    NSNavLastUserSetHideExtensionButtonState = 1;
    ViewModelMigrated = 1;
    "first minute of day time range" = 0;
    "first shown minute of day" = 0;
    "last calendar view description" = "7-day";
    "last minute of day time range" = 1440;
    lastViewsTimeZone = "Asia/Tokyo";
    "number of hours displayed" = 24;
}

という感じで見ることができます。また、どのようなアプリケーションがあるかは、

$ defaults domains

で確認します。

すでに多くの方々が defaults で設定している例があるため、こちらを参考にすると良いでしょう。

iCalの設定などGUIでは設定できないもの(デフォルトのスケジュールを15分にする、カーソルのリピートを最速にするなど)もあるので、興味が湧いた方はぜひマスターしてみてください。

これらのコマンドには再起動しないと反映されないものもあるため、設定後うまく反映していなさそうでしたら、macを再起動してみてください

おすすめ設定

# tabなどでMacのボタンのフォーカスを変えられるフルコントロールを設定
defaults write NSGlobalDomain AppleKeyboardUIMode -int 3

# キーリピートの高速化
defaults write NSGlobalDomain KeyRepeat -int 2
defaults write NSGlobalDomain InitialKeyRepeat -int 15

# スペルの訂正を無効にする
defaults write -g NSAutomaticSpellingCorrectionEnabled -bool false

# CapsLock を Ctrl に変更する
# get string like : 1452-630-0 for keyboard_id (ref: http://freewing.starfree.jp/software/macos_keyboard_setting_terminal_commandline)
keyboard_id="$(ioreg -c AppleEmbeddedKeyboard -r | grep -Eiw "VendorID|ProductID" | awk '{ print $4 }' | paste -s -d'-\n' -)-0"
defaults -currentHost write -g com.apple.keyboard.modifiermapping.${keyboard_id} -array-add "
<dict>
  <key>HIDKeyboardModifierMappingDst</key>\
  <integer>30064771300</integer>\
  <key>HIDKeyboardModifierMappingSrc</key>\
  <integer>30064771129</integer>\
</dict>
"

# タップしたときに、クリックとする
defaults write -g com.apple.mouse.tapBehavior -int 1
# マウスの速度を速める
defaults write -g com.apple.mouse.scaling 5
# 3本指でmission control & expose
defaults write com.apple.dock showMissionControlGestureEnabled -bool true
defaults write com.apple.dock showAppExposeGestureEnabled -bool true
defaults write com.apple.dock showDesktopGestureEnabled -bool true
defaults write com.apple.dock showLaunchpadGestureEnabled -bool true


# show hidden files in finder
defaults write com.apple.finder AppleShowAllFiles YES
# Automatically hide or show the Dock
defaults write com.apple.dock autohide -bool true
# Wipe all app icons from the Dock
defaults write com.apple.dock persistent-apps -array
# Magnificate the Dock (Dock の拡大機能を入にする)
defaults write com.apple.dock magnification -bool true

# 上記設定後は、Finder と Dock を再起動する
killall Finder
killall Dock

#========================== Safari Settings ==========================
# Enable the `Develop` menu and the `Web Inspector`
defaults write com.apple.Safari com.apple.Safari.ContentPageGroupIdentifier.WebKit2DeveloperExtrasEnabled -bool true
defaults write com.apple.Safari IncludeDevelopMenu -bool true
defaults write com.apple.Safari WebKitDeveloperExtrasEnabledPreferenceKey -bool true

#========================== iCal Settings ==========================
# default duration to 15m
defaults write com.apple.iCal "Default duration in minutes for new event" 15
# 24 hour view
defaults write com.apple.iCal "number of hours displayed" 24

#========================== TextEdit Settings ==========================
# make default to plain text
defaults write com.apple.TextEdit RichText -int 0