Text editors, although perhaps not the sexiest topic in the world, are really important. One of the most powerful text editors, and therefore one of the most popular, is Vim (short for, “Vi Improved”).
Although Vim has a sharp learning curve (which can be overcome with hours spent playing with the interactive Vim tutorial), and can be intimidating to new users, developers love it. That’s because it allows them to be more productive, and because it can be endlessly customized. This is done by editing the Vim configuration file, known as the “dotfile”.
The Power of the Dotfile
The dotfile is amazingly powerful, and can control almost every part of the Vim experience. Editing yours is really easy. First, you’ve got to check that you’ve actually got one. You can do this by running:
If there’s nothing there, you simply need to create it:
The Vim dotfile has its own embedded programming language, called VimScript. It’s what we’re going to be using in the rest of this article. But don’t worry. It’s really simple to understand. To get you started, here’s some useful lines to add to your config file.
Handle Indentation For You
Last month, I published an article on how to write clean, readable code. One of the points I made was to always avoid using ‘soft tabs’, as various text editors interpret them in different ways, and it can introduce problems in programming languages which are strict about indentation, such as Ruby and Python.
Thankfully, with a few lines of VimScript, you can turn each tab into a predefined number of spaces. Just add the following to your Vim dotfile.
set expandtab set tabstop=4 set softtabstop=4 set shiftwidth=4
Python users should use four spaces, as is recommended by the PEP-8 standard. For other languages, you’re best either reading the relevant style guide, or using your best judgement.
You can also set it to auto-indent where required. Again, just add the following lines of code to your dotfile. The second line of code ensures that no line can ever be longer than 80 characters.
set autoindent set textwidth=80
Turn Vim into a Distraction-Free Word Processor
While Vim is a great text editor for developers, it’s also great for those who want a simplified, customizable yet distraction-free environment for writing.
With a few lines of code, you can configure vim to switch into a “word processor” mode when required. This changes how text is formatted in the editor, and introduces things like spellchecking.
First, create a function called WordProcessorMode, and include the following lines of code.
func! WordProcessorMode() setlocal textwidth=80 setlocal smartindent setlocal spell spelllang=en_us setlocal noexpandtab endfu
Then, you’re going to need to define how you’ll activate it. The following line of code allows you to create a command. When in command mode, if you call “WP”, it will activate word processor mode.
com! WP call WordProcessorMode()
To test that it works, open a new text file in VIM, and press escape. Then type “WP”, and hit enter. Then, type some text, with some words intentionally spelled incorrectly. If VIM highlights them as incorrect, then you know you’ve installed it correctly.
Vundle is a package manager for Vim, not too dissimilar to the Ubuntu package manager. It allows you to extend the functionality of it, by installing third-party packages. Many of these are aimed at programmers; others are more generalized productivity tools.
Installing Vundle is easy. First, you’re going to need to install the Git version control system. If you don’t have it already, install it. The command for that on Ubuntu is:
sudo apt-get install git
Then, you’re going to need to clone the Vundle repository onto your local machine. That’s easier than it sounds. Just run the following command, and remember that it’s case sensitive.
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Then, add the following lines to your Vim dotfile.
filetype plugin indent on
In-between “Vundle#Begin()” and “Vundle#End()” is where you’ll add the packages you want to install. The only required one is “VundleVim/Vundle.vim”, but I also decided to install “L9” for the purpose of illustrating how to install third-party packages.
Once these lines have been added to your Vim config file, you need to do one more thing. Exit your Vim dotfile, and open a new text document. In command mode, run “:PluginInstall”.
If it worked, it’ll look a bit like this.
Change How It Looks
Many text editors (like on of my personal favorites – iA Writer) allow you to switch between a darker, ‘night’ mode, and a daytime mode. Vim is no exception.
To change the color scheme, simply add “colorscheme” to your dotfile, with the theme you wish to use. It should look like this.
@matthewhughes 3 fav lines: colorscheme darkblue au InsertLeave * colorscheme darkblue au InsertEnter * colorscheme molokai :-)
— Wyatt Andersen (@wandersen02) October 20, 2015
To see if it worked, reopen Vim, and confirm visually.
Slap On Some SPF13
If you don’t feel confident enough to modify Vim dotfile, there’s a simpler way. SPF13 is a distribution of Vim that comes pre-built with the plugins and dotfile modifications you need to be a productive developer. Installing it on Linux, FreeBSD, and OS X is remarkably simple. Just open up a terminal and run:
curl http://j.mp/spf13-vim3 -L -o - | sh
Then, let do what it has to do. Once it’s finished, you’ll have a turbo-charged Vim install. If you’re on Windows, read the instructions on the SPF13 website.
Using SPF13 doesn’t stop you from configuring Vim to how you like it, however. Just edit the .vimrc dotfile as required.
It’s worth noting that if you make a mistake in your Vim dotfile, Vim will let you know with some helpful debug messages.
If you have trouble making sense of them, a great place to ask for help is on StackOverflow, the Vim subreddit, and even MakeUseOf Answers. Just pay attention to this simple advice to increase the chance your question will be answered.
Over To You
Vim is a remarkable text editor. One of the great things about it is how it lets you configure it to your hearts content. If you want more inspiration, I highly recommend you check out the excellent Learn VimScript The Hard Way by Steve Losh.
For more inspiration, check out other people’s dot files as well. Many people have published their own on Github, under open-source licenses. The dotfiles written by Hakim Cassimally, Andrew Bolster, Amir Salihefendic, and one-time MakeUseOf writer Erez Zukerman are worth a look.
Are You a VIM user? Have you configured your own dotfile in an interesting way? I want to hear about it. Leave me a comment below, and we’ll chat.