One kakoune session per project with direnv and connect.kak?

Hi there! I have been lurking for a while here and thought it was about time to post a question.

I recently found the excellent tool direnv which sets environment variables in your shell based on which directory you are in.

I thought it would be great if there was a way to integrate this with connect.kak so that when you launch a terminal and cd into your project directory it would be as though you had launched the terminal from kakoune with connect.kak’s connect-terminal.

I have tried the following .envrc (which is just blindly copy and pasting bits of the env setup performed by connect.kak) with no success

# kak env
export KAKOUNE_SESSION=dotfiles
export KAKOUNE_CLIENT=${kak_client:client0}
export KAKOUNE_PRELUDE="$HOME/.config/kak/plugins/prelude.kak/rc/"

# overides

If you then run kak -s dotfiles, launch a separate terminal, cd into dotfiles and run edit file no errors are produced but the file is not opened in kak.

I was wondering if anyone had tried anything similar or has any suggestions for how I might be able to get this working?


Where has this been all my life?

On your actual point, this is a really interesting sort of dir level environment sync idea… I haven’t done it but now I want to, I sort of invert it (launch all my tmux panes with terminals with connect). But if I accidentally close kak when having the shell open, I then have to manually go fix it, not fun. This sounds like a better approach.


I’m planning to do something similar.

I want to write a fish shell functions which opens a new kitty tab / OS window with a ‘project session’.

That would consist of Kak, one to two terminals (one maybe called kak_replto allow repl function) and nnn.

I looked at ‘conect’ and think it’s amazing but for my usecase I might not need it after all.

The Kak session will be called after the current project’s directory, the client will be named ‘master’.
All shells in the current tab will have a env variable with the project’s dir.

I will write another fish function (kakwrap') which opens files in the 'master' client if the env variable exists: echo “evaluate-commands -client master edit $filename” | kak -p $kakoune_session_name`
I set $VISUAL=kakwrap and set nnn to open files with visual.


I used another approach for a while, I set my $EDITOR to a script that:

  • checks if there is a current git rev-parse --show-toplevel output
  • computes an unique session name based on a hash of the absolute path of the git toplevel
  • starts the session in background if necessary
  • edits the file given as argument in that session.

…this worked quite well and I even extended it to fallback to a system-managed default session if not in a git project.

But the direnv solution looks great!


1 Like

Is that published? :slight_smile:

There you go!

Please note that there are a few things that might bother you at first :

  • a default session named main must always be running (I use Systemd to ensure that)
  • if the editor is started outside a git directory and without arguments, then a change-directory to $PWD command is performed : I found it really upsetting that when you open the editor without arguments and connect the the default session you’re not in the directory your shell was in

Sorry if the code is unclear, this was not supposed to leave my messy $HOME…

1 Like