From a34a00eae6eb6e8b35cbc3bd0d2c8f4a34fb20c6 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Tue, 27 May 2025 18:07:13 +0200 Subject: [PATCH] i3: Fix opening kitty in the current working dir Since I run kitty with --single-instance, xcwd does not work for it. Replace it with a short script that retrieves the CWD for the focused window through the remote-control ls command. This changes the current behaviour slightly, since now only terminals are used and not any X window (which I rarely used or needed) and that the working dir of the shell running in kitty will be used and not of any deeper children. I'm still unsure if I want the latter back, but will figure this out in the future. This is an addendum to 01392948311c ("Use kitty instead of st as terminal"). --- .config/i3/config | 12 +++++++++--- .config/kitty/get_cwd.sh | 23 +++++++++++++++++++++++ .local/bin/kitty-cwd | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100755 .config/kitty/get_cwd.sh create mode 120000 .local/bin/kitty-cwd diff --git a/.config/i3/config b/.config/i3/config index 0fca47c..042eaad 100644 --- a/.config/i3/config +++ b/.config/i3/config @@ -38,8 +38,14 @@ set $TERM_CMD_FLAG # For this to work best, launch one hidden "daemon" instance at startup so that # the kitty process is always running, even when no OS windows exists. # +# Additionally allow remote_control over a socket, so that kitty-cwd works. +# # [^1]: kitty(1) -exec --no-startup-id $TERMINAL --start-as hidden --detach +exec --no-startup-id $TERMINAL \ + --start-as hidden \ + --detach \ + -o allow_remote_control=socket-only \ + --listen-on unix:/tmp/mykitty # Multi monitor support exec_always --no-startup-id ~/.config/i3/monitor-setup.sh & @@ -47,7 +53,7 @@ exec_always --no-startup-id ~/.config/i3/monitor-setup.sh & # Keybindins ################################################################### # Terminal -bindsym $mod+Return exec $TERMINAL $TERM_DIR_FLAG "$(xcwd)" +bindsym $mod+Return exec $TERMINAL $TERM_DIR_FLAG "$(kitty-cwd)" bindsym $mod+Shift+Return exec $TERMINAL # Program launcher @@ -231,7 +237,7 @@ bindsym $mod+comma exec \ i3-msg '[class="^$TERM_CLASS$" title="^scratchpad-terminal$"] \ scratchpad show' \ || $TERMINAL $TERM_TRANSPARENCY_FLAG$TERM_SCRATCHPAD_TRANS \ - $TERM_DIR_FLAG "$(xcwd)" \ + $TERM_DIR_FLAG "$(kitty-cwd)" \ $TERM_TITLE_FLAG scratchpad-terminal for_window [class="^TelegramDesktop$"] \ diff --git a/.config/kitty/get_cwd.sh b/.config/kitty/get_cwd.sh new file mode 100755 index 0000000..63cdd6b --- /dev/null +++ b/.config/kitty/get_cwd.sh @@ -0,0 +1,23 @@ +#!/bin/sh +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Julian Prein +# +# Print the current working directory of the focused kitty window. Returns 4 if +# none exist or is focused. + +# NOTE: the backticks are used for hacky line-continuation, taken from +# https://stackoverflow.com/a/7729087/2092762c9 +kitten @ --to unix:/tmp/mykitty ls \ + | jq -er ".[]` + ` | select(.is_focused).tabs.[]` + ` | select(.is_focused).windows.[]` + ` | select(.is_focused).cwd" + +# An alternative version that uses recursive descent to find focused objects +# that also have a `.cwd` key: +# +# | jq -er "..` +# ` | objects` +# ` | select(.is_focused)` +# ` | to_entries.[]` +# ` | select(.key == \"cwd\").value" diff --git a/.local/bin/kitty-cwd b/.local/bin/kitty-cwd new file mode 120000 index 0000000..22774ae --- /dev/null +++ b/.local/bin/kitty-cwd @@ -0,0 +1 @@ +/home/user/projs/dotfiles/.config/kitty/get_cwd.sh \ No newline at end of file