Skip to content

Kak daemon helper : 1 session per project

Matt Schick edited this page Aug 7, 2023 · 7 revisions

As stated in the documentation one can run kakoune in daemon mode using the following command:

setsid kak -d -s <session-name> &

in order to list all sessions:

kak -l

and to connect to the socket of an existing session:

kak -c <session-name>

personally I like to create a separate daemon process for each code repository / project I'm working on so I created the following binstub that can be placed in your $PATH (or turned into a shell function). The behaviour is pretty straightforward.

#!/bin/bash 

server_name=$(basename `PWD`)
socket_file=$(kak -l | grep $server_name)

if [[ $socket_file == "" ]]; then        
    # Create new kakoune daemon for current dir
    setsid kak -d -s $server_name &
fi

# and run kakoune (with any arguments passed to the script)
kak -c $server_name $@

Once invoked it looks if there is a existing session for the current directory, if there is none then it will run kakoune in daemon mode with the basename of the current working directory as session name. Then it will connect to this session using kak -c.

If there is an existing session for the current working directory, then it will simple connect to the existing instance.

MacOS specifics

setsid is not bundled with the OS, but a port is available here https://github.com/tzvetkoff/setsid-macosx

Usage example

This bin stub can be useful when used with tmux, like in the following scenario:

  • a new tmux session is started in the foo directory.
  • the bin stub described above is executed, creating a new kak session called foo with a first client attached to it.
  • by using split-window -h -c "#{pane_current_path}" as a tmux command (usually binded to a key), a new pane is created on the right with a shell ready in the same foo directory.
  • executing the bin stub in this new pane will start a new client connected to the same foo session.
Clone this wiki locally