Skip to content

Instantly share code, notes, and snippets.

@a-c-m
Last active April 13, 2026 10:49
Show Gist options
  • Select an option

  • Save a-c-m/42f0d69e7a891cdfd38035265c089df5 to your computer and use it in GitHub Desktop.

Select an option

Save a-c-m/42f0d69e7a891cdfd38035265c089df5 to your computer and use it in GitHub Desktop.
Home/End remap for OSX (like it should be)
#!/bin/bash
# =============================================================================
# fix-home-end-keys.sh
#
# Fixes Home/End key behaviour on macOS so they jump to the start/end of the
# current line, rather than the top/bottom of the document (page up/down).
#
# macOS supports a system-wide key binding file that most native (Cocoa) apps
# respect. This script safely adds the required bindings to that file without
# clobbering any existing configuration.
#
# Bindings added:
# Home → move to start of line
# End → move to end of line
# Shift+Home → select to start of line
# Shift+End → select to end of line
#
# Safe to run multiple times — skips any bindings already present.
#
# Note: Electron apps (VS Code, Slack, etc.) manage their own keybindings and
# are unaffected by this file. Configure those apps separately.
#
# After running: log out and back in (or restart) for changes to take effect.
# =============================================================================
KEYBINDINGS_FILE="$HOME/Library/KeyBindings/DefaultKeyBinding.dict"
# Bindings to add (compatible with bash 3 which ships with macOS)
KEYS=(
'"\UF729"'
'"\UF72B"'
'"$\UF729"'
'"$\UF72B"'
)
VALUES=(
"moveToBeginningOfLine:"
"moveToEndOfLine:"
"moveToBeginningOfLineAndModifySelection:"
"moveToEndOfLineAndModifySelection:"
)
echo "=== fix-home-end-keys ==="
echo ""
echo "Configures Home/End keys to move to start/end of line in macOS native apps."
echo ""
# Ensure the KeyBindings directory exists
mkdir -p "$HOME/Library/KeyBindings"
# Create the bindings file if it doesn't exist yet
if [ ! -f "$KEYBINDINGS_FILE" ]; then
echo "No existing keybindings file found — creating one."
printf "{\n}\n" > "$KEYBINDINGS_FILE"
else
echo "Found existing keybindings file — will merge safely."
fi
echo ""
# Add each binding only if it isn't already present
for i in "${!KEYS[@]}"; do
KEY="${KEYS[$i]}"
VALUE="${VALUES[$i]}"
if grep -qF "$KEY" "$KEYBINDINGS_FILE"; then
echo " [skip] $KEY already configured"
else
ESCAPED_KEY=$(printf '%s' "$KEY" | sed 's/\\/\\\\/g')
sed -i '' "s/^}/ $ESCAPED_KEY = $VALUE;\\n}/" "$KEYBINDINGS_FILE"
echo " [added] $KEY = $VALUE;"
fi
done
echo ""
echo "Done. Log out and back in for changes to take effect."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment