Kak session manager snippet

Thought I would share some snippets here about how I manage my kakoune sessions, in case it’s useful for anyone else.

I prefer to have one session per git repo named after the path to the repository, stripped of non-alpha-numerical characters. If I launch kakoune inside a git repo it sees if the session already exists and joins. Otherwise, it creates the session. If I’m not inside a git repo, I’m probably editing a config file or doing something quick so kakoune is started without specifying a session.

Here’s a shell script that can be placed in $PATH:


repo=$(echo $(git rev-parse --show-toplevel 2>/dev/null) | sed 's/[^[:alnum:]]//g')

if [ -n "$repo" ]; then
    if kak -l | grep -q "$repo"; then
        args="-c $repo" # join session
        args="-s $repo" # make session

exec kak $args $@ # launch editor

Here’s a bash function that can be sourced from .bashrc:

function kak-mgr {
    local repo=$(git rev-parse --show-toplevel 2>/dev/null)
    local args='' # empty
    if [[ -n $repo ]]; then
        local session=${repo//[^[:alnum:]]} # strip non-alnum chars
        local regex="\b$session\b"
        if [[ $(/usr/bin/kak -l) =~ $regex ]]; then
            args="-c $session" # join session
            args="-s $session" # make session
    /usr/bin/kak $args $@ # launch editor

Note: if you alias kak='kak-mgr', make sure to specify the full path to the kak binary, so you don’t recursively call the kak-mgr function.

Thoughts and feedback are welcome. For the bash regex, I couldn’t get "^$session\$" to match a full line, so I had to resort to using the word boundry \b tag. Anyone know why?


you can call your function kak and call kakoune inside via command kak this way it is more portable. [[ can be replaced with [ and instead of using bash =~ there’s expr

Thanks for the command suggestion. I’ve been trying to keep the stand-alone shell script as POSIX and portable as possible and the bash function as pure bash as possible.

ah, got it. I just see everything as POSIX first, then as bash, then as zsh :slight_smile:

1 Like

I have a very similar thing here: https://github.com/eraserhd/dotfiles/blob/develop/kak/wrapper/wrapper.sh

findSessionName used to look for the tmux window name, which is how I did it, but I’m trying to move to a single, global session.

It occurred to me that there should be some way to configure this with a strategy name. Maybe an environment variable, or something in ~/.config? In any case, it seems like there should be a way to reuse the bulk of it somehow.

(Also, in your first script, the grep will catch a substring. If you have apache-configs running and you launch in apache directory, it will try to connect to an existing apache.)

I use this script:

It has a distinction between a one-terminal session and a session in which every window is a separate terminal. Also, it handles stdin (in a hacky way).
It connects to the global session by default, unless a project specific session is defined in the ENV of the terminal.

Thanks. These other implementations are interesting to look at.

Worth mentioning this one