Search This Blog

Learn Vim Fast: Useful Plug-ins

Now that we've learned quite a bit about Vim, from the basics to movement and editing, it's time to really put Vim to work for us by extending it with some very useful plug-ins. Vim is an extensible editor, and a vast array of plug-ins are available to make Vim even more powerful for editing code quickly and efficiently. We'll take a look at some of the most useful plug-ins and how they can accelerate your coding even further, but first we need a way to efficiently manage all of these new plug-ins. Not surprisingly, a plug-in manager is available for just this purpose.

Managing Plug-ins with Vundle

The first thing you'll want to do before looking around for any Vim plug-ins is install the plug-in manager Vundle (short for Vim bundle). You can find it at GitHub under VundleVim/Vundle.vim, and the installation is as simple as can be, as long as you are comfortable with git. You do use git, don't you? Here's the git command for installing Vundle:
$ git clone ~/.vim/bundle/Vundle.vim
Once it's installed, you can start adding plug-ins to your Vim configuration file. I'll show the entire configuration section of my .vimrc file, and then we can go through each of the plug-ins and their related configuration settings. Here they all are:
set nocompatible
filetype off

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'

"Run :PluginInstall from Vim to install"
Plugin 'mileszs/ack.vim'
Plugin 'ggreer/the_silver_searcher'
let g:ackprg = 'ag --nogroup --nocolor --column'

Plugin 'ctrlpvim/ctrlp.vim'
map y :CtrlPBuffer

Plugin 'tpope/vim-commentary'
Plugin 'tpope/vim-endwise'
Plugin 'tpope/vim-surround'
Plugin 'tpope/vim-bundler'
Plugin 'tpope/vim-rails'
Plugin 'tpope/vim-rake'
Plugin 'pangloss/vim-javascript'
Plugin 'kchmck/vim-coffee-script'
Plugin 'othree/html5.vim'
Plugin 'vim-airline/vim-airline'
Plugin 'jiangmiao/auto-pairs'
Plugin 'scrooloose/syntastic'

call vundle#end()
filetype plugin indent on

let g:airline#extensions#tabline#enabled = 1
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

if executable('ag')
  " Use ag over grep "
  set grepprg=ag\ --nogroup\ --nocolor\ --column

  " Use ag in CtrlP for listing files. Lightning fast and respects .gitignore "
  let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'

  " ag is fast enough that CtrlP doesn't need to cache "
  let g:ctrlp_use_caching = 0

  " bind \ (backward slash) to grep shortcut "
  command -nargs=+ -complete=file -bar Ag silent! grep! <args>|cwindow|redraw!
  nnoremap \ :Ag<SPACE>

" bind K to grep word under cursor "
nnoremap K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>
" bind Tab and Shift-Tab to cycle through buffers "
nnoremap <Tab> :bnext<CR>
nnoremap <S-Tab> :bprevious<CR>
Now that looks like a lot, but trust me, it's worth it. We won't go into all of the gory details, but this setup makes things in Vim work really slick. Once you get comfortable with it, you'll probably go out hunting for more plug-ins. For now, put all of this at the beginning of your .vimrc file, run :source ~/.vimrc (or restart Vim), run :PluginInstall, and watch all of the plug-ins get downloaded and installed:

Gvim plug-in installation

Once you restart Vim after the plug-in installation, it should have a new bar at the top:

Gvim new airline bar

That new bar is due to the airline plug-in, but we'll get to that in a minute. Let's step through all of these new plug-ins from the beginning of the list. The very first plug-in is the one for Vundle itself and doesn't need more explanation, but the rest do.

The Silver Searcher
It doesn't take long for any programming project to outgrow one file, and once that happens you'll want a fast and easy way to find things in different files. This is where the Silver Searcher comes in. First, we need a plug-in to enable the use of external command line search tools in Vim. The ack.vim plug-in is exactly what we need, and then we can bring in the Silver Searcher for wickedly fast searching right from within Vim. The line after the Silver Searcher plug-in sets 'ag' as the default search tool in Vim. (Ag is the abbreviation for the element silver. Clever, right?)

Now you can type :Ag <search_term> to search for any term in the current working directory, but we can do even better than that. Towards the end of the configuration file, we map \ to ':Ag ' so we can quickly start a search using a single key press, and then we map K to search for the word under the cursor. These key bindings make searching files really painless. You can quickly look up the definition of a function call or find all of the uses of a function with a quick keystroke. For example, in our demo statistics code, if I move to the stdev function and type K, I find that the function is used in the current file and an additional one called statistics.rb:

Gvim silver searcher demo

I can move the cursor up and down and hit <Enter> to open either of these files at the location shown, or I can cancel the window by typing <Ctrl>wc. If I had opened a file, I can close the window by typing <Ctrl>wj<Ctrl>wc. The cursor has to first be moved back to the bottom split window with <Ctrl>wj before we can close it.

