Session management: 1 session per desktop on bspwm

Hi everyone! Here’s a simple session management setup I came up with a few weeks ago that I wanted to share.
I use the following as a kak wrapper:

desktop="$(bspc query -D -d 2>/dev/null)"

# bspc result was empty, so most likely not using bspwm 
[ -z "$desktop" ] && exec kak "$@"

kak -clear

# if session with desktop id is found, connect to it. otherwise create it
if kak -l | grep -q "^${desktop}$"; then
    exec kak -c "$desktop" "$@"
else
    exec kak -s "$desktop" "$@"
fi

What it does is take the focused bspwm desktop ID and use it as a session ID for Kakoune. Should be easy to port to other WMs too.
I find this to be a nice middle ground between “a single global session for all projects” and “only one session per project” and I find it pretty intuitive.

For each project I usually have all the buffers open together in a single desktop (disclaimer: I haven’t worked in “big” projects yet so I’m not sure how good this works with those).
Sometimes I’m not even working on a “project” and just want to copy code between two files, so opening both on the same desktop does the trick.

It does have the following issues though:

  • (Edit: I fixed my wrapper above to clear dead sessions with kak -clear.)
    Closing the terminal window (i.e. “alt f4” instead of :q) results in the session becoming a dead session, and no new Kakoune clients will open on that desktop. This can be worked around by deleting the dead session socket before creating a new one.
  • Moving a terminal from one desktop to another messes up the whole “all Kakoune clients in this desktop belong to the same session” idea.

I’d like to hear your thoughts on this setup! If you have other ways to manage your sessions/projects please share them too!

This is my first time posting on here, so let me know if I messed anything up :

2 Likes

I personally use kak-shell from connect.kak. All my work usually happens in the same workspace, one at a time. I just close the windows when I’m done and connect to the session again when I want to resume.

I may have found what I was missing from a long time: programs with daemon capabilities, so they are not attached to the environment you ran them. I can not use tmux in a desktop environment, but can when I ssh and reattach my work thanks to the program socket.

I started using pueue for long running commands where I do not want a window for, such as web servers or services.


For your specific issue, what you miss is the -d flag for daemon to keep running the session when quitting. You can use the kill command inside Kakoune to kill it.

2 Likes

I used to do that too, until I realized my muscle memory just wouldn’t get used to it (I kept opening new terminals through sxhkd instead of Kakoune/connect.kak) so I switched to my wrapper setup instead. I might give connect.kak a try again someday though!

I haven’t needed a daemonized workflow yet, as I only edit on one machine. I find sessions being automatically killed when closing the last buffer a lot more intuitive.
I can also see myself forgetting that I have a project open in another session in the background, so having a visual indication that a project is open (desktop is occupied) is great, at least for me.

pueue looks like a really nice tool though! What I used to do was just open a tmux session, run the command I wanted, and detach. But this looks so much better, thanks!

You can add a rename session command to your kakrc to rename the sessions to your desktop.

This is a nice and clean setup, it feels more obvious than what Vim or Emacs offer. The server exits when the last client is closed, which is usually what you want on the desktop; in the past I have used a dedicated daemon process but manual :kill is unintuitive.

I’m using essentially the same script, but with one session per project.
I have used fzf.kak to switch between projects in the past, but now I just launch a new client with the wrapper script. I want to add support for CDPATH, which is a really nifty feature in shells (also supported by Vim), then switching projects with cd becomes easy.

Sometimes I want to run multiple :make jobs in parallel, so I just open another session by running kak instead of the wrapper script. I might extend make.kak so I can have multiple make buffers, but creating a new session is not too bad.

I wonder why. I thought the process would get a SIGHUP in this case.
However, when I send that to a kak server, it exits cleanly.
When I send it to a client process, it crashes, but also doesn’t leave a dead session. It must be something else.

I 've recently learned that quotes are not necessary here, it makes sense when you think about it :wink:

1 Like