Let’s get this out of the way. The default vi editor is horrible. I think I first used it on my Raspberry Pi and, like many other people, had no clue what was going on.
It’s only after taking the time to experiment and explore the flexibility Vim offers can I believe that it’s the best text editor out there. This post is meant to show off some things in my Vim setup that I think can be useful to you. Also, this post assumes you have some understanding of Vim (modes, movement, basic editing). Although my
vimrc is on Github, I don’t recommend copy and pasting all of it. It’s too personalized to me, instead read this post first and build up your own. My
vimrc can be used as an example of how to create a unique
vimrc for one’s needs.
The default version of Vim that ships on Macs is out of date so I recommend using Homebrew to
brew install vim and use that latest version. Some of the plugins below require newer versions of Vim.
Make Vim Great Again
I’d seen countless people on the internet praise Vim and show off some pretty terminal screenshots of Vim. How do they do it? It’s all in their magical Vim configuration file,
vimrc, which is usually placed as a hidden file in the home directory (
The very first thing I have in my
Doing this sets up the basics. Next, an awesome feature of Vim is backups. Make sure you have a
.vim/tmp and a
.vim/backup directory setup to make use of Vim backups.
You can set it and forget it and Vim will automatically make backups of the file you edit. Having these backups has saved me on more than one occasion.
Mappings take one Vim command and direct them to another. Some commands, like enabling spell check, can be cumbersome to type out everytime
:setlocal spell spelllang=en_ca in Vim.
It is easier to create a kind of shortcut key to activate our own commands in Vim. This key is called the Leader. I define my Leader as the Space key.
So we can remap the spelling command seen above to something much simpler using a mapping command. There are a couple different mapping commands and you can read about the differences between them here. I prefer to use
nnoremap because it keeps my commands restricted to normal mode and non-recursive.
When it hit
<Space>sp or when I open a
.md file, spell check is enabled. The
<CR> at the end represents the
<Enter> key and makes sure the command gets executed. To disable it, I also use a custom mapping.
Once you learn the features and commands of Vim and other plugins, see which ones you use the most and remap them to quicker commands. In the long run, it will save you plenty of time.
Plugins are programs that integrate and interface with Vim. To be able to use plugins, you need a plugin manager. I use and recommend Vundle. Other options are Pathogen and NeoBundle. This post will use Vundle as the plugin manager and will use commands specific to Vundle. For any of the following plugins, you can read about their usage and settings by typing
:help [Plugin Name] in Vim after being installed.
Definitely the greatest Vim plugin. Airline is a “lean & mean status/tabline for Vim that’s light as air.” The default Vim status bar leaves a lot to be desired and Airline fills this void. It integrates with a bunch of other plugins and is very customizable. If you’re looking for a first plugin, Airline is the place to start.
With Vundle, add the plugin like so:
To get the nice symbols to appear, a patched font is required called powerline. You can read more about it here. Enabling the fonts, after being installed, requires these option to be set:
There are plenty of customizable settings for YCM and I encourage you to read through the help menu to see them all. For YCM to complete C-family languages, it requires an addtional configuration file to function,
.ycm_extra_conf.py. There is an addtional Vim plugin that will generate this file for you, provided that there is a Makefile for your current code project in the current directory. I also remap this generation command.
Ok…but what about the Makefile? I still have to generate that for each project. This is where a project of mine started from. Pymake will automatically generate the Makefile for you based on the files in your current working directory. The global config file you see in the code snippet above is a fallback config file that may not always be the right fit for the project I’m working in. To tie everything together, it made complete sense to script the whole thing in my
In addition to the function, I have a few YCM specific mappings. The first is the most useful. When YCM and detects a syntax error or another minor error, I can place my cursor on that current line, hit
<Leader>f and it will automatically correct the error for me!
Syntastic is a syntax checking plugin for Vim that utilizes external syntax checkers and displays any errors to to the user with tight integration with Airline. Syntastic can also automatically check your syntax everytime you write to disk. You can customize the feedback mechanism and have it display error messages in your statusline.
There is no other plugin that comes close to Fugitive for a git wrapper for Vim. My entire workflow in git can be done through Fugitive, without leaving the comfort of Vim.
NERD Tree is a plugin to help you explore your filesystem and open files in Vim. Its very helpful for quickly switching between files and directories.
The long command is too tedious to type everytime. Mappings to the rescue. Sometime I start Vim, but don’t specify the file, so I have NERD Tree start if no file is specified and I can quickly find my way around my file system.
VimColors.com highlights a wide variety of colorschemes for Vim. Take a look and choose one that fits you. I use Molokai and like it (it’s the scheme in the gif above). Choosing the right color scheme can brighten up your Vim setup and make it more enjoyable to work in.
Here’s a collection of some great Vim jokes I’ve seen around the Internet.
I've been using Vim for about 2 years now, mostly because I can't figure out how to exit it.— I Am Devloper (@iamdevloper) February 17, 2014
NO GRANDPA— SecuriTay (@SwiftOnSecurity) June 17, 2015
JUST HIT THE POWER BUTTON
YOU CANT EXIT VIM
Vim is a lot more fun if you treat it like a text adventure game:— I Am Devloper (@iamdevloper) June 20, 2014
- You remain stationary
- You remain stationary
The plugins, mappings, and configurations you see above just scratch the surface. I encourage you to tinker and experiment with Vim and the numerous plugins out there. I’ve tried to cover the basics of Vim customizations that will work for almost everybody, but there are many more customizations that can help you and your workflow, you just need to find them. :wq