/ ViM

Completion tricks in ViM

Somewhere in 2000, I got infected with the ViM virus :) By accident, I started ViM in a terminal and did not know how to actually quit the program. The only way I knew how to get my terminal back was to switch to another terminal, login and kill the ViM process....

This first experience might have been a bit negative, but I am pretty sure it is the experience that pretty much everybody will have had (or will have) with ViM :)

I did hear some good stories about ViM, so decided to give it another try after my previous experience and got me a book to learn some more about this editor. From that moment on, I was hooked on the editor and it is the one editor I would use all the time :)

Even after 17 years, I am still learning new things about ViM that blow my mind. After learning some of these new functions, sometimes really wondering how I ever was working without these functions, or how much time they could have saved me!

Today, this feature is the different completion functions that ViM provides:

  • Word completion
  • Line completion
  • Filename completion

The word completion (ctrl-p and ctrl-n) I have been using a lot already. The other two were new to me :)

Line completion

Recently, for a specific type of task, I had to often copy one of the previous lines. Of course, there are more ways to achieve this and one approach is to set a marker at the current cursor position (e.g. m a), go to the line you need to copy, yank this line (with y y), go back to the marked cursor position (with ` a) and paste the yanked line.

Turns out that there is a much better way in ViM for this, namely the line completion feature. To complete your current line based on other lines in your file, you do the following:

  • Enter the beginning of your line and stay in insert mode
  • Press ctrl-x. This will show you a message in the status line about the ^X mode.
  • Now press ctrl-l. This will tell ViM that you want to use the Line completion mode.
  • Use ctrl-p and ctrl-n to scroll through the possibilities

In your terminal, you will see the following:
Demonstration of line completion in ViM

Filename completion

Very often when creating some help or manual for something, I need to refer to specific files. Often I remember the filename, but sometimes I want to be sure (was the file in CamelCase, or was it with underscores, or was there a space somewhere...)

My solution till now was straightforward: open another terminal, go to the directory holding the file and check :) Turns out, there is another easy way in ViM also, namely filename completion.

This works as follows:

  • Type the beginning of the file or path
  • Press ctrl-x. This will show you a message in the status line about the ^X mode.
  • Now press ctrl-f. This will tell ViM that you want to use the filename completion mode.
  • Use ctrl-p and ctrl-n to scroll through different files

In your terminal, you will see the following:
Demonstration of line completion in ViM

Besides these two main functions, you can also do the completion based on dictionary or thesaurus by using a differetn control combination after the initial ctrl-x.

Although these features have been in ViM for quite some time now, I never found them before. Makes me wonder, what other features are there for me to still discover... :)

Do you have any great ViM features that have made your life easier, let me know in a comment!

Guido Diepen

Guido Diepen

Senior Data Scientist at T-Mobile NL | Fascinated with new technologies and tools | Trying to automate the boring bits of life | Love to travel

Read More