iTerm2和tmux集成


4

我想实现以下行为:

当我启动iTerm2时,如果正在运行tmux会话,请连接到它。如果没有运行tmux会话,请创建一个并连接到它。

任何指针非常感谢!


2
请注意,iTerm2现在内置了tmux支持。我不知道如何最好地利用它(因此通过搜索来了解它),但现在事情应该变得非常简单。很高兴看到这个新工作流程的更新。
马特B.

不确定是否将其设置为自动连接,但此处有信息可以帮助您修改@Felix的脚本。
askewchan 2016年

Answers:


4

我使用这个脚本,exec每当我的shell作为登录shell启动时都是如此。你可能想要编辑一下......

#/usr/bin/env zsh

local tmux=$1
local sname
local ssh

[[ -z "$tmux" ]] && tmux=tmux

# export COLORTERM to fix some programs
case "$TERM" in
  *256color)
    export COLORTERM="$TERM" ;;
esac

# build a fancy session name
[[ ! -z "$SSH_CLIENT" ]] && ssh="/$SSH_CLIENT"
sname=$(print -P "%D{%Y-%m-%d-%H-%M-%S}/%n/%y$ssh")

# make sure session 0 exists  
$tmux has-session -t 0 2>/dev/null || $tmux new-session -d -s 0

# create our own mirroring session
$tmux new-session -d -s "$sname" -t 0

# this doesn't return until the session is closed or detached
$tmux attach-session -t "$sname"

# kill the mirroring session if it's not dead already 
$tmux has-session -t "$sname" 2>/dev/null && $tmux kill-session -t "$sname"

# return to prompt outside of tmux if special file is present 
if [[ -f "$TMUX_LEAVE_SHELL_FILE" ]]; then
  rm "$TMUX_LEAVE_SHELL_FILE"
  exec zsh -i
fi

我的.zshrc中的相关部分

# drop into tmux if this is a login shell and we're not in tmux already
if (which tmux >/dev/null) && [[ -o login ]] &&  [[ -z "$TMUX" ]] && [[ -f ~/etc/script/tmux-login ]]; then
  exec zsh "$HOME/etc/script/tmux-login" $(which tmux)
fi

# handy command to detach tmux but keep the shell running (kind-of)
case $TERM in
  screen)
    alias detach="touch $TMUX_LEAVE_SHELL_FILE && tmux detach" ;;
esac
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.