Bash Scripts for tmux

How to save a few keystrokes when setting yourself up for development

Bash Scripts for tmux

If you’ve recently made the switch from an IDE (integrated development environment), like visual studio code, to a more terminal-focused development environment, then you have likely heard of tmux. If you’ve never heard of tmux, then check it out on google! If you’re already a seasoned tmux user, then I’m sorry for this intro.

Very quickly, tmux is an awesome window management tool that let’s you split your terminal into multiple windows or panes/sections.

With the default key-bindings, after starting tmux with:tmux

you can:

  • Create a new pane by splitting horizontally: Ctrl-b “
  • Create a new pane by splitting vertically: Ctrl-b %
  • Move to the top pane: Ctrl-b (Up arrow)
  • Create a new tab: Ctrl-b c
  • Go back to your first tab: Ctrl-b 0
  • Go to your second tab: Ctrl-b 1
  • and finally, close the current tab or pane: Ctrl-d

You can also resize panes and do many other things, see here for a nice cheatsheet.

This is great and all, but one problem I keep coming across is that I need to do this every time I want to start working on a project. After some thinking and research, I realised I could automate this with a bash script and save myself a bit of time!

Note: much of this tutorial assumes you are working with mac or linux with the bash language. If you are on windows, then you can use the Windows Subsystem for Linux.


Making the bash script

Commands like ls, cd and grep are all c programs that you run with bash. These are found in your /bin/ directory. Don’t believe me? Print their contents to the terminal with the following command:cat $(which ls)

or (if you’re using fish shell)cat (which ls)

You should see some gobbely goop (because they’re written in compiled c code), but trust me that’s the contents of the file.

One cool thing you can do is write your own bash scripts (in the human-readable bash language), if you know a bit of bash or are following along with a guide such as this.

To get started, a common convention is to save your bash scripts in the ~/bin directory. If that directory doesn’t exist, create it now with:mkdir ~/bin

Now that we have this folder, we can add it to our shell’s (e.g. bash, fish or zsh) path and run any bash script found within. This directory may already be added by your shell’s path, but to make sure it is, add the following line to the end of your shell’s config file.export PATH="$HOME/bin:$PATH"

For bash, this config file will be found at: ~/.bashrc

For zsh, this config file will be found at: ~/.zshrc

For fish (if you use it like me), this config file will be found at: ~/.config/fish/config.fish

If these files don’t exist, create them and add the above line of bash code.

Now that we have a folder where we can add our custom bash scripts, create one called “dev” (with no extension) and add the following code:

Note: “dev” is just a simple name I used for this script. You could call it anything you like!

Now what’s going on here?

Well, the first line specifies that this should be run with bash, and the next two lines are tmux commands that say “split the window vertically into two panes with the bottom section being 20% of the total height” and “split the window horizontally into two panes with the right window being 50% of the total width”, respectively.

Now, once saved, you will need to allow the current user the ability to run the bash script with the following command:chmod u+x ~/bin/dev

Running the bash script

Now start up tmux withtmux

and type in the name of your bash scriptdev

You should now be greeted with three tmux panes that you can use for development (like the image, below). I like to use this setup to have my top pane for code editing with neovim (my editor of choice) and the bottom panes for running/testing my code and servers.

Extending the bash script

Now that you have your script setup, you can extend this script to do other interesting things, like start your server or run make files. Because I work a lot with python, one thing I extend this script to do is check to see if I have a python virtual environment in the current path, and if so, activate it in all three panes.

For bash, you could do this with the following:

One annoying thing is that fish, the shell I use (it’s a hip alternative to bash) has to be started with the activate.fish script, rather than the activate script, so if you use fish, you would need to change the script to the following:

Now, what I’ve done here is add the: tmux send-keys “” commands. These type the keys specified within the “” marks and finally executes them if you end the statement with C-m. I use this command to check if there is a directory called venv and if so, activate the virtual environment found within. I also get the last pane to print “Welcome back, Jake!” to give me a bit of joy when I start my day. You could change this to run any valid bash commands for your workflow.


That’s it! I hope this was helpful to you and thank you for reading to the end. This is my first post (hopefully of many more to come!). Please comment below if you have any questions or ideas for future posts.

Thank you to sir_bok, lllllll22 and funbike for their recommended corrections :).