4

I've found using iTerm2 and tmux -CC to be quite helpful tying the existing iTerm UI with tmux windows & panes.

I'm trying to figure out how to automatically attach to an existing session at iTerm launch (with the Send text at start: profile setting) or create a new one if it doesn't exist.

I've seen several people suggest something like the following:

tmux attach -t base || tmux new -s base

but adding the -CC flag results in some strange behavior / doesn't work. I get the following error on first launch:

A tmux protocol error occurred.
Reason: %begin with empty command queue

and the shell (client?) has the following output:

> tmux -CC attach -t base || tmux -CC new -s base                     
** tmux mode started **

Command Menu
----------------------------
esc    Detach cleanly.
  X    Force-quit tmux mode.
  L    Toggle logging.
  C    Run tmux command.
Detached
** tmux mode started **

Command Menu
----------------------------
esc    Detach cleanly.
  X    Force-quit tmux mode.
  L    Toggle logging.
  C    Run tmux command.
Detached
%output %0 \015\015\033M\033[0m\033[23m\033[24m\033[J\033[36m@\033[32mgloesch\033[m\017 \033[1m\033[34m~\033[m\017 \015\012> \033[K\033[68C\033[31m<system>\033[m\017 \033[m\017\033[77D
%begin 1444229906 14 1
%end 1444229906 14 1
%exit

A second window is created during this process but then ends up being closed once everything finishes.

2

Instead of using Send text at start:, I'd suggest passing the command as an argument to ssh. In my case, the following works (with the latest iTerm2 beta though, so you might want to try and upgrade to that if it doesn't work for you):

ssh -t <user>@<remote> "tmux -CC new -A -s foo"

You can wrap this into a function and put that in your shell init file (.zshrc, .bashrc et al.) for easy invocation:

$ cat ~/.zshrc
function cch {
  ssh -t $@ "tmux -CC new -A -s foo"
}
$ cch <user>@<remote>

See the tmux man page for details on the -A switch to the new-session command, and also this thread on iTerm's GitLab, where George Nachman suggested this elegant solution using -A which integrates well with iTerm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.