vim - completion

Vim completion

Let Vim do the typing by George Brocklehurst (@georgebrock)

Even if you’re good at it, typing is still slower and more error-prone than not typing. This presentation will explore a few ways to avoid typing by getting Vim to do the typing for you.

Vi, or Emacs?

As modal editors, Vi and Vim tend to avoid modifier keys in favour of modes. The behaviour of a key stoke is dictated by the editor’s mode. This is different from Emacs-like editors, where behaviour is modified by pressing modifier keys instead of changing mode.

Everything in this presentation uses modifier keys in insert mode: the commands might feel more like Emacs commands than Vim commands, but they are useful, and have their place in the Vim editing philosophy.

Vim users like to be efficient, and part of that efficiency is making changes as atomic edits. An atomic edit can be repeated (using .) or undone (using u). A single insert updates the . register, making the inserted text readily available. Sometimes, when we’re inserting text that includes something Vim’s already seen – either because it’s in one of our files, a symbol in our program, or something from one of our registers – it’s more efficient not to change mode, and to reach for a modifier key instead.

A few of my favourite things

Insert text from a register

`ctrl+r`

Last inserted text

`ctrl+a`

There are three common cases where we want to insert something Vim’s already seen as part of a larger insert:

  1. Insert from a register, using [Ctrl][r] register.
  2. Insert the same text as the previous insert, using [Ctrl][a]. This is similar to [Ctrl][r][.]
  3. Complete a partially-typed word or phrase, which will be the focus of the next few sections.

Completion

Simple word completion

[Ctrl][p]

or

[Ctrl][n]

  • [Ctrl][p] finds the previous matching completion for the partially typed word.

  • [Ctrl][n] finds the next matching completion for the partially typed word.

  • [Ctrl][p] is usually more useful, because you’re more likely to be looking for a word you just used than a word you’re about to use.

  • Where Vim looks for completions is controlled by the ‘complete’ setting. The default is .,w,b,u,t,i, which means Vim will look in:

    1. The current buffer.
    2. Buffers in other windows.
    3. Other loaded buffers.
    4. Unloaded buffers.
    5. Tags.
    6. Included files.

    I also like to add kspell to the end of the list:

    1. The active spell checking dictionary, when spell checking is enabled.

    Tag completion

[Ctrl][x][Ctrl][]]

Sometimes, you know that the word you want to complete isn’t just any old word that appears in your file or files; you know you’re trying to complete a symbol from your program.

If you’re using ctags, then you can complete a word in your tags file using [Ctrl][x][Ctrl][]]

The initial [Ctrl][x] puts Vim into a completion mode, which is a sub-mode of insert mode. We’ll see this prefix again.

When the completion menu appears, you can use [Ctrl][p] and [Ctrl][n] to navigate through the options.

Filename completion

[Ctrl][x][Ctrl][f]

Filename completion will complete paths relative to the current working directory, similar to tab completion in Unix shells.

Context-aware word completion

[Ctrl][x][Ctrl][p]

or

[Ctrl][x][Ctrl][n]

Repeat the command to continue adding matches, e.g. in a document that contained the string Hello world, you could type Hel[Ctrl][x][Ctrl][p] to complete `Hello`, and then immediately repeating [Ctrl][x][Ctrl][p] would add world.

At any point, if there are multiple possible matches, you can use [Ctrl][p] and [Ctrl][n] to navigate through the options.

Context-aware line completion

[Ctrl][x][Ctrl][l]

As with word completion, you can repeat the command to continue adding matches.

Language-aware completion

[Ctrl][x][Ctrl][o]

If you have Vim’s filetype plugins enables, you will have access to omnicomplete for some languages. This completion style will complete language keywords and built in classes or functions.

For example, in a Ruby file, typing “Hello world”.cap[Ctrl][x][Ctrl][o] would complete to “Hello world”.capitalize or “Hello world”.capitalize!.

Insert mode completion : [Ctrl][N]

Omni completion : ^O ^N ^P

Completion tips :

  • vim is very completion friendly
  • just use <Tab> on command line
  • for filenames, set ‘wildmenu’ and ‘wildmode’ (I like “list:longest,full”)
  • :new ~/dev/fo<Tab> - complete filename
  • :help ‘comp<Tab> - complete option name
  • :re<Tab> - complete command
  • hit <Tab> again to cycle, CTRL-N for next match, CTRL-P for previous


Links to this note