Search code examples
shellexectmuxfish

Safely exec tmux upon login


I would like to execute tmux upon logging into a shell for my user. I am using fish, but I think this question is relevant to any shell. So far, I've accomplished this by following the advice in this question: https://askubuntu.com/questions/560253/automatically-running-tmux-in-fish, specifically, adding the following line to my config.fish:

test $TERM != "screen"; and exec tmux

However, I have one major issue with this approach, and that is if tmux fails to start, perhaps if I've introduced a syntax error in my .tmux.conf file, the shell process immediately exits, booting me out of the session.

Is there a way to automatically run tmux in new shell executions whereby I can:

  • Catch errors and fallback on a "plain" shell execution (i.e. just fish without tmux)
  • Not have to exit a login twice - once to quit tmux then again to quit fish

?


Solution

  • This works for me:

    if status --is-login
        source $HOME/.config/fish/login.fish
        tmux; and exec true
    end
    

    Obviously you may or may not have a login.fish file. I like to keep my config.fish lean by putting code that might not be needed for the current session in separate files so I've also got a interactive.fish script