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 0139294831 ("Use kitty instead of st as
terminal").
This commit is contained in:
2025-05-27 18:07:13 +02:00
parent 0c17ae8280
commit a34a00eae6
3 changed files with 33 additions and 3 deletions

View File

@@ -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$"] \

23
.config/kitty/get_cwd.sh Executable file
View File

@@ -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"

1
.local/bin/kitty-cwd Symbolic link
View File

@@ -0,0 +1 @@
/home/user/projs/dotfiles/.config/kitty/get_cwd.sh