From 008f3a2212d8b1f7e8560d084308ffea896bb61c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 19 Jul 2026 09:59:13 -0700 Subject: [PATCH] Fix Kitty terminal cwd detection --- bin/omarchy-cmd-terminal-cwd | 24 +++++++++++++++--------- config/kitty/kitty.conf | 1 + migrations/1784479832.sh | 8 ++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 migrations/1784479832.sh diff --git a/bin/omarchy-cmd-terminal-cwd b/bin/omarchy-cmd-terminal-cwd index 88da9a69..afb7d622 100755 --- a/bin/omarchy-cmd-terminal-cwd +++ b/bin/omarchy-cmd-terminal-cwd @@ -4,18 +4,24 @@ # omarchy:hidden=true terminal_pid=$(hyprctl activewindow | awk '/pid:/ {print $2}') -shell_pid=$(pgrep -P "$terminal_pid" | tail -n1) +kitty_socket="$XDG_RUNTIME_DIR/omarchy-kitty-$terminal_pid" +cwd="" -if [[ -n $shell_pid ]]; then - cwd=$(readlink -f "/proc/$shell_pid/cwd" 2>/dev/null) - shell=$(readlink -f "/proc/$shell_pid/exe" 2>/dev/null) +if [[ -S $kitty_socket ]]; then + cwd=$(kitten @ --to "unix:$kitty_socket" ls --match "state:focused" 2>/dev/null | + jq -r '.[].tabs[].windows[].cwd // empty') +else + shell_pid=$(pgrep -P "$terminal_pid" | tail -n1) - # Check if $shell is a valid shell and $cwd is a directory. - if grep -qs "$shell" /etc/shells && [[ -d $cwd ]]; then - echo "$cwd" - else - echo "$HOME" + if [[ -n $shell_pid ]]; then + cwd=$(readlink -f "/proc/$shell_pid/cwd" 2>/dev/null) + shell=$(readlink -f "/proc/$shell_pid/exe" 2>/dev/null) + grep -Fqsx "$shell" /etc/shells || cwd="" fi +fi + +if [[ -d $cwd ]]; then + echo "$cwd" else echo "$HOME" fi diff --git a/config/kitty/kitty.conf b/config/kitty/kitty.conf index 2934c57f..7493b9e2 100644 --- a/config/kitty/kitty.conf +++ b/config/kitty/kitty.conf @@ -20,6 +20,7 @@ map alt+shift+enter send_text all \e[13;4u # Allow remote access allow_remote_control yes +listen_on unix:${XDG_RUNTIME_DIR}/omarchy-kitty-{kitty_pid} # Aesthetics cursor_shape block diff --git a/migrations/1784479832.sh b/migrations/1784479832.sh new file mode 100644 index 00000000..fd96a808 --- /dev/null +++ b/migrations/1784479832.sh @@ -0,0 +1,8 @@ +echo "Enable Kitty cwd lookup through a remote control socket" + +kitty_config="$HOME/.config/kitty/kitty.conf" +listen_on='listen_on unix:${XDG_RUNTIME_DIR}/omarchy-kitty-{kitty_pid}' + +if [[ -f $kitty_config ]] && ! grep -qE '^[[:space:]]*listen_on[[:space:]]' "$kitty_config"; then + printf '\n# Allow cwd lookup from global keybindings\n%s\n' "$listen_on" >>"$kitty_config" +fi