Vim essentials
I’ve always been curious about how cool programmers use vim so efficient. Many screencasts and videos from conferences show that some programmers prefer vim over real IDEs. I decided to master vim, there should be something charming - so many people can’t be wrong. Here are 21 Reasons to Learn Vim. So, let’s get it started.
##Vim in 5 sec
The only thing you shouldi know is: hit Esc and :q!
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
More info about it.
##How to start
I started to poke around and realized that there is no one source which can describe how to do something like that:
Ok, we’re motivated to do something cool. Even though, vim is a crossplatform solution, this article aims to cover MacOS and iTerm2. I like this stack and use it all time. So, first of all, we need to update default vim 7.3 to 7.4. We could use brew install vim
but for macos, there is a better way - using macvim. Macvim is the latest vim compiled with proper flags. It adds a few benefits like system clipboard support.
So, all special vim features are controlled by a plugin system. The first step for plugin installation is to pick a plugin manager. Vim has a bunch of plugin managers, but as I understand there is the most popular one - Vundle.
The installation procces is pretty easy:
Then, you need to create a file ~/.vimrc
with the following content:
This is basic vim configuration. Now we are able to modify it to customize vim view and behaviour to meet our needs. There is a bunch of cool features which you have to turn on in the beginning. You just need to add following lines to your ~/.vimrc
file.
set mouse=a
allows you to work with vim using your mouse;set number
shows line numbers;set relativenumber
another cool setting which can show line numbers relative to the line you are currently on; This setting can be combined withset number
. In this case, current line will have absolute value, but all others relative.set clipboard=unnamed
helps to work with MacOS clipboard usingy
;- A few options to manage indentation:
Plugins Installation
Now we can add some plugins to extend standard view and functionality. I found an awesome site with a catalog of all available plugins http://vimawesome.com/. The plugin installation is really simple. You need to copy the string which looks like Plugin '***/***'
and paste it to ~/.vimrc
bellow the line with text "Plugins go here
. After that, you may install all plugins: open vim, and type :PluginInstall
.
Let’s start with essentials.
NERDTree
The NERD tree allows you to explore your filesystem and to open files and directories. It presents the filesystem to you in the form of a tree which you manipulate with the keyboard and/or mouse. It also allows you to perform simple filesystem operations.
This is an essential plugin which helps to work with project tree.
A few tips and trick:
- To make it more conviniet, you can add
map <F5> :NERDTreeToggle<CR>
to your~/.vimrc
. It allows you to show NERDTree by hitting F5; - Hit
r
to refresh file tree.
ctrlp.vim
Full path fuzzy file, buffer, mru, tag, … finder for Vim.
Ctrl+p shows panel like that:
Fugitive
This plugin helps you to manage git from vim.
Airline
Airline is a customizable status bar and tabline.
The installation process is pretty tricky. First of all, you need to add the plugin:
Then, add the following configuration lines:
We just did basic configuration of airline. It’s almost done, but you’ll have font issues. To solve them, you need to checkout powerline/fonts and run ./install.sh
. The next step is iTerm2 configuration - go to Preferences -> Profiles -> Text and set Regular Font and Non-ASCII Font to Inconsolata-g for Powerline
.
Easymotion
EasyMotion provides a much simpler way to use some motions in vim. It takes the
out of w or f{char} by highlighting all possible choices and allowing you to press one key to jump directly to the target.
Theme
By default, vim inherits system colors and it doesn’t look too awesome. To change it, there are hundreds of themes. I used vimcolors.com to pick a theme.
The installation process is simple. Pick a theme and go to its github page. Copy the author name and project name and paste to ~/.vimrc
Then, you need to add this line to apply theme:
Summary
In this article, I described all steps which I went through to make a vim view look more than a simple text editor. I mentioned only a few plugins out of hundreds, but it’s extremely easy to find and install new ones by yourself. The next part of this article is going to collect all essential shortcuts and commands which you should know to be productive in vim.