DEV Community

loading...
Cover image for tmux - A Terminal Multiplexer

tmux - A Terminal Multiplexer

pmihaylov profile image Preslav Mihaylov Originally published at pmihaylov.com Updated on ・8 min read

Last time, I wrote about why I think everyone should try using a bare bones text editor like vim at least for a while.

After such an experience, some might give up on it, while others might want to switch to vim full-time.

For the latter - congratulations!

But inevitably, a problem will occur with this approach, especially if you are working on a big project - using bare bones vim for big projects is quite unproductive compared to IDEs.

I used to use vim on my hobby side projects and it did a great job, but once I tried using it on a project with a large code base and several different build variants, things started to get messy and I preferred using the IDE.

Some of you might disagree with me, I have seen people prefer sticking solely to bare bones vim.

But in my experience, apart from the text editing boost it gives you due to its awesome user interface, all the auxiliary tools you need to maintain a big project are invaluable and not easily available from a bare bones vim.

So, determined to amend that, I started exploring many different tools and plugins to enable my vim to challenge the productivity my IDE provides.

In this new series of articles, I will share my favorite tools which help me keep myself productive everyday by using vim.

In the first one, the topic is my all-time favorite vim add-on - tmux.

This article is part of the sequence  Boost Your VIM where I share my favorite vim plugins and tools which can greatly optimize your productivity and make you a better keystroke ninja.

So what is this tmux fuss about?

tmux is a terminal application, that allows you to emulate several terminals in a single terminal window.

That's all there is to it really. And it is more than enough.

Why is this necessary?

Well, have you ever had to do some editing in vim and perform some external terminal commands?

For example, do some changes and use git to add them to your repo and commit?

Or whilst doing that, you want to check something in a different directory, but you still want to preserve your current session?

Well, in normal circumstances, you would open another terminal window, do your work and return to the previous one.

But if you have to open too many sessions, things can, unfortunately, get hairy.

The problem is that opening a new terminal window means opening a new GUI application, which you can go to using the mouse easily, but navigating to it using keyboard stokes is harder.

And apart from that, if you want to observe the progress on several terminal windows at once, you would have to adjust their position on the screen using the mouse.

I don't know about you but whenever I have to move my hands from the keyboard to get the mouse, it feels bad.

And more importantly, you lose valuable keyboard frenzy inertia. So overall, your productivity drops.

In order to spawn all those terminals and order them in this manner it took me a looot of mouse clicks and movements.

If you have to do that often, it can quickly get quite frustrating, especially if you are deeply focused on your problem at hand.

But by using tmux, on the other hand, you can do all that work in a single terminal window with several keyboard strokes rather than by using the mouse.

In order to adjust the above layout, not a single mouse click was used.

Install and Configuration

tmux is a separate software from vim. So you can install it the way you would install any other normal software.

On Debian distros:

sudo apt-get install tmux

On Red Hat systems:

yum install tmux

That's it! You have tmux on your system. You can run it by simply typing tmux.

However, the default configuration you get with tmux is pretty rugged and it can get you a bit irritated once you start using it for the first time.

For example, I recall there were some problems with copying text from a tmux terminal, there were issues with some colors and there was incompatibility when using certain shortcuts in vim with tmux.

In order to fix that, you will have to browse a lot to make tmux actually usable and user-friendly. If you like doing such stuff, be my guest.

But if you just want to get a normal, working configuration of tmux and start using it, you can copy my .tmux.conf file from my github repo.

Simply copy the contents of the file I shared and paste it in a .tmux.conf file located in the root of your home folder. That is, the ~ directory.

That's that! Now you have a working tmux fully compatible with vim and (almost) not a pain in the ass to use.

tmux is great! Can't I make it start automatically when I open a terminal window?

(flashes, buzzz, peew peew sounds. The Supervim guy lands in front of you)

Yes you can!

