Git Gud at Vim 4: Plugins and Configs

November 06, 2018

This is part four of a four part series. To see all the articles, click here.

Our quick jaunt through some of Vim’s features is coming to a close. In this article, I’d like to explore some of the settings that I have in my .Vimrc file to help you get set up in an optimal way. I’d also like to highlight some hidden Vim magic by touching on some plugins that both benefit and extend that functionality.

In my opinion, everyone should start a bare file, and add slowly as they realize what they need.

When I started out I used a “starter pack” style configuration someone else had put together, and as a result I didn’t learn a lot of the neat abilities of Vim that I’ve outlined in previous articles because I didn’t know what was Vim and what was a plugin.

I also don’t recommend using plugins to “make Vim an IDE” or “make Vim behave like [insert other editor here]”. This also will take you down a path of ignoring the strengths that Vim posesses and frustrate you as you try to force Vim to behave in a way it wasn’t designed. Vim is designed in a manner consistent with *NIX philosophy: do one thing, and do it well.

I intend to opinionatedly recommend a few useful plugins that fit this strategy. This is not going to be a comprehensive list of all useful plugins or cool plugins, there are other websites for that kind of thing.

If you want to just look at my current config file (I use neovim so it’s located usually in ~/.config/nvim/init.vim), look here. My configuration is opinionated and specific to me. I write a lot of markdown, HTML, and elixir for fun. I would advise in general, if you don’t know what something does, don’t add it to your configuration.

I wrote an annotated version of the current configuration file here if you need more commentary on what everything does.

Config Essentials

There are a number of “sanity” settings that I consider useful for most Vim usage. I’ll sum them up here:

  " turn off compatibility mode with vi
  set nocompatible
  set backspace=indent,eol,start

  " turn on filetype plugins
  filetype plugin indent on

  " turn on colors
  set termguicolors
  syntax on

  " ignore case in search
  set ignorecase
  set smartcase

  " enable buffers to be hidden
  set hidden

If you have questions about what these do, you can always search :help for them.

You can also use tpope’s vim-sensible plugin to achieve most of this in one plugin.

Language Files and Omnicomplete

Vim ships with compatibility for many languages, but you may want to add an updated package for improved syntax highlighting, and more importantly: omnicomplete functions! (Fun to say and fun to use!)

  1. <c-p> to complete in file to complete a word from in the file.
  2. <c-x><c-f> can autocomplete files on your filesystem… I think it’ll even work over ssh/ftp!
  3. Using <c-x><c-o> I can view the methods available in python on the objects I’m referencing. Notice how the omnifunc even puts the documentation in a buffer split above the file? Awesome!

Vim ships with a capable completion engine that you can read about in :help ins-completion. You can complete words, spell check and synonyms, whole lines, filenames, tags… or using the omnifunc. You may need to add an updated language file and install some external packages to make an omnifunc work properly, but when it does it can provide information like all the methods available on an object.

Pro tip: For some reason, :help omnicomplete doesn’t find the right help file. Try :help compl-omni.

Plugins

plug-vim has an awesome async update!

Vim plugins are (mostly) written in vim script, a sort of lisp-y like language. As the editor has been around for a long time, there are a whole range of plugins to solve nearly any problem that you face.

In fact, some core Vim functionality is implemented as a plugin, including the very awesome netrw.

One of the main reasons to learn Vim is that it’s everywhere. It is worth noting… if you customize your Vim instance, those customizations won’t be present on every box you SSH into. It is really valuable to know how to use Vim with no customizations present, so that in that situation you aren’t totally lost.

I use plug-vim to manage my plugins. There are several excellent ways to download and load plugins, but this is my favorite. It lets you define all your plugins and easily keep them up to date.

Now that we have a way to download and update our plugins, let’s look at a few:

Core: Must Have Plugins

These plugins or configurations I think are very useful no matter what kind of text files you are working on.

fugitive.vim

Showing some of fugitive’s time travel at work

