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/prelude.sh"

# overides
PATH=$HOME/.config/kak/plugins/connect.kak/rc/paths/aliases:$HOME/.config/kak/plugins/connect.kak/rc/paths/commands:$PATH:$HOME/.config/kak/plugins/connect.kak/rc/paths/tools
VISUAL=edit
EDITOR=edit

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?

4 Likes

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.

nice!

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`
(https://github.com/mawww/kakoune/issues/3533)
I set $VISUAL=kakwrap and set nnn to open files with visual.

Hi,

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!

Cheers

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…

2 Likes

I’ve written a function related to this in fish, you might be able to adapt it. You don’t need connect for it, it’s just pure fish script:


#!/usr/bin/env fish

function kc --wraps kak --description "Connect to existing kakoune session or create a new one"
  if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1
    or ! git rev-parse --show-toplevel >/dev/null 2>&1

    # If git toplevel can't be found, run a disconnected session
    kak $argv
    return
  end

  set -l dirname (git rev-parse --show-toplevel)
  set -l projectname (basename $dirname | string replace . -)
  set -l session (kak -l | grep $projectname)

  if test -z "$session"
    # Create a new session if there are none
    kak -s $projectname $argv
  else
    # Otherwise connect to existing session
    kak -c $projectname $argv
  end
end

I’ve named it kc and put it in my fish functions folder to make it available everywhere. With that I can run “kc” anywhere in a git project and it will connect to a session for that project if there is one. If there is no existing session it will create one based on the folder name of the project root.

It forwards its arguments to kakoune, so you can just use the command in the same manner as the kak command, and you can still have command completions based on the completions for kak.

3 Likes