Automate your workflow using tmux scripts

Bonnie Eisenman
InstructorBonnie Eisenman
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

Do you have a standard workflow that involves setting up a specific tmux layout, or running certain commands? By writing tmux scripts, you can automate your tedious daily setup, while simultaneously documenting it for yourself. We'll cover the tmux command equivalents of common key bindings and write some simple, useful scripts.

[00:00] If you often find yourself wanting to create the same kind of tmux set up -- say you always want the same layout when you're working on a project -- tmux scripting can be a really powerful tool.

[00:09] You already know that we can use tmux from the command line in order to create panes, organize windows, things like that. Now we're going to put that together into a script. I'm going to create a file called tmux-launch.sh. Then I'm going to make it executable. Now that we've done that, let's edit it.

[00:30] The first thing I'm going to do is I'm going to make a variable called session. This is going to be what we name our session, and we'll need to refer to it later, so this lets us change our mind later on if we want.

[00:41] To start with, I'm going to create a new session by running tmux new-session -g, so we start in detach mode so that the rest of the script can run, and then -s, and we'll name the session. This is where we will use the variable we created above.

[00:57] Next, let's create a new window within our session. We're going to do that by typing tmux new-window, now we'll use -g, and we're going to do $SESSION, and then 1. This is going to be our window number 1, and then -n. We're going to give it a name. I'm going to call this web-server.

[01:17] This is the window that we're going to use to keep a web server running while we develop a project. Before I forget, let's put that in some quotes. All right, let's test this to make sure it's working as I expect.

[01:28] I'm going to run sh, and then tmux launch. If I type tmux ls, you can see that we have a session named dev running with tmux. I can attach to that by running tmux attach -g, and then the name of the session, which is dev.

[01:45] You can see we've got our two windows here just like we expect, one for Bash and one that's called web-server because we named it in our script. All right, let's keep going. We're going to open tmux launch again. The next thing I want to do is I want to configure this window a little bit. We're going to do tmux and then select window.

[02:04] We're going to set the target to be the first window in our session, so again, SESSION=1. Now our subsequent commands will affect this window. I can do tmux split-window. Let's say we want a horizontal split, so I'll do -h. Remember, we're working in this to do MVC projects.

[02:22] Let's say I want to launch the web server in one of the examples. I'm going to do tmux send-keys to send these commands to the tmux window we selected. Then I'm going to type cd examples/react python-m simple http server.

[02:45] Then, and here's the important part, I have to end this with a capital C-m, which will send "Enter" keystroke. Finally, another useful thing that we're going to want to do is attach to the sessions.

[02:56] We'll do tmux attach -t and then the session name. All right, let's give this a try. I'm going to quit out of Vim. I'm going to look at my currently running tmux sessions and kill the one from when we were testing this before, so kill session -t dev. Now let's try our script.

[03:16] I type tmux launch.sh. As you can see, here we go. We are spit into GG MVC directory, and then we also have the Python server running right there for us when we start up. Obviously, you can get really complicated with this and tune it as much as you need to your workflow.

[03:38] You can set up many different windows. You can start many different processes in the background depending on what you need for your development. Just remember that any amount of configuration you can do within tmux, you can usually do from the command line, and therefore you can script it too.