Skip to main content

Managing Multiple Terminals

20 Jan, 20195 min read

During our development cycle, we all have switched to different terminal tabs to run differnt commands because in one tab we open our editor, in another (or two or three) we are logged into a server, in another our build system is running, in another our tail .log is running and so and so.... Sometimes, we forget about the arrangement of our tabs and so we switch 2-3 tabs to actually findout which tab we want to switch. Wouldn't it be nice if we could simply keep our related terminals tabs in one tab!! Wouldn't it be great if we could simply name our different terminal tabs and move from one to other seamlessly!! Surely it would (y)

Multiple Terminals in one screens (tmux)

What we need is a terminal multiplexer. There are many programs out there which does this and I'm use tmux. Tmux has a terminology that is quite suited to our workflow. It has sessions, windows and panes. Don't freek out :). Keep patience and you will get the terminology. Think of a session as a project and window as different terminals that will open for that project which are not that much related (we generally don't switch between them) and panes are like different tabs of a terminals where we frequenly switch or we like them to be visible to us all the times. Let's take an example for a better understanding of it.

Suppose we are a frontend developer (I'm :)). What would be our daily workflow. We change to our workspace directory, we start our build system (a build system is a set of multiple commands that needs to be excuted to output something, search for make which was/is used into build system in schools). We then open our editor, we then open another tab where we run git commands for different git operations, we then open another terminal where we run our deployment script which pushes our code to server (ssh). We do the same for whatever projects we are working on. So let's map them into tmux.

A project can called as session for tmux. (Assuming tmux is already installed on your machine, check it by running tmux -V else install it globally if you have admin rights or visit how to install tmux locally if you want to install locally, recommanded) We start a session by running tmux new -s my_project. This will create a session nammed my_project. Now we go to our project directory. As you can see, we can seperated our terminals into two categories: 1. related to our development environment and 2. related to our deployment which we don't that open. We want to keep our development environment as open to our eyes as possible and let deployment hide be hidden as we don't want it. So we create two windows. When we ran previous command, we actually created a window with a pane opened!!. To created a new window while we are in tmux, hit Ctrl+b (this is called Prefix for tmux) followed by c. This will create another window for us. Hit Prefix followed by w to see a list of all the window. We can navigate between them with arrow keys and press enter to enter into a window. Now let's name them. hit Prefix followed by , and you will b prompted (see on the bottom of your terminal) to enter name for a window. Name one and then switch to another and name that too. Assuming that we haved named them as Development and Deployment, which to Development window. What we want here. An editor, git and build system (npm start). And so we want to create three panes. When we create a window, it always creates a pane (it has to start somewhere =D). Open your editor here (vim or emac or nano =D). Hit Prefix followed by " to create a horizonal split. You will be moved to the newly created pane. Run your git status here. From here hit Prefix followed by % to create a vertical split. We will run our build system here. Backend guys can run tail xyz.log here. You should visit this cheatsheet to get yourself familiarize with tmux commands. There are just a few so do check them out.

Experiment with tmux by creating another session, switching between sessions and ditaching from session when we off of a project for long time, installing plugins for better user experience and creating your own tmux configuration best suited to your needs. We can do a lot of things with tmux like pair programming, panes syncing, session sharing and a lot more =D.

Automatic panes and windows creation (teamocil)

As we start our development in tmux, we will need something that can create panels and windows automatically for us when we kill a tmux session and need to restart all windows and panes or we need to share our tmux session configuration with our team or you messed up your layout. And so comes the teamocil, a tool (which really easy to integrate and can be learned in just 5 minutes (y)) that will help us creating windows and panes automatically for us with simple YAML file configuration. Just create a YAML file with your window configuration (visit here to know everything about teamocil) and save it somewhere on you machine (preferred to ~/.teamocil/project.yml or to your project's root if it is shared), start a tmux session with tmux and run teamocil --here _project_ and BOOM. Done. Create as many project yml files as you want.

Subscribe to the telegram channel (sudkumar) or add the RSS to your feed aggregator to get updates when a new article is published.