How to preconfigure Tmux sessions with Tmuxp (Part IV)

They way most people work with development environments varies a lot depending on tools they use and goals they have. Numerous times I saw people spending precious minutes on switching between IDE and console and back to run few lines of code. Moreover, have you ever heard about the guy that accidently run a migration on production because he got lost between tabs? These are things we want to avoid by using Tmux with preconfigured sessions provided by Tmuxp.

Running Tmuxp

Tmuxp uses regular YAML files for configuration. One can run it with

[code language=”bash”]
tmuxp load -2 $HOME/.tmuxp/scratchpad.yaml
[/code]

For convenience, I recommend defining few aliases that once remembered make things easier. For me it’s
[code language=”bash”]
alias tns=’tmuxp load -2 $HOME/.tmuxp/sratchpad.yaml’
alias tas=’tmux -2 a -t scratchpad’
alias tks=’tmux kill-session -t scratchpad’
alias tls=’tmux ls’
[/code]

Respectively, they are used to start, attach, kill and list sessions. Similar aliases should be defined for all of the sessions you have ex. dev, production, scratchpad etc. Yes, that’s a lot of aliases. And yes, it’s overwhelming at the beginning but you will get used to them faster than you think.

Basic configuration

Every session is represented by a separate file located by default in ~/.tmuxp. The name is up to you, although use things that make sense to you. Let’s break down one of mine dev config files.

Three most important things at the top of the file are:
[code language=”bash”]
session_name: growthhacker
start_directory: $HOME/workspace/growthhacker
shell_command_before: workon growthhacker
[/code]
Thanks to them session has a name, windows start in proper directory and Virtualenv is loaded automatically. Really useful stuff to avoid duplications later on.

Windows and panes

For those of you who are not familiar with window vs pane concept. The window is opened in a separate tab while pane is opened in the same window in a vertical or horizontal split. Personally, I use only windows as they allow me to focus on single thing at the point in time, which is crucial from the standpoint of productivity.

Example of window with single pane:
[code language=”bash”]
windows:
– window_name: vim
focus: true
panes:
– shell_command:
– clear
– vim .
[/code]
All it does is to launch Vim in a directory specified in start_directory with loaded Virtualenv shell_command_before and sets focus on it. Typical development environment consists of few windows, usually for Vim, Shell, PostgreSQL (if used), Mongodb (if used) etc.

Full config file

Looks like this:

[code language=”bash”]
session_name: growthhacker
start_directory: $HOME/workspace/growthhacker
shell_command_before: workon growthhacker
windows:
– window_name: vim
focus: true
panes:
– shell_command:
– clear
– vim .
– window_name: shell
panes:
– shell_command:
– clear
– window_name: postgresql
panes:
– shell_command:
– export PGHOST=localhost
– export PGPORT=5432
– export PGUSER=test
– export PGPASSWORD=test
– psql -d growthhacker
– window_name: mongo
panes:
– shell_command:
– mongo
– use growthhacker
[/code]

If you are curious about other configuration options, don’t be afraid to visit the official page.

Summary

Tmuxp is a really simple tool that magnifies your productivity up high. Configuration is easy, usage is even easier. I must say, this thing is legendary. Absolute must have. Next post in the series will talk about building reproducible development environments with Docker and Docker-compose. Don’t forget to check it out!



LEAVE A COMMENT

This site uses Akismet to reduce spam. Learn how your comment data is processed.