All you have to do is append these lines at the end of your .bashrc file (or whatever bash profile file you use) in your user home directory.

But be wary!

If you add these lines and you break something in tmux's configuration, you won't be able to start a terminal at all.

So, if you are tweaking your .tmux.conf file, make sure you comment those lines out beforehand.

But how do I use it?

Finally, the moment we've been waiting for. You got your new toy, you got it assembled. Now it's time to play with it!

So, you can use tmux just like you would use a normal terminal. There should be no other strange behavior observed.

With a single exception (I have found thus far).

Whenever you want to copy something from the terminal with the mouse, you need to hold shift as well as drag the mouse.

That's for the side effects of using tmux. Of course, if you skip the configuration part, you would have A LOT of side effects. But assuming you did that part well, there is nothing to fear.

Ok, now for the interesting part, how do you utilize the tool to spawn countless terminals inside your terminal and control them.

The special tmux controller state

When you use tmux everything works as normal UNTIL you click Ctrl-B. This is a special key-binding which activates the tmux controller state. I have no idea how it is actually called, that's how I prefer to call it. I better patent it soon, hehe.

However you prefer calling it, it is the state in which you can use different keys and they will not act as they would in normal circumstances, but do something specific to control the multiplexed terminals.

If you don't do anything in this state for some time OR if you click the enter key, you will go back to the normal terminal state.

There are numerous keys you can use in this state to do all sorts of things. I will tell you only the most important ones, which are the only ones I usually use. For some other fancy use case, refer to the tmux man pages.

Spawning a new terminal

<Ctrl-B>" - Spawns a new terminal vertically.

<Ctrl-B>% - Spawns a new terminal horizontally

These operations can be recursively used on inner tmux windows as well to create a beautiful grid of terminals.

Just a note on these two operations. After you use them, you go back to the normal terminal operation.

This means you can't chain terminal spawns like:

