问题描述
不必每次都输入 tmux
,我怎么能让 tmux
总是用于新的会话窗口?
Instead of having to type tmux
every time, how could I have tmux
always be used for new session windows?
所以如果我没有打开终端窗口然后我打开一个,第一个会话怎么会在 tmux
中?
So if I have no terminal windows open and then I open one, how can that first session be in tmux
?
好像是 .bashrc
之类的东西?
Seems like a .bashrc
sort of thing perhaps?
推荐答案
warning 这现在可以损坏"(使其无法打开终端窗口 - 这不好!)你的 Ubuntu 登录.使用时要格外小心,并确保您在计算机上有第二个管理员帐户,如果您遇到与我相同的问题,您可以登录该帐户.请参阅我的其他回答以了解更多详细信息和不同的方法.
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.
鉴于该警告,最简单的解决方案是将 tmux
调用附加到 .bashrc
的末尾,例如
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
注意 exec
表示打开终端时启动的 bash 进程被 tmux
replace,所以 Ctrl-B D
(即与tmux断开连接)实际上是关闭了窗口,而不是回到原来的bash进程,这可能是你想要的行为?
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?
此外,if
语句是必需的(它检测当前 bash 窗口是否已经在 tmux 进程中)否则每次启动 tmux 时,所包含的 bash 进程将尝试启动自己的 tmux会话,导致无限数量的嵌套 tmuxen,这可能非常烦人(也就是说,它看起来很酷).
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).
然而,有一个非常小的风险,这会使 bash
以其他程序不期望的方式运行,因为运行 bash 可能会导致它变成一个 tmux 进程,所以它可能会更好地修改您启动终端模拟器的方式.
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.
我使用了一个小的可执行shell脚本~/bin/terminal
(在$PATH
中带有~/bin
,所以它会被自动找到) 看起来有点像:
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
(我不使用 gnome-terminal,因此您可能需要删除 exec
,我不确定.)
(I don't use gnome-terminal, so you might have to remove the exec
, I'm not sure.)
现在,每当您运行 terminal
scipt 时,您都会有一个带有 tmux 的终端.您可以将其添加到菜单/桌面/键盘快捷键中以替换默认终端.
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.)
这篇关于每当我开始一个新的 shell 会话时,如何让 TMUX 处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!