As a web developer you need to start different web services in console. Even if you have a small project you need a backend web server, frontend web server, database, terminal, maybe some logs. Which means every single time when you work with project you open 5+ tabs in your terminal. Things get even worst if you work with a team on a big project. It may be that you need to simultaneously run 3-4 projects with 5 tabs each on your machine. Which means you get 20+tabs in your terminal and you have no idea how to find the correct one and what to do.
In this video you will learn how to organise any amount of tabs in your terminal by using tmux.
So what is Tmux? It's a terminal multiplexer. This means that you get tabs and splits in your terminal. And here you for sure want to say that your terminal like Iterm2 for example supports such stuff out of the box. And you are right. But what standard terminal can't do is switch between projects and store sessions in a comfortable way.
So problem #1 for us is that we have all projects mixed in our tabs and we don't have a way to split them. After installing Tmux we can start a Tmux session. This is exactly a way to split our project.
tmux new -s frontend
This creates a tmux session and attaches to it. Attach in Tmux world means jump inside. We named this session frontend because we want to start here things related to our frontend application.
Now we can detach from tmux and start another session by using Ctrl+b d. And Ctrl b is a Tmux default prefix before commands. And d obviously means detach.
Now let's create one more session
tmux new -s backend
We are directly inside backend session were we will call all our webservers related to backend.
Now the most interesting part. We can hit Ctrl + b + s which means show the list of sessions. And what you see here is 2 sesssions. Frontend session and backend sesssion. This means that even after we detached from frontend session it is still there. And this is the most awesome and mindblowing thing about Tmux. all sessions are hanging there until we destroy them.
So we open a session, we create different tabs, we rename them, start webserver, logs, whatever, then let's say you need to switch to other project completely. We just detach from this session and switch to another. When we want to return on working on our project we simple attach to session back. And all our splits, tabs, webservers, logs, files and exactly in the same state how we left them.
So this are 2 main things why I use tmux more then 10 years and it is still amazing. I constantly have 5-10 sessions just hanging there and I need just 1 click to get back to any of them.
Now if you want to start using Tmux it won't be super smooth. You need to learn Tmux commands and keybings, configure it for yourself and tune things from time to time.
If you want to see my Tmux config I will link it in the description box below.
But actually I don't use that many Tmux commands.
- When I'm inside Tmux I create new tab with Ctrl + b + c
- To rename a tab I use Ctrl + b + ,
- To switch between tabs I'm using number Ctrl + b + 1/2/3/4
- I also don't like the default Tmux prefix Ctrl + b this is why I reconfigured it to Ctrl + Space
- If I need to split window I have custom keybings Ctrl + b | and Ctrl + b - which are easy to remember because the key shows how the window will be splitted.
- And of course Ctrl + b +s to see the list of sessions
And here is a advanced bonus of Tmux for you. So what Tmux doesn't do by default is it doesn't remember what tabs and names were opened in session if we restart the machine. So what we want to do is to configure a folder to work with specific session. For example I can a Rails project which requires Editor, web server, shell and database. This is why inside a project I created .tmux file.
#!/bin/sh set -e if tmux has-session -t=mla-website 2> /dev/null; then tmux attach -t mla-website exit fi tmux new-session -d -s mla-website -n vim -x $(tput cols) -y $(tput lines) # Vim tmux send-keys -t mla-website:vim "v ." Enter # Server tmux new-window -t mla-website -n server # Webhooks tmux new-window -t mla-website -n webhooks # Shell tmux new-window -t mla-website -n shell tmux attach -t mla-website:vim
In this bash script I specify the session name and what tabs needs to be open. Now instead of creating session and all tabs and start applications after every rebooting I can simply run this script and it will be created on the fly.
And obviously I have an alias for that so I just press t and magic happens.
Tmux is a strong and versatile tool which allows us to simplify working with terminal.
And if you are interested what editor I'm using together with Tmux make sure to check this video also.
Also if you want to improve your programming skill I have lots of full courses regarding different web technologies.