Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create new tmux session if none exists

Tags:

tmux

I am trying to figure out how to attach to a tmux session if a named tmux session exists, if not I want to create a new one with the given name.

Currently, I know of a few tmux commands which can partly achieve what I am looking for, but its not clear how to combine them together to get what I am looking for:

  • tmux attach attaches to an automatically existing session - but errors out if no session exists
  • tmux new creates a new session - but it does so every time, so I can't leave it in my .tmux.conf
  • tmux has-session tests whether a session exists - but I don't know how to stitch it together with the other commands

Thus, I would like to create a tmux script, so that this happens automatically, instead of having to manually create it everytime I need to log into a sessions.

How can I write a automatic script so as to create a new tmux session (if a given session name doesnt exist) or attach to a session name (if it exists)?

like image 218
rampion Avatar asked Aug 07 '10 23:08

rampion


People also ask

How do I create a new session in tmux?

Using the Prefixes to Control Tmux By default, the prefix is CTRL+B. That is, we have to press the keys CTRL+B and then the command. For example, to create a new session, the command would be C. So, to create a new session we need to press CTRL+B and next C – CTRL+B, C.

How do you create a new tmux session from your current tmux session with the name Kali?

All the commands you can launch within your terminal, like tmux new -s sessionName can be launched from within tmux by pressing the trigger key (eg: ctrl-b ) then : then the command without the starting tmux part.

How do I delete a tmux session?

The Tmux kill-session command is used to kill a Tmux session. Tmux will terminate the current or last active session if no session is specified. Depending on whether or not you are currently in an active Tmux session. The -t option, with the session name as its argument, can be used to specify a session to kill.

How do I start tmux automatically?

To configure your terminal to automatically start tmux as default, add the following lines to your ~/. bash_profile shell startup file, just above your aliases section. Save the file and close it. Then close and reopen the terminal to start using tmux by default, every time you open a terminal window.


1 Answers

I figured it out (and had it pointed out to me).

tmux attach || tmux new 
like image 133
rampion Avatar answered Nov 09 '22 11:11

rampion