I switched from bash
to fish
shell. I liked it and decided to use it on my servers also. How can I start tmux
automatically on ssh
connection? I followed this instruction for bash but fish
shell is different and this recipe doesn't work without cardinal rewriting.

- 517
3 Answers
I rewrote script. The most tricky part was to disconnect from ssh
by exiting from fish
as exit
inside ./.config/fish/config.fish
didn't work.
It starts tmux
only if parent of the fish
is ssh
.
Here is part of my ./.config/fish/config.fish
file:
if status --is-login
set PPID (echo (ps --pid %self -o ppid --no-headers) | xargs)
if ps --pid $PPID | grep ssh
tmux has-session -t remote; and tmux attach-session -t remote; or tmux new-session -s remote; and kill %self
echo "tmux failed to start; using plain fish shell"
end
end
More readable version can be found in my other question (thanks to ridiculous-fish, author of fish shell): How to use booleans in fish shell?
Byobu, a terminal multiplexer, based on tmux, offers an autostart feature.

- 1,332
- 1
- 12
- 23

- 517
Make a .fish file in .config/fish/conf.d
:
if status --is-interactive
echo server is interactive
if set -q SSH_CONNECTION[1]; and set -q SSH_CLIENT[1]; and set -q SSH_TTY[1]; or pstree -p | grep "sshd.*\($fish_pid\)"; and not set -q TMUX
tmux new -AD -t remote -s remote
end
end
If your local machine also has a tmux session, then you have to use CTRL+B CTRL+B D to detach from remote session. More details here. To know why we are using -AD
in the tmux command, please check here.
P.S. Just to be clear about the accepted answer, normally fish is not set as the login shell. So, after ssh, we run fish
. In that case PPID
is bash
and not ssh
.

- 2,678