I started using Vim a while ago with the great distribution Janus. Since then I made a a lot of changes to my config, but it never felt entirely like my own setup. There always were a few plugins I wouldn’t use and some features I couldn’t get to work because I didn’t understand the internals of my distribution.
Because of this I decided to entirely drop the setup I’ve been working with for the past years and started from scratch, adding only what I needed. If you want to see the result, my
.vim is available on Github.
Overall I found the experience to be way more easy and fun than expected, so I decided to share! In this article I’ll explain how to quickly setup and customise Vim without having to use a pre-made distribution, just Git and a Terminal.
Disclaimer: All the choices made here reflect my opinion, the way I use Vim and the tools and languages I generally use. Obviously if you’re using an ergodox and only code in Flow-Matic we will have very different opinions on what is best.
Out of the box, Vim is very hard on the eyes and doesn’t have a lot of feature we’ve come to expect from other editors like Sublime or Atom. However, it’s more evolutive and customisable, so the first thing to do is to start setting it up to fit your preferred way of coding. I recommend versioning your configuration to share across machines while keeping track of any changes you made.
To do this, create a
.vim repository with a
vimrc file that will contain most of your configuration:
$ cd ~ $ mkdir .vim $ cd .vim $ touch vimrc $ git init .
Now you need to go back to your home directory and symlink the
vimrc file. This way you get to use your
.vimrc file normally while still get all the advantages of versioning with Git.
$ ln -s ~/.vim/vimrc ~/.vimrc
Getting Vim To Look Nice
Picking a Theme
If you don’t like this one, don’t worry: there are tons out there. Just Google “Vim Themes” and browse around.
Once you found you favorite one, add it to your project by first creating a folder to store it:
$ mkdir ~/.vim/colors
Add the theme’s
.vim file to this folder. Once this is done, edit your
.vimrc file and add these lines to let Vim know that you want syntax highlighting based on file names:
filetype on syntax on colorscheme Tomorrow-Night
Once you have your main theme, time to pick a font! I like my fonts readable, simple and big. So I went with Menlo Regular 18pts.
To use this, download and install the font and then add this line to your
set guifont=Menlo\ Regular:h18
To get Vim to open with a given size, add this to your
set lines=35 columns=150
Add a colored column at 90 to avoid going to far to the right:
I’m not quite sure why this isn’t by default, but here’s how to add line numbering:
Now that your Vim starts to look better, let’s start to improve how it actually functions. The great thing with Vim is that we can get it to do whatever we want… it might be overwhelming at first, but it’s really not that hard.
Before doing anything else, we need to set a Leader key.
The Leader key is Vim’s way to reduce conflicts in configuration. The Vim user can set this key to be anything and then every command are going to be preceded by this key. By default it’s set to be backslash, but I like to change it to
let mapleader=" "
Reload Vim Config Without Having To Restart Editor
Every time you edit your
.vimrc file, if you want to see the changes you have to close & open it again or source the
.vimrc file manually. It’s a pain, so let’s map some keys to do it automatically:
map <leader>s :source ~/.vimrc<CR>
It’s the first time we see this syntax and it might look a bit weird. Basically it declares “please map (
Leader key + s) to the following command:
:source ~/.vimrc and then press
Enter” which effectively reload the Vim configuration. Once I add this to my configuration, pressing the leader key followed by
s will run the command above.
Note that it only works for
.vimrc changes and not for adding or removing plugins.
Vim is pretty old and made to run on machines with low processing power. Because of this, when you install Vim a lot of interesting features are deactivated by default. Here’s what most people would want their 2016 text editor to have.
Keep more info in memory to speed things up:
set hidden set history=100
Have some logic when indenting:
filetype indent on set nowrap set tabstop=2 set shiftwidth=2 set expandtab set smartindent set autoindent
Note that this particular config is made for myself and I mostly use Ruby which uses 2 spaces as tabs.
Dealing With Whitespaces
Here’s a handy command to remove whitespaces on save.
autocmd BufWritePre * :%s/\s\+$//e
When running a search, get Vim to highlight found words:
Ability to cancel a search with
nnoremap <silent> <Esc> :nohlsearch<Bar>:echo<CR>
People are divided on this, but I have made my mind and I prefer using arrow keys instead of the default Vim mapping. If you want to do the same:
map <D-A-RIGHT> <C-w>l map <D-A-LEFT> <C-w>h map <D-A-DOWN> <C-w><C-w> map <D-A-UP> <C-w>W
Re-Open Previously Opened File
I find myself using this literally all the time. It’s basically
cd - for your current window and allows you to quickly move back and forth between two files by pressing the Leader key twice.
nnoremap <Leader><Leader> :e#<CR>
Note that there’s already a way of doing it by pressing
Ctrl + ^, but I like <Leader><Leader> a lot more.
Show Matching Parenthesis
When selecting a parenthesis it will highlight the one matching:
Plugins are what make Vim great. There is a plugin for everything you might need, so being able to add and remove them easily is important.
Loading Plugins With Pathogen
Pathogen is, in my opinion, the best way to manage plugins currently available. It’s very simple, non obtrusive and really “just works”.
To install it, you can simply use the one liner below. If you don’t feel like copy and pasting
curl commands, just look up their very clear documentation.
mkdir -p ~/.vim/autoload ~/.vim/bundle && \ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
Now open your
.vimrc and add this line at the top:
From now on, you can add plugins in
~.vim/bundle/ and they will get loaded automatically!
Managing Plugins With Git
All the Vim plugins I’m going to mention here have a Git repository somewhere. This is great, because updating to the latest version can become as easy as pulling master.
To do this we’re going to use git submodules to reference another repository from our main repository. It’s actually pretty straightforward and will look like this on Github:
For every plugin, you need to run:
$ cd ~/.vim $ git submodule add git@source/pluginname.git bundle/pluginname
For instance if you want to add
vim-ruby located at https://github.com/vim-ruby/vim-ruby, you’ll do:
$ git submodule add email@example.com:vim-ruby/vim-ruby.git bundle/vim-ruby
Later on when you use your config on another machine or want to update your plugins, you’ll be able to use other commands like
git submodule init or
git submodule update.
Vim Plugins I Used
Here is a list of all the plugins I decided to include and why. I also added the advanced configuration I used and some examples when needed. This list is, of course, very subjective and will depend on what you expect your editor to do for you, but I feel like it’s a good starting point.
Very fast fuzzy search to open files quickly without many keystrokes. Get it here.
To ignore some files, you can use
When you create a new file, it’s not indexed by default. Here a way to reindex everything by simply pressing
noremap <Leader>r :CommandTFlush<CR>
Comment / Uncomment large blocks of code easily. Get it here.
After trying different plugins such as Airline, I decided on Lightline. This plugin gives you a light a configurable status line without too many pre-made decisions. Get it here.
A simple yet very useful tree explorer. Note that this is a project drawer and because of this has some limits when working with splits as explained in this article, however it fits the way I work and is an easy transition for anyone using an editor like Sublime or Textmate. Get it here.
Hit the right arrow to open a node:
Display hidden files:
Toggle display of the tree with
<Leader> + n:
nmap <leader>n :NERDTreeToggle<CR>
Locate the focused file in the tree with
<Leader> + j:
nmap <leader>j :NERDTreeFind<CR>
Always open the tree when booting Vim, but don’t focus it:
autocmd VimEnter * NERDTree autocmd VimEnter * wincmd p
Do not display some useless files in the tree:
let NERDTreeIgnore=['\.DS_Store', '\~$', '\.swp']
Autocomplete when using
tab. Get it here.
Git wrapper that I mostly use for its useful
:Gblame feature, allowing you to blame any line directly from Vim. Get it here.
Displays the diff in the gutter and allows you to do actions on it. I use it to see what I’ve just modified, but some some of its more advanced functions are also very good. For instance you can preview a change with
<Leader> + hp and revert any change on a line with
<Leader> + hr. It’s great! Get it here.
I don’t like my editor changing size while I’m using it, so I chose to always display the gutter - even when there are no changes.
Vim Multiple Cursors
Of course if you wanted to, you could have the same feature with native Vim features, but for now this plugin is really helpful to me.
Ruby Specific Plugins
I mostly use Ruby and Ruby on Rails for my projects, so I also added some plugins to help me with that.
I mostly use it to get better highlighting in the
Gemfile, but it also allows you to
bundle open quickly. Get it here.
end keyword when needed. Get it here.
Must have plugin for anyone using Rails. It just does so much it would be a shame. I particularly like how it allows you to jump from file to file with ease. You can really go from a controller to its spec, to its views and so on. Get it here.
Main plugin for dealing with Ruby. Get it here.
If you have plugins you feel belong here, feel free to share them in the comments or on twitter!
Since you scrolled this far, you might be interested in some other things I wrote:
- Building A Multiplayer 8 Bits Sequencer
- Trailing Slashes, Github Pages, Jekyll 3 & 404s
- How CSS Animations Can Break Your Tests
- Some Respect For Legacy Code
- Tips on Creating a Website From When I Was 12
- Enough With The Trolls
- Please Keep a Changelog For Your Open Source Lib
- The One Thing That Made Me Switch To Vim
- Startup & Tech Book Reviews