I use this plugin every day. To be honest, I probably use 20% of its functionality, but I use the :Gblame command nearly constantly. It allows me to travel back in time through code (that has a git history) to see who added what work when and why. This allows me to see who was the last person to touch this code, to find out from their commit message why they did it, and follow up with them if I have more questions.

For those who don’t need to time travel… fugitive.vim also has :Gbrowse, an awesome command that opens the current file or range on github.com for easy sharing. I use this almost every day to send someone a question or a fragment of code over a chat program like slack.

fzf

This plugin is the main way I open files in vim. Those who have used “fuzzy finder” interfaces that have become common in every editor will feel right at home. It consists of an executable and a Vim plugin. I use fzf’s CLI executable in addition to the Vim plugin, it’s awfully nice to have the same interface to find and select files in and out of my editor.

FZF has the ability to help you do many other things, like switch buffers and git branches, but I honestly only use it for selecting files. It is really nice to have an extendable interface that is useful in and out of vim though.

targets.vim and surround.vim

Both targets.vim and vim-surround extend Vim with new text objects and motions that make working with text surrounded by delimiters, parameters, or strings easier. Since most code has blocks, tags, ({["''"]}), of all kinds… it is worth looking into.

Once again, these plugins don’t drastically change Vim’s behavior, just extend and enhance the superpowers already there.

tpopery

It turns out that tpope has written so many plugins… I might as well just list him as a sponsor of my work. I’ve already mentioned fugitive and surround.vim, I’m just going to list just a few more that I use constantly:

  • commentary provides an operator for commenting out code.
  • endwise automatically ends certain code structures. (i.e., adds an end for ruby)
  • eunuch lets you save, delete, move, or sudo save files you already have open in your editor.
  • ragtag provides operators and motions for web templating languages. (HTML tags, get it?)
  • repeat extends the repeat operator . to work in more places and with more plugins.
  • unimpaired adds a host of useful bindings and toggles for common Vim actions.
  • vinegar cleans up :netrw views to make browsing easier.

Cool: Neat Stuff

Gundo: Wait, go back!

Vim doesn’t just store a straight undo/redo path… it stores a tree of states. If you use a normal editor and undo a few changes then make some new changes, the changes you made previously are lost. Vim keeps track of all your changes, and you can traverse this tree (natively) using the g- and g+ operators, as well as the :earlier and :later commands which take arguments. Several times I’ve been saving and re-saving a config file to fix some server, and then lost some critical part of the configuration in all the changes. :earlier 1h throws the file back to its state from one hour ago, and saved the day!

If these native commands aren’t clear enough, there’s an awesome plugin in gundo.vim. Gundo gives you the ability to visualize this tree of changes and move around it with ease.

Emmet.vim

Emmet.vim at work. Notice how I can undo the operation of expanding the abbreviation, adjust it, and then expand it again until I get what I want.

Emmet has been a constant companion of my professional career for a long time. The first time I used it it was known as Zen Coding, and I used it as a plugin for Textmate or Coda.app. It’s nice that the editor ecosystem is robust enough that I can use the same cool interface in multiple editors, including my beloved Vim.

In short, Emmet is a shorthand notation for generating HTML and CSS. I used to use it a lot more than I do now, but it’s still been handy even while writing this series of articles. When I want to put in a photo in a figure, I can write: figure.right>img+figcaption{This will be the caption}, then press y<leader> to generate:

<figure class="right">
  <img src="" alt="">
  <figcaption>This will be the caption</figcaption>
</figure>

If you don’t really write HTML, then a snippets engine like ultisnips may be a better option for you.

Conclusion

There’s honestly too much to cover. There are so many ways that you can customize and tweak vim, tons of colorschemes, autocomplete functions, etc. I just hope that this quick peak gives you a hint as to how I use Vim, and ways that you can use and customize Vim for your maximum effectiveness.

If you have questions or need help customizing your setup, drop me a line! Let me know how I can help!

Read More:

  • 2018-11-07 11:44:48 -0600

    Update blog post with more info

  • 2018-11-06 22:42:25 -0600

    Final Vim Post: First Version