Skip to content

Instantly share code, notes, and snippets.

@frobware
Last active January 10, 2025 19:04
Show Gist options
  • Save frobware/e6e019ed516b9678f324459d02ea2707 to your computer and use it in GitHub Desktop.
Save frobware/e6e019ed516b9678f324459d02ea2707 to your computer and use it in GitHub Desktop.
ghostty-customize-menu-shortcuts
#!/usr/bin/env bash
# Reset and customise menu bar shortcuts for the Ghostty macOS app.
# Restart the app to apply the new bindings.
set -euo pipefail
# Constants for modifier keys.
CMD="@"
CTRL="^"
OPT="~"
SHIFT="$"
get_bundle_id() {
local app_path="$1"
if [[ ! -e "$app_path" ]]; then
echo "Error: Application path '$app_path' does not exist." >&2
exit 1
fi
local bundle_id
bundle_id=$(mdls -raw -name kMDItemCFBundleIdentifier "$app_path" 2>/dev/null)
if [[ -z "$bundle_id" ]]; then
echo "Error: Failed to retrieve bundle ID for '$app_path'." >&2
exit 1
fi
echo "$bundle_id"
}
clear_shortcuts() {
local bundle_id="$1"
if ! defaults write "$bundle_id" NSUserKeyEquivalents "{}"; then
echo "Error: Failed to clear existing shortcuts for bundle ID '$bundle_id'." >&2
exit 1
fi
}
set_shortcuts() {
local bundle_id="$1"
defaults write "$bundle_id" NSUserKeyEquivalents "$(cat <<EOF
{
"Settings..." = "\0";
"Reload Configuration" = "\0";
"Hide Ghostty" = "\0";
"Hide Others" = "\0";
"Quit Ghostty" = "\0";
"New Window" = "\0";
"New Tab" = "\0";
"Split Right" = "\0";
"Split Down" = "\0";
"Close" = "\0";
"Close Tab" = "\0";
"Close Window" = "\0";
"Close All Windows" = "\0";
"Copy" = "${CTRL}${SHIFT}C";
"Paste" = "${CTRL}${SHIFT}V";
"Select All" = "\0";
"Show All Tabs" = "\0";
"Increase Font Size" = "\0";
"Reset Font Size" = "\0";
"Decrease Font Size" = "\0";
"Terminal Inspector" = "\0";
"Minimize" = "\0";
"Zoom Split" = "\0";
"Show Previous Tab" = "${CMD}${SHIFT}[";
"Show Next Tab" = "${CMD}${SHIFT}]";
"Select Previous Split" = "${CMD}[";
"Select Next Split" = "${CMD}]";
"Ghostty Help" = "\0";
}
EOF
)"
}
read_shortcuts() {
local bundle_id="$1"
if ! defaults read "$bundle_id" NSUserKeyEquivalents; then
echo "Error: Failed to read shortcuts for bundle ID '$bundle_id'." >&2
exit 1
fi
}
main() {
local app_path="${1:-/Applications/Ghostty.app}"
if ! command -v mdls &>/dev/null || ! command -v defaults &>/dev/null; then
echo "Error: Required commands 'mdls(1)' or 'defaults(1)' are not available." >&2
exit 1
fi
local bundle_id
bundle_id=$(get_bundle_id "$app_path")
echo "Bundle ID: $bundle_id"
clear_shortcuts "$bundle_id"
echo "Cleared existing shortcuts for bundle ID '$bundle_id'."
set_shortcuts "$bundle_id"
echo "Set new shortcuts for bundle ID '$bundle_id'."
echo "Updated shortcuts for $app_path ($bundle_id) :"
read_shortcuts "$bundle_id"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment