Configuring Vim and TMux to Boost Your Productivity.
Why Vim and TMux?
Vim is a modal, highly customizable and lightweight text/code editor that comes preinstalled on most *nix machines. As the word modal suggests, it supports various modes of execution allowing one to achieve text manipulation objectives using powerful and finely-tuned built-in commands and user-defined macros.
Vim’s modality being its most powerful feature also make it incredibly difficult to master. That said, the rewards of mastering it quickly outweigh the initial cost of learning it.
Here are a few reasons that make Vim outstanding among other editors:
1. Performance — Vim’s core is implemented inC which makes it blazingly fast and lightweight. Unlike most of the existing code editors which are Electron-based like VSCode (which is essentially a browser instance) or running on JVM like IntelliJ, it is optimized through the use of low-level machine specific code.
2. Highly Customizable — the ability to configure your editor down to a keystroke is an immensely valuable feature. Through the use of a high-level scripting language (VimScript), one can compose built-in commands to create macros that tweak the different modes on Vim to one’s suiting.
3. Distraction-free — as a developer, I value having a distraction-free zone to concentrate on a task I am working on. Noise, however, does not relate to the environment only. In most editors, you continually have to reach out to your trackpad to open a new file or click on a button to trigger some action. With this, you can easily drift away from the task at hand. Vim, on the other hand, supports the use of the keyboard only to achieve everything imaginable on an editor.
TMux is a terminal multiplexer(therefore the name TMux) that allows you to run multiple terminal sessions simultaneously on the same window. It is also highly configurable to support keybindings that can spawn new windows and to quickly navigate between them.
Together, both Vim and TMux marry harmoniously to form a Swiss army knife for all your code editing needs.
Learning Curve Awareness
Before we get to how to installing and configuring both Vim and TMux it is important to be aware that mastering these two would require both time and effort. Although comical, the Vi (now known as Vim) learning curve indicated below is a good depiction of what to expect.
I would, therefore, advise you do not rush through this write-up and instead regard it as a reference for close to one week.
Installing and Configuring Vim
If you are running a *nix machine(Unix, GNU/Linux, and its derivatives or MacOS) you would most likely have Vim preinstalled. It is possible, however, to have Vim’s predecessor Vi installed and so you would still need to install it.
To be sure that we have Vim installed, kindly run the following command on a MacOS terminal (for GNU/Linux based machines kindly check how to install Vim using your specific package manager):
$ brew install vim
Having installed it, we can now fire it up by running the following command:
Going through the tutor, in my opinion, is the one stage that demands a lot of discipline and determination and one in which most people are likely to give up.
Although the tutor suggests you use 25–30 minutes to go through it, I would advise you have the 30 minutes sessions for 3 days to make sure you master most of the commands. Over time you will realize that these actions become intuitive and instead of having to think about what to type next, you find yourself navigating through the tutor rather easily.
The default Vim without extra configuration is really powerful for text editing. However, a few pain points may be realized such as not having a project’s files tree, code completion, integration with Version Control, fuzzy file search and syntax highlighting for certain languages. This is where plugins come to the rescue to unravel the full power of Vim.
To get you started, you would have to install a plugins manager. For this, I would recommend using VimPlug, which can be installed as directed here.
Having installed VimPlug, you can now install some basic plugins by creating a ~/.vimrc file running the following command:
$ touch ~/.vimrc
Within the file, ensure you have the following content (I would advise you use a code editor you are familiar with like VSCode):
Save it and on your terminal run Vim again through:
Now to add plugins, simply edit the ~/.vimrc file (using a code editor of your choice) and paste their configuration in the section indicated with ("---> this is where you add you plugins <---)above and restart Vim.
You would then type: PlugInstall while on Vim’s command mode to install the plugins. The following are the most essential plugins I believe you will need:
1. Project’s files explorer — NerdTree is currently what I use as my files explorer. To install it, add the following to ~/.vimrc :
3. Fuzzy Finder — to support fuzzy file searching, you would have to add CtrlP plugin as follows:
[pastacode lang="c" manual="%22%7B%7B%20Configuring%20CtrlP%0APlug%20'ctrlpvim%2Fctrlp.vim'%0A%22%7D%7D" message="" highlight="" provider="manual"/]4.Snippets — code snippets allow you to quickly write some boilerplate code that is often repetitive. To add code snippets support, you will have to add UltiSnips.[pastacode lang="c" manual="%22%7B%7B%20Configuring%20UltiSnips%0APlug%20'SirVer%2Fultisnips'%0APlug%20'honza%2Fvim-snippets'%0A%20%20let%20g%3AUltiSnipsExpandTrigger%20%3D%20%22%3Ctab%3E%22%0A%20%20let%20g%3AUltiSnipsJumpForwardTrigger%20%3D%20%22%3Ctab%3E%22%0A%20%20let%20g%3AUltiSnipsJumpBackwardTrigger%20%3D%20%22%3Cs-tab%3E%22%0A%22%7D%7D" message="" highlight="" provider="manual"/]
5. Version Control — to add an integration to git add the following plugins:
[pastacode lang="c" manual="%22%7B%7B%20Git%20integration%0A%22%20---%3E%20git%20commands%20within%20vim%20%3C---%0APlug%20'tpope%2Fvim-fugitive'%0A%22%20---%3E%20git%20changes%20on%20the%20gutter%20%3C---%0APlug%20'airblade%2Fvim-gitgutter'%0A%22%20---%3E%20nerdtree%20git%20changes%20%3C---%0APlug%20'Xuyuanp%2Fnerdtree-git-plugin'%0A%22%7D%7D" message="" highlight="" provider="manual"/]6.Color scheme configuration — Vim comes with quite a number of preinstalled color-schemes. There is the one I personally use though and I highly recommend it. To add it use:[pastacode lang="c" manual="%22%7B%7B%20Color-scheme%0APlug%20'morhetz%2Fgruvbox'%0A%20%20set%20background%3Ddark%0A%20%20colorscheme%20gruvbox%0A%20%20let%20g%3Agruvbox_contrast_dark%3D'default'%0A%22%7D%7D%0A" message="" highlight="" provider="manual"/]7.Autoclosing tags brackets and braces — typing out pairs of tags and braces can be rather painful. To fix this, use:[pastacode lang="c" manual="%22%7B%7B%20Autopairs%0A%22%20---%3E%20closing%20XML%20tags%20%3C---%0APlug%20'alvan%2Fvim-closetag'%0A%22%20---%3E%20files%20on%20which%20to%20activate%20tags%20auto-closing%20%3C---%0A%20%20let%20g%3Aclosetag_filenames%20%3D%20'*.html%2C*.xhtml%2C*.xml%2C*.vue%2C*.phtml%2C*.js%2C*.jsx%2C*.coffee%2C*.erb'%0A%22%20---%3E%20closing%20braces%20and%20brackets%20%3C---%0APlug%20'jiangmiao%2Fauto-pairs'%0A%22%7D%7D" message="" highlight="" provider="manual"/]
8. TMux integration — as we are going to be integrating Vim with TMux this plugin will be required on Vim’s side:
This completes the main configurations required on Vim to be up and running. In case of any pain points, a quick search will usually land you a solution in the form of a plugin or a script you would have to add to your configuration.
Installing and Configuring TMux
TMux also comes preinstalled on most of the *nix machines. To ensure that it is installed(or to install it), run:
Now, to split your window simply press Ctrl-A (referred to as the prefix key) followed by either | for a vertical split or - for a horizontal split. This way you can have as many terminal sessions as you want.
The Vim-TMux integration allows you to quickly navigate between the two by pressing Ctrl together with the HJKL keybindings used by default in Vim. This also works between any TMux sessions.
In conclusion, it might take quite a lot to get your Vim-TMux configuration working but the reward of having a code editor you have configured yourself next to your favorite terminal shell is totally worth it.
Lastly, I hope this has been helpful and in case you would prefer to use my configurations instead, you can easily access them here.