Wiki: Tmux - Productive Mouse Free Development

Hi! This article is part of my personal wiki where I write personal notes while I am learning new technologies. You are welcome to use it for your own learning!

Oftentimes when doing software development you’ll run into the need of having lots of terminals open running different tasks: development web servers, editors, git, building, linting, remote servers, etc… If you haven’t put much thought/energy into it you’re likely to use tabs or different terminal windows which you create on demand and arrange every now and then with your mouse. This is typically slow and will require you to redo the whole setup any time you restart your computer. Tmux is a tool (a terminal multiplexer if we want to speak with property) that helps you level up your terminal wizardry. To put it in a succint way, tmux is the vim of terminal management. It:

eases the creation and management of terminal windows and panes with a few keyboard shortcuts

lets you setup working/developing environments that you can pause and resume at will

is entirely customizable and can be made to work perfectly in tandem with vim

lets you pair program remotely with your colleagues

In this wiki you’ll learn how to setup tmux to improve your development workflow and it’s up to you to train your muscle memory to take the most advantage of tmux.

Setting up tmux

If you’re using a mac you can intall tmux using homebrew:

$ brew install tmux

Otherwise use the favorite package manager of your OS for choice and you should be able to find it. When in doubt take a look at tmux’s website.

You can check whether it works by running the following:

$ man tmux

Which should show you tmux manual or:

tmux

Which should start a tmux session. A tmux session?

Tmux Sessions, Windows and Panes

You can think of a tmux session as a workspace or project work environment. A session can have multiple windows (which behave like text-based virtual desktops) and multiple panes which let you divide the screen horizontally and vertical within the sample window.

When you start tmux like this:

$ tmux

You create an anonymous session with 1 window and 1 pane. Each pane has its own isolated terminal running within it.

Within a session you can create new windows and panes at will. When you want to communicate with tmux (as opposed to the terminal within the active pane) you use a special key combination that tells tmux to handle whatever comes next. The special key combination is typically called prefix and it defaults to C-b (as in keep CTRL pressed and b).

For instance, you can type prefix + % to split a window vertically (creating an additional pane to the right), and you can type prefix + " to split a window horitzontally (creating an additional pane below). These are shortcuts for tmux commands which can be accessed by typing prefix :. The equivalent commands for the shortcuts above are prefix :split-window -h and prefix :split-window -v (for some reason I haven’t been able to comprehend yet tmux considers a horizontal split what the rest of the world considers a vertical one).

Basic Configuration

An awesome thing about tmux is that is completely configurable. If you’re a vim user you’ll be more comfortable creating vim-like bindings for tmux. For instance, you can change the prefix to C-j instead of C-b, that will put the CTRL key beside your left pinkie finger and j below your index finger nice and cozy in the home row.

Tmux configuration resides in ~/.tmux.conf. You will need to create that file and type the following:

This basically says that you no longer want to use C-b and insist in using C-j which is much better.

Using the tmux vim navigation plugin?

There is a great plugin called vim-tmux-navigator which allows you to navigate seamlessly between tmux and vim panes. Using CONTROL in addition to the hjkl motion keys will allow to navigate between panes regardless if you're in vim or tmux.

This is super nifty but it collides with the mapping above. Another great mapping that you can use is C-;. Tmux doesn't support this binding so you cannot map it directly inside your configuration file. Instead you'll need to tell your terminal to map that key combination to C-b. In iTerm2 for OSX you can do that inside Profile, Keys, Send Hex Codes, then use 0x02 which is the hex code for that key combination.

You can then continue customizing stuff using mnemonics the vim way. Splitting a window in panes is much easier to remember if you use | for vertical splits and - for horizontal splits:

Would you like to receive more articles like this one on programming, web development, JavaScript, Angular, developer productivity, tools, UX and even exclusive content like free versions of my books in your mailbox? Then sign up to my super duper awesome inner circle.

Did Ya Know I've Written Some Books?

I have! The JavaScript-mancy series is the lovechild of three of my passions: JavaScript, writing and Fantasy. In the pages of each one of the books of the series you’ll find a breadth of JavaScript knowledge, delivered with a humorous and casual style of writing and sprinkled with Fantasy at every turn.

They are the weirdest and quirkiest JavaScript books you'll ever find. There's nothing out there quite like it.