zellij-org / zellij

A terminal workspace with batteries included
https://zellij.dev
MIT License
19.38k stars 610 forks source link

`zellij attach` opens on top of current session #3411

Open sonofviking opened 3 weeks ago

sonofviking commented 3 weeks ago

Issue description

I am trying to write a script that opens zellij inside of a certain project, setting the name of the session to that project (folder). Whenever I try to run zellij attach inside of another zellij session, it opens that attached within the session instead of replacing it.

To go back to the original session I'd have to kill the attached session as attaching to the original would glitch out zellij

Minimal reproduction

zellij attach --create-background "session-to-attach" # Create a session in the background
zellij -s "current" # open a named zellij session

# From within 'current'
zellij attach session-to-attach

This is what should happen to the status-bar (I have the compact-bar configured).

image

To show what happens when trying to reattach to the 'current' session.

zellij attach current

Note that this is the entire vertical of my monitor, rendering the zellij session useless.

image

Other relevant information

zellij version: 0.40.1

The session I was trying to write (akin to ThePrimeagen's sessionizer script)

#!/usr/bin/env bash

if [[ $# -eq 1 ]]; then
    selected=$1
else
    selected=$(find ~/Projects $XDG_CONFIG_HOME ~/.local/bin -mindepth 1 -maxdepth 1 -type d | fzf)
fi

if [[ -z $selected ]]; then
    exit 0
fi

selected_name=$(basename "$selected" | tr . _)
zellij_running=$(pgrep zellij)

if [[ -z $ZELLIJ ]] && [[ -z $zellij_running ]]; then
    echo "name: $selected_name, selected: $selected"
    zellij attach $selected_name --create options --default-cwd $selected
    exit 0
fi

is_already_session=$(zellij list-sessions --short --no-formatting | rg $selected_name)
current_session=$(zellij list-sessions -n | rg "(current)" | sed 's/\s.*//')

if [[  $current_session == $selected_name ]]; then
    exit 0
fi

if [[ -z $is_already_session ]]; then
    zellij attach --create-background $selected_name options --default-cwd $selected
fi

#zellij -s $selected_name run -- zellij kill-session $current_session

# Attach does not seem to work the way i'd expect it to, should probably open issue on zellij github about this
zellij attach $selected_name
Peeeaje commented 1 week ago

Zellij doesn't have and doesn't aim to implement switching commands so you need to implement such plugin. (I remember the maintainer said it is the role of plugin system)

I wrote that for my own. (sessionizer too btw) https://github.com/Peeeaje/zellij-switch-session