Smart reuse session per $PWD (bash alias)

Hi everyone!

I’m using kak since a few weeks and I very like it, I’m a long term vim user, so I still fight against my muscle memory :slight_smile:

Beside the editor usage, I had an alias:

alias gvim='gvim --servername $(basename $PWD) --remote-silent'

That reuses a shared session if the editor is spawned from the same folder.

The option --remote-silent makes the trick and silently connects to a remote server if the name is already taken.

Having a look at Session management: 1 session per desktop on bspwm, I made up an horrendous hack:

alias kak='f(){ kak -clear; name=$(basename $PWD); if kak -l | grep -q -x "$name"; then kak -c "$name" "$@"; else kak -s "$name" "$@"; fi }; f'

Is there a better option to make kak do the same without wrappers?

1 Like

Is there a better option to make kak do the same without wrappers?

I would say no because there is no way to change a client to another session.

I like your wrapper. However you can just define the function and skip the alias. Use command to make the shell call the executable instead of making a recursive call.

function kak() { 
  command kak -clear; 
  name=$(basename "$PWD"); 
  if command kak -l | grep -q -x "$name"; then 
    command kak -c "$name" "$@"; 
  else 
    command kak -s "$name" "$@"; 
  fi 
}
2 Likes