How I can run a command in Tmux when I'm attaching to a session ?

I want attach and immediately run a command.

I read the docs, but found only send keys, which not suits my needs.

  • I wonder what you expect to happen if, for instance, the tmux session you attach to happens to be running vi. Aug 15, 2013 at 8:15
  • It will run only the bash
    – astropanic
    Aug 15, 2013 at 8:25

6 Answers 6


You can attach to a running tmux session and spawn a new window which runs a particular command:

tmux attach \; new-window vim

Note that this does not spawn vim in the pre-exsiting window - there is no facility for doing that, it doesn't really make sense: as @Falcon Momot points out, an existing window could be running anything, the only way to issue commands is "send keys".

  • 4
    Great answer! This must be obvious to most readers, but it took me a while to realize it, so just in case anyone else has the same question: the \; is an escaped semicolon, used here to separate two tmux commands (without escaping the semicolon new-windows would be treated as a new shell command).
    – Joe
    Oct 14, 2014 at 10:49

I was searching for a solution to this exact problem. It can be done using 'set-buffer' and 'paste-buffer' commands

tmux att -t <session-name> \; set-buffer "<command>^M" \; paste-buffer

Here is a complete example :

# let's start with two sessions running bash
tmux new -s theOtherSession \; detach
tmux new -s astropanic \; rename-window main-window \; detach

# attach to the 'astropanic' session, run a directory listing, output
# current datetime, then detach. Note for carriage return (^M) type ^V^M
tmux att -t astropanic \; find-window main-window \; set-buffer "ls;date^M" \; paste-buffer \; detach

# reconnect to check status
tmux att -t astropanic
  • 1
    I did not manage to do carriage return as you said. I had to send the enter key like this: tmux attach -t "$1" \; set-buffer "ls;date" \; paste-buffer \; send-keys C-m. Oct 19, 2017 at 22:53
  • 1
    This should be the accepted answer. Basically you should name a window and make the name stick by adding DISABLE_AUTO_TITLE="true" to your shell profile (this is important), then use this window only to run commands upon connecting. I use it to connect to a ssh host and then get my kerberos ticket at the start of the day. ssh hostname -t "tmux a -t 0 \; select-window -t cmd \; set-buffer \"kinit -f -l 1d -r 30d^M\" \; paste-buffer \;". For convenience, give it a nice name and save it as a script in your path. :) Also note the ^M should be inserted by pressing C-v then Enter in your editor.
    – Gaurav
    Aug 17, 2018 at 17:23

It's not clear to me what kind of command you want to run, a tmux command or a shell/OS command. Here's an example of each:



# give the session a name; makes it easier to reuse code lines

# start a whole new tmux session
tmux new-session -s $_SNAME -d -x 140 -y 35

# can set tmux options
tmux set-option -t $_SNAME default-path /opt/foo/build

# create a new window that's just a shell
tmux new-window -t $_SNAME -n build -d

# create a new window that's running a program
tmux new-window -t $_SNAME -n vim -d vim

This leaves the session unattached. If you want to attach to it, too, add this line at the end of the shell script:

# attach to the new session
tmux attach -t $_SNAME

Try out this to run mutt, for example

tmux has-session -t mail
if [ $? != 0 ]
tmux new-session -s mail -n mel_GMAIL -d "TERM=xterm-256color ; mutt -F $HOME/.mutt/muttrc_perso"
lxterminal --command="tmux attach -t mail"

It checks the existence of a 'mail' session running, if not, it creates one and starts the application mutt; finally it attaches to it


Here is a little script that starts or attach to Tmux and runs a command in it. Once the command has been executed, it'll exit Tmux.

#!/bin/sh -x

tmux has-session -t $SESSION_NAME 2>/dev/null
if [ $? -ne 0 ]
  tmux new-session -d -s $SESSION_NAME "$*"
exec tmux attach -t $SESSION_NAME

Usage example:

$ ./script-above 'echo hello world && sleep 10'

I like to do this:


function tmuxed {
    /usr/local/bin/tmux new-session -d -s bkp 2&>/dev/null
    /usr/local/bin/tmux send-keys "/Users/xxx/bin/thisscript backup" C-m

function backup {
    echo 'bla bla doing stuff'


And run /Users/xxx/bin/thisscript tmuxed

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .