Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make TMUX be active whenever I start a new shell session?

Instead of having to type tmux every time, how could I have tmux always be used for new session windows?

So if I have no terminal windows open and then I open one, how can that first session be in tmux?

Seems like a .bashrc sort of thing perhaps?

like image 396
Michael Durrant Avatar asked Jun 17 '12 04:06

Michael Durrant


People also ask

How do I run tmux automatically?

To configure your terminal to automatically start tmux as default, add the following lines to your ~/. bash_profile shell startup file, just above your aliases section. Save the file and close it. Then close and reopen the terminal to start using tmux by default, every time you open a terminal window.

How do I start tmux by default?

How to always auto-start tmux. Always run tmux automatically when you open a terminal. If there's a tmux session, attach to it, or create a session.

How do I toggle between tmux sessions?

We can switch session from within tmux: PREFIX + ( : Switch the attached client to the previous session. PREFIX + ) : Switch attached client to the next session. PREFIX + s : Select session from a list of sessions for the attached client interactively.


1 Answers

warning this can now 'corrupt' (make it unable to open a terminal window - which is not good!) your Ubuntu logins. Use with extreme caution and make sure you have a second admin account on the computer that you can log into in case you have the same problems I did. See my other answer for more details and a different approach.

Given that warning, the simplest solution can be to append the tmux invocation to the end of your .bashrc, e.g.

alias g="grep" alias ls="ls --color=auto"  # ...other stuff...  if [[ ! $TERM =~ screen ]]; then     exec tmux fi 

Note that the exec means that the bash process which starts when you open the terminal is replaced by tmux, so Ctrl-B D (i.e. disconnect from tmux) actually closes the window, instead of returning to the original bash process, which is probably the behaviour you want?

Also, the if statement is required (it detects if the current bash window is in a tmux process already) otherwise each time you start tmux, the contained bash process will attempt to start its own tmux session, leading to an infinite number of nested tmuxen which can be, err, quite annoying (that said, it looks cool).


However, there is a very small risk this can make bash behave in a way that other programs don't expect, since running bash can possibly cause it to turn into a tmux process, so it might be better to modify how you start your terminal emulator.

I use a small executable shell script ~/bin/terminal (with ~/bin in $PATH, so it is found automatically) that looks a bit like:

#!/bin/sh exec gnome-terminal -e tmux 

(I don't use gnome-terminal, so you might have to remove the exec, I'm not sure.)

Now whenever you run the terminal scipt you have a terminal with tmux. You can add this to your menu/desktop/keyboard shortcuts to replace the default terminal.

(This approach also allows you to more easily customise other things about the terminal emulator later, if you ever desire.)

like image 162
huon Avatar answered Sep 17 '22 13:09

huon