Home Terminal Tuts How to create a Tmux session using a script

How to create a Tmux session using a script

by Abraham
tmux session with a script

You probably already know what Tmux is if you’re here so I won’t go over it again. However, if you don’t know about Tmux, don’t worry since we have an article tailored to help you quickly learn about Tmux. You can find it here.

When using Tmux, you occasionally utilize several layouts and numerous environments depending on the type of project at hand. However, you can’t keep using the long methods to get things done. There will come a time when you need to create a script to help quickly make a Tmux session. If that is your case, then stick around to learn how to create a Tmux session with a script.

Regular Tmux users will conquer that they are used to re-creating the structure of the same sessions each time they start a Tmux session. However, various projects aim to provide a mechanism to build a configuration file and then restore a session based on it, for example, Tmuxinator and Teamocil. The two are both Ruby-based, and if you are not a Ruby developer, getting a language interpreter and package management for such a task may seem like too much of a burden.

However, worry not, since I’ll show you how to use Tmux CLI commands to construct a Tmux session script in this post, so you can have a similar structure without repeatedly setting up everything.

Creating a Tmux session script

The first thing is to create and make the tmux-start.sh file executable. You can do so by using the commands below:

touch tmux-start.sh  #Creates the script file

chmod +x tmux-start.sh  #Makes the script file executable

Once you are done creating and making the Tmux script executable, set the file contents with the aid of the bash shell to make them executable using the line of code below:

#!/bin/bash

The above line of code informs the system that the subsequent text will be a bash script.

Now create a new session and name it. But first, it is essential to note that we shall be defining a variable to store our Tmux name session. This is vital as it will help us change the session name in the future.

session= "foss"

tmux new-session -d -s $fosslinux

Note: There can’t be any spaces here, and you can’t use the same name repeatedly. Name collisions may be particularly strange in nested sessions, with windows nesting one another in an unending loop.

Except for the prefix key (Ctrl-b), every keybind in Tmux is implemented by delivering a command to Tmux. Ctrl-b>c, for example, transmits the new-window command, whereas Ctrl-b>n transmits the next-window command.

You may do the same thing by issuing the following commands from the shell:

tmux new-window

Many commands take options; for example, we may use “-t” to indicate the target index for a new window. You may receive a list of all default key mappings by typing Ctrl-b>? (list-keys).

This is a strong notion since anything we interactively with Tmux can be programmed. We can write a shell script to launch a workspace with this information.

For this example, I’ll construct a script to launch a workspace where I can write on my website. We’ll need three windows: one with only a shell, one to launch a web server, and one to launch foss.

First, we’d want to begin a new session:

tmux new-session -d -s fosslinuxtuts

Code explanation

The “-d” option stops Tmux from connecting to the new session; that’s what the “-d” command does for most commands. The “-s” option specifies the session’s name. ” new-session” also launches a window because you can’t have a session without windows. If you wish to name this window, add the “-n” command

Create a new window using the line of code below:

tmux new-window -d -t '=foss' -n server -c _foss

tmux send-keys -t '=foss:=server' 'python -mhttp.server' Enter

Code explanation

  • “-t” specifies the target window, which in this case is simply a session name so that Tmux may use the subsequent unused index.
  • The “=” ensures an exact match.
  • The “-n” option names the window
  • The “-c” option specifies the directory.

Note: In this illustration, I will not start the program using the shell command new-window since I don’t want the pane to quit if I restart or stop it. Thus, I will start it with send-keys.

tmux new-window -d -t '=foss' -n fosslinux

tmux send-keys -t '=foss:=fosslinux' 'FOSSLINUX_NO_BUNDLER_REQUIRE=1 fosslinux build -w' Enter

Once you are done, attach the new session:

[ -n "${TMUX:-}" ] &&

    tmux switch-client -t '=foss' ||

    tmux attach-session -t '=foss'

The above test will ascertain that it works perfectly inside and outside another Tmux session.

Putting everything together:

#!/bin/sh

set -euC

cd ~/code/arp242.net

att() {

    [ -n "${TMUX:-}" ] &&

        tmux switch-client -t '=foss' ||

        tmux attach-session -t '=foss'

}

if tmux has-session -t '=foss' 2> /dev/null; then

    att

    exit 0

fi

tmux new-session -d -s foss

tmux new-window -d -t '=foss' -n server -c ~/code/arp242.net/_foss

tmux send-keys -t '=foss:=server' 'python -mhttp.server' Enter

tmux new-window -d -t '=foss' -n fosslinux

tmux send-keys -t '=foss:=fosslinux' 'FOSSLINUX_NO_BUNDLER_REQUIRE=1 fosslinux build -w' Enter

att

Conclusion

That’s all. You can now launch a Tmux session using the script we created. Please let us know how you found this article guide. Thanks for reading.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.