Preslav Mihaylov
Posted on May 1, 2018
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.
Posted on May 1, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.