Episode #5

Vim Autocomplete Mini-Overview

I demonstrate six ways of doing autocomplete in Vim: based on strings in the current buffer, functions in the codebase (using Universal Ctags), file names in your project, full lines in the buffer, dictionary words, and - last-but-not-least - any class/constant/function in your codebase using the Language Server Protocol (LSP).

June 06, 2020

Show Notes


  • neovim - The vim fork I happen to use. I have no opinion on whether it's better or worse than regular Vim. This video used version 0.4.3
  • universal-ctags - A maintained version of ctags. Used to index ("tag") language objects that can be used in navigation and auto-complete.

Vim Plugins

  • vim-lsp - Language Server Protocol integration for Vim. Neovim will soon include a built-in LSP integration, so presumably most of the neovim community will switch away from third party plugins once the feature stabilizes.
  • asyncomplete.vim - The autocomplete ecoystem I used in the video. It's a bit buggy and perhaps others autocomplete engines are better (I haven't compared recently).
  • ALE linter - Provided the incidetnal linting you observed throughout the episode.



Transcribed by Rugo Obi

Next, let's look at the various ways that Vim can do autocomplete.

So the first is to autocomplete using a symbol or string in the current buffer. So, one line below, you see userSession. So I'm going to start typing that.

And now I press tab to select it. You can see in the overlay, there is a right-hand column with [buffer] inside it. That means the source for this autocomplete suggestion is the current buffer.

(And there you go, I’m going to get rid of it.)

The next type of autocomplete, a very common one, is to autocomplete based on functions or classes that are anywhere in the codebase. I'm doing this via universal Ctags, and basically remember we had a function called mark{X}, I think markAsSeen. Yeah, there you go.

So I can tab through it and you can see these are sourced from tags. So here we go. I've got this one here, It's going to turn red because I haven't imported this (but we don't need this here so I'm just going to delete it).

The third type of autocomplete worth looking at is a filename auto-completion. So what I can do here is -- just by specifying a file name -- Vim will autocomplete that. You can see here, we've got discounts.js suggested as a file. That's very handy for imports and that kind of thing.

Now, let's move on to the fourth and probably the most exotic type of autocomplete: line-based autocompletion.

Now, imagine for whatever reason, I'm creating a function that's sort of similar to the one below. And I want that line return configFor(..) and then I'll change the argument. So I’ll just start typing return, and then c and I press ctrl-x ctrl-l for line-based autocomplete. And now I can just grab that one there. And bingo, I've got it. I can go and change this as I please.

(I don't really want this so I'm just going to delete all that.)

Now the very last type of autocompletion here is dictionary-words autocompletion.

So let's say I want to print out the message 'Vim is awesome', but I don't want to type the rest of the ‘awes’, I'm lazy like that. So here I press ctrl-x ctrl-k and populate it with words from the dictionary, just like that.

Oh wait, I nearly forgot the most important of all the autocomplete functionalities in a modern Vim setup: the Language Server Protocol autocomplete. So to demonstrate that I'm going to open up my Product class from the Ruby on Rails side of my codebase. And then in a vertical split, I'm going to open up a completely blank new Ruby file. Now, in the left pane, I'm going to instantiate a product in Ruby, which is Product.new. Next, I'm going to call . on this product instance. And as you can see all these methods that are available in the Product class in the right-hand pane are now available, and sometimes they're even arguments like the defaults given within this overlay.

See you next time.