How can I make TMUX be active whenever I start a n

2019-01-30 02:00发布

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?

11条回答
放我归山
2楼-- · 2019-01-30 02:35

My original, accepted answer, stopped working on my Ubuntu14 system after a recent upgrade.

Using either

[ -z "$TMUX" ] && command -v tmux > /dev/null && TERM=xterm-256color && exec tmux

or

[ $TERM != "screen" ] && TERM=xterm-256color && exec tmux

would stop me from being able to even login. I was only able to resolve this due to having a second admin login on the computer.

The fix for me on Ubuntu (and in osx too) was to change my terminal program to actually run tmux instead, i.e.

enter image description here

I still have

[ `uname -s` != Linux ] && exec tmux

as my last .bashrc line but that his only for my Mac OSX systems now.

查看更多
Anthone
3楼-- · 2019-01-30 02:41

If you want to have a single tmux session, put the following in your ~/.bashrc for bash or ~/.zshrc for zsh:

tmux attach &> /dev/null

if [[ ! $TERM =~ screen ]]; then
    exec tmux
fi

The tmux attach line is to make sure if there is a session it attaches to and if there was no session you will not get the warning about "no session".

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-30 02:43

I just made it a keyboard shortcut (in Linux Mint not Ubuntu; so I'm not sure if it is this easy)...


custom shortcut for tmux terminal


It might be hard to see, but the custom shortcut is gnome-terminal --window --maximize -e tmux. This starts a new gnome-terminal window maximized and then executes tmux for you.

I additionally have another custom shortcut that starts a "normal" gnome-terminal maximized (it's the same without the -e tmux).

I feel this is the best way because you can start whatever terminal whatever way you want and is the most customizable way.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-30 02:44

To enable tmux for login and ssh sessions, you can add this to the end of your .bashrc:

# enable tmux at login
PNAME="$(ps -o comm= $PPID)";
if [ $PNAME == "login" ] || [ $PNAME == "sshd" ] ; then
  exec tmux
fi

This script looks for the parent process of the bash shell. If bash was started from logging in or from ssh, it will execute tmux. If you want this to work with a GUI terminal, you can add that in there as well. For example, if you want to start tmux automatically when you start Ubuntu's standard gnome-terminal, you would use this:

PNAME="$(ps -o comm= $PPID)";
if [ $PNAME == "login" ] || [ $PNAME == "sshd" ] || [ $PNAME == "gnome-terminal" ] ; then
  exec tmux
fi

I've tested the above on Live Ubuntu Desktop and I was able to log in afterwards. This should not break the GUI login unless it invokes the login command to log in. I am not aware of a linux GUI that does this.

查看更多
地球回转人心会变
6楼-- · 2019-01-30 02:45

Append following line of code to the end of .bashrc,

[[ $TERM != "screen" ]] && exec tmux
查看更多
\"骚年 ilove
7楼-- · 2019-01-30 02:46

A one-liner that also makes sure the terminal type is set correctly for 256 colors:

[ -z "$TMUX" ] && export TERM=xterm-256color && exec tmux
查看更多
登录 后发表回答