Vim Autocomplete Mini-Overview

Episode #5 in the A Decade of Vim Series

Tags:

by Jack Kinsella

@semicolonandson

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).

👉 Learn exactly how to build and grow your indie-hacker business into a passive-income machine.

Show Notes

Software

  • 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.

Dotfiles

Screencast.txt

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.

Subscribe Now:

These screencasts are aimed at ambitious programmers who need to take full responsibility for their codebases - especially as owners of small software companies.

  • Programming from a business-owner perspective - how to monitor your systems, write tests that'll let you sleep at night, and solve common technical issues faced by indie-hackers (like bot attacks, accounting systems, or financial fraud.)
  • Online marketing - scalable SEO, taming Google Analytics, profitable paid ads, email marketing.
  • Going one level deeper in the stack - when ultimate responsibility for a codebase lies on your shoulders, you need to understand software at a more fundamental level. Therefore we emphasize deep and timeless technical topics - like SQL, unix, vim, etc.