zellij icon indicating copy to clipboard operation
zellij copied to clipboard

Allow attaching without providing session name when multiple sessions exist

Open zolrath opened this issue 2 years ago • 11 comments
trafficstars

Currently if you run zellij attach with a single session, it will successfully attach, but if there are multiple sessions active the command will fail and list the sessions instead.

My initial expectation is that it would attach to the session I most recently exited, allowing me to hop in and out of a session easily but still swap to other sessions at will.

I'd expect this to be the default but it could be added as an --recent option or something of that nature if needed.

zolrath avatar Jan 16 '23 10:01 zolrath

A solution exists already. In integrations you can see a snippest that uses fzf to choose your session. With this intergration, you will be literally one keystroke away from entering your session, which is definitely faster than your proposed --recent 8 keystrokes flag solution. You can customize it further to your taste and default behaviour expectations.

thisismygitrepo avatar Jan 17 '23 02:01 thisismygitrepo

Those are great for user facing commands but if I set a my shell to auto start zellij, also from integrations with

set ZELLIJ_AUTO_ATTACH true
set ZELLIJ_AUTO_EXIT true
eval (zellij setup --generate-auto-start fish | string collect)

my terminal will fail to launch if I have more than one zellij session active as the attach command fails.

zolrath avatar Jan 17 '23 05:01 zolrath

Yes I see.

  • Once again, if you insist on using this integration in particular, then one must attend to your request about how default behaviour should be.

  • But if you are looking for a quick solution, I think you can call the other integration that I mentioned, and remove the fzf from it, and write simple if statement to implement your logic of defaulting to most recent session if sessions exist. The modified script sould be sourced or included explicitly in your .bashrc.

  • An even quicker solution it to put this one-liner zellij attach default_sess_name -c, which creates it if not available and attach if available, but it differ from your request in that it specifies the name.

thisismygitrepo avatar Jan 17 '23 05:01 thisismygitrepo

BTW, the integrations that you mentioned have different behaviour from your issue discription. The auto-start integration keeps creating new sessions whenever I login (I use bashrc). My pick with it is that it creates too many sessions and at some point your need to start filter through them one by one to see which one to close. This is probably the diametrical opposite of your problem of not attaching.

thisismygitrepo avatar Jan 17 '23 05:01 thisismygitrepo

I'm simply using the auto-attach code that zellij produces for fish, and it fails once you have two sessions. I'm not looking for a quick solution, I'm fine creating my own logic for now but in the long run this shouldn't be able to case your terminal to fail to launch.

With a single session, every new terminal successfully reattaches to the session, as expected.

If I open a second session:

❯ zellij list-sessions
tested-fang (current)
stiff-fall

all new terminals fail immediately.

[process exited with code 15 (0x0000000f)]

The bash script may function differently, if that's the case the bug report here would be that the fish script handles multiple sessions incorrectly, as this setting should not stop your terminal from launching.

zolrath avatar Jan 17 '23 05:01 zolrath

Are you aware that there are shortcuts to attaching a particular session? Example:

$ zellij a
Please specify the session to attach to, either by using the full name or a unique prefix.
The following sessions are active:
better-laborer
mammoth-disease
$ zellij a b # attaches the first session

Also, you can set a custom, fixed session name and use that as your "main" session to attach to by default.

raphCode avatar Jan 18 '23 11:01 raphCode

Also, you can set a custom, fixed session name and use that as your "main" session to attach to by default.

Where would I do that?

sascha-wi avatar Nov 09 '23 15:11 sascha-wi

yeah imo the shell integrations are broken currently. the script generated by zellij setup --generate-auto-start bash should probably also include a default session name.

zeratax avatar Nov 10 '23 15:11 zeratax

An even quicker solution it to put this one-liner zellij attach default_sess_name -c, which creates it if not available and attach if available, but it differ from your request in that it specifies the name.

I'd also like something like this, where it will create a new session when the default is already in use

noahbald avatar Dec 31 '23 11:12 noahbald

I've cooked up the following launch command which might do what we want

zellij a main || zellij -s main || zellij

Which will attempt to attach to main, or create main, or otherwise launch normally

noahbald avatar Dec 31 '23 11:12 noahbald

Something like this would enable user to specify the session name without command chainging: https://github.com/zellij-org/zellij/pull/3233

rsniezek avatar Apr 03 '24 09:04 rsniezek

I've cooked up the following launch command which might do what we want

zellij a main || zellij -s main || zellij

Which will attempt to attach to main, or create main, or otherwise launch normally

I have alias engage='zellij attach --create main' in my .zshrc for a similar purpose.

mlambie avatar May 28 '24 01:05 mlambie