<Ctrl-B>""""

Instead, you have to Invoke <Ctrl-B>" separately several times.

Navigating the grid of terminals

<Ctrl-B>Left - go to the left terminal screen.

<Ctrl-B>Right - go to the right terminal screen.

<Ctrl-B>Up - go to the upper terminal screen.

<Ctrl-B>Down - go to the lower terminal screen.

In contrast to the previous commands, when you invoke any of these ones, you will be left in the controller state.

This means you can combo these keystrokes to quickly navigate between screens.

<Ctrl-B>Left Down Right

And finally, when you reach the screen you want, click Enter to go back to normal mode.

Resizing a cell

If you feel a terminal cell is smaller than you want it to be, you can use the following key bindings to adjust it's width and height:

<Ctrl-B><Ctrl>Left -  increase width to the left

<Ctrl-B><Ctrl>Right -  increase width to the right

<Ctrl-B><Ctrl>Up -  increase height towards the above cell

<Ctrl-B><Ctrl>Down -  increase height towards the cell below

It's easier if you try for yourself to get the hang of it.

Again, similarly to the previous operations, these leave you in the controller state.

 Conclusion

That's all you need to know in order to get started with tmux. This is enough knowledge to get you to level ground to actually use it efficiently.

The rest is practice and getting used to the new toy.

Hope you enjoyed it!

But that is nowhere near enough to boost your vim and get it to level ground with a modern IDE in terms of tooling and enhancements.

What about navigating your project structure? Finding usages of functions? Auto-completion?

We are just getting started!

So stay tuned for more awesome tools that can turn you into an efficient vim keystroke ninja.

Discussion (17)

Collapse
biros profile image
Boris Jamot ✊ / • Edited

Hi, tmux is a really useful tool when using a terminal without split feature.
I use guake and I easily started tmux with your config, but I face a problem while trying to resize panes vertically with <ctrl+B><ctrl>Up or Down : nothing happens whereas it works perfectly for horizontal splitting.

Any piece of advise?

Collapse
pmihaylov profile image
Preslav Mihaylov Author

Hi Biros,

I tried doing that on my terminal and it worked (the resizing for vertical splits).
Maybe you have remapped Ctrl-Up/Down to something else in some of your configurations?

Try remapping the controls in tmux.conf to not C-up but something else, like C-u or something like that just to debug what the problem is.

Collapse
biros profile image
Boris Jamot ✊ /

In fact, the problem is only in full-screen mode with guake.
I also tried with another terminal and it works, whatever it is fullscreen or not.

Collapse
pmcgowan profile image
p-mcgowan • Edited

Yes! Tmux + vim (with ctrlp-vim) is the way to go - only one or two terminals per project.

I also added set -g mouse on to .tmux.conf to click-select panes and mouse wheel to scroll (instead of scrolling through bash history).

Collapse
dmfay profile image
Dian Fay

To add: if you don't want to deal with the complexity of a multiplexer, use neovim and you get a built-in terminal emulator that can run in a split :)

Collapse
aghost7 profile image
Jonathan Boudreau

There's a couple of things that you can do to make tmux feel similar to VIM. I'm my tmux.conf file I have the following:

# When in clipboard selection mode, behave like vim. E.g., "b" will go back a
# word, "w" goes to the start of the next word, "e" goes to the end of the next
# word, etc.
setw -g mode-keys vi

# Start the selection with "v" just like in vim
bind-key -Tcopy-mode-vi 'v' send -X begin-selection

# Copy the selection just like in vim with "y"
bind-key -Tcopy-mode-vi 'y' send -X copy-selection

# Move around panes like you can with ctrl+w in VIM
bind-key j select-pane -D
bind-key k select-pane -U
bind-key h select-pane -L
bind-key l select-pane -R

Full config: github.com/AGhost-7/docker-dev/blo...

Collapse
pmihaylov profile image
Preslav Mihaylov Author

That's actually pretty useful. Thanks!

Collapse
pmihaylov profile image
Preslav Mihaylov Author

neovim is a great tool, I agree.

However, tmux can be useful outside of vim as well.

If you want to do a non-vim related task in the terminal, I wouldn't want to have to open vim just to be able to multiplex my terminal.

Correct me if I'm wrong. :P

Collapse
dmfay profile image
Dian Fay

It depends, as always. If you're just writing code and need a shell or two to start builds or commit things, the inline terminal emulator is everything you need and more. If you're managing a bunch of servers -- obviously not.

Collapse
moopet profile image
Ben Sinclair

You don't need neovim to do that - it's been part of Vim for a while now.

Collapse
aboardgravyboat profile image
Jon

Nice to see love on this topic.

It's worth mentioning that with some config, you can use your mouse to switch tabs or resize splits in tmux. With x forwarding, this also works over ssh. Mouse is handy to have while you're learning the shortcuts

Collapse
colinmtech profile image
Colin Morgan

Great article. Tmux is an awesome tool and I highly recommend learning it. Tmux sessions also provide a great way to maintain your shell sessions across devices and also for sharing them with others.

Collapse
engineercoding profile image
Wesley Ameling

I have been using this for a few days now and really enjoying it! You have given me a solution to a problem I have always thought about researching but always ended up in the bottom drawer.

Thanks!

Collapse
iglesias profile image
Fernando J. Iglesias García

For very quick tasks, instead of opening a new tmux pane, I also like to Ctrl+z my vim window, do the quick job, and then go back in a microbeat to vim with fg.

Collapse
codevbus profile image
Mike Vanbuskirk

Any reason why you wouldn't use Vim buffers?

Collapse
pmihaylov profile image
Preslav Mihaylov Author

Sometimes you want to use tmux not only as an add-on to vim, but for more generic terminal-related tasks as well.

In those cases, I think it's pointless to have to open vim just to be able to multiplex your terminal.

Collapse
frzleaf profile image
Lê Thành • Edited

It makes windows live again :)

Forem Open with the Forem app