The search results have brought up a Vim feature we haven't seen yet—split windows. The Vim editor can open split windows either vertically or horizontally, and in this case it was a horizontal split. Typing <Ctrl>w will enable you to move the cursor between windows with the normal cursor movement keys or arrow keys. To create your own split window, type :split for a horizontal split or :vsplit for a vertical split. You can move around the windows and open different files in each one. I don't use split windows very often, but some people find them quite useful. Anyway, let's move on to something even more cool than fast searching.

This plug-in builds on top of the silver searcher, and it is an invaluable addition to Vim. As you would expect, the key sequence for it is <Ctrl>p, and typing it brings up another split window with a partial list of files in the current directory and sub-directories. As you type a search term, the list of files narrows down in real time. At any point you can select a file and hit <Enter> to open it. To cancel the search, just hit <Esc>.

Gvim CtrlP demo

The cool thing about CtrlP, other than its speed, is that it does a fuzzy search for file names. That means, you shouldn't type any special characters or spaces. Just type a stream of letters for your search, and the right file will get selected in short order. For example, if you're looking for a Rails view called app/views/users/index.html.erb, you can type <Ctrl>pviewuser, and most likely, the view you're looking for will already be the one selected and ready to open with <Enter>. CtrlP is also good about selecting files that you are most likely to want, given the file you are currently in. If you are in a controller file, and you type just the name of the controller, the first file selected will be its model. If you are in a model and type its name, you'll get its test file. Other related files are also close by in the list. It feels like magic, and CtrlP is probably the single most useful plug-in for increasing my productivity with Vim.

This one is pretty simple. It allows you to easily comment or uncomment lines. Type gcc to comment out the current line. Type gc<movement> to comment out a set of lines based on <movement>. It's a toggle, so do the same thing to uncomment lines.

This plug-in automatically adds end lines for most structures in Ruby, making it easier to code programming structures.

Gvim endwise demo

If you need to wrap code in other characters, like parenthesis, quotes, or HTML tags, this is the plug-in you need. Check out the GitHub page for all of the different ways you can edit HTML files with ease.

Vim-Bundler, Vim-Rake, and Vim-Rails
These are some plug-ins that are great to have if you code in Ruby on Rails. Vim-bundler gives you access to bundle commands from the Vim command line as well as syntax highlighting in gem files. Vim-rake gives you access to rake commands. Vim-rails adds a ton of features for Rails developers such as improved navigation of Rails projects, enhanced syntax highlighting, access to the rails command, and easy extraction of partials and concerns. Check out the Github page and the Vim help file for all of the gritty details.

This plug-in simply provides improved syntax highlighting and indentation for JavaScript files.

In addition to normal syntax highlighting, this plug-in adds compile, execute, and lint features for working with CoffeeScript files. Check out the GitHub page for a full run-down of features. I mostly use it for the syntax highlighting in Rails projects.

This is yet another syntax highlighting plug-in. Vim comes preloaded with quite an extensive syntax highlighting pack, but some plug-ins offer improvements that are hard to go without. Luckily, it's easy to add more plug-ins with Vundle.

This is a cool plug-in. It adds the bar at the top of the Vim window that we saw at the top of this post with the names of the files that are currently opened. Along with the bindings for <Tab> and <S-Tab> that were added to the configuration file that switch forward and backward through these open files, Vim-airline basically adds tabbed editing to Vim. Being able to switch between files with the <Tab> key instead of <Ctrl>-<Tab> like in most other editors is another nice feature of Vim. A little less friction really adds up over time.

Gvim airline demo

Coupled with CtrlP, it becomes quite easy to open and move through a ton of files in one Vim session. At this point it would be good to go over a couple other commands for managing all of these open files. To get another list of the open files, type :buffers.

Gvim buffers command

Down the left side of this list is a numbered identifier for each buffer. The next column contains a '%a' next to the current buffer and a '#' next to the previous buffer. You can move back to the previous buffer by typing <Ctrl>6, or if you precede that command with a number, you'll move to the buffer with that ID. For example, a 5<Ctrl>6 will move you to the fibonacci.rb file. If you have too many files open and want to get rid of some of them, move to the buffer you want to close and type :bd, for "buffer delete." This doesn't actually delete the file, only the buffer in Vim. With this knowledge, it's easy to move through multiple files very quickly.

This is another simple plug-in that automatically inserts brackets, parentheses, and quotes in pairs when you type the opening character. It will also delete in pairs. It's another nice little feature that is common in most modern code editors.

Okay, syntastic is quite extensive, so I'll mostly refer you to the GitHub page for how to use it. Briefly, it's a syntax checker that works with a huge number of languages. I have it set up to automatically check files on open, but not on :wq because I don't want to be stopped by a syntax checker when quiting Vim.

I've now gone on for long enough, so I'll close out this mini-series on Vim by saying that there is much more to Vim than what I've covered here. The commands we've covered are the ones I use the most and will provide a great foundation. If you learn them well, you'll be well on your way to editing at the speed of thought. Vim is a powerful text editor, and even the small set of plug-ins in this post extend its capabilities greatly. There are tons more plug-ins out there. For a good site to start exploring more, check out Vim Awesome, and I wish you luck in becoming fluent with this deceptively elegant editor.

No comments:

Post a Comment