Tab Completion in Vim and Neovim

Vim's command line tab completion is lousy. But you have options. Let's start by generating some useless files that help illustrate the problem:

#   Filename: mkfiles
let n=1
let max=50
while [ ${n} -le ${max} ]
    if [ ${n} -lt 10 ]
        touch "f0${n}of${max}"
        touch "f${n}of${max}"

This generates fifty numbered empty files:

$ ls
f01of50  f07of50  f13of50  f19of50  f25of50  f31of50  f37of50  f43of50  f49of50
f02of50  f08of50  f14of50  f20of50  f26of50  f32of50  f38of50  f44of50  f50of50
f03of50  f09of50  f15of50  f21of50  f27of50  f33of50  f39of50  f45of50  mkfiles
f04of50  f10of50  f16of50  f22of50  f28of50  f34of50  f40of50  f46of50
f05of50  f11of50  f17of50  f23of50  f29of50  f35of50  f41of50  f47of50
f06of50  f12of50  f18of50  f24of50  f30of50  f36of50  f42of50  f48of50

If I run vim -u NONE (deliberately starting with no configuration file), I get something akin to old command line vi: among other things, it has no Status Line. If I then type :e f<TAB>, I get the following result:

First filename

If I hit <TAB> again, I get:

Second filename (this will take a long time)

You can probably guess where this is going. If you keep hitting <TAB>, Vim will cycle through all 50 files.

One solution is to use netrw. On almost all versions of Vim these days, this is autoloaded at start-up, and saying :e . which will open a window like the following:

netrw file explorer interface inside Vim

You can then use your arrow keys or standard Vim JK keys to navigate to the file you want and open it by pressing enter. Note that this IS a plugin, so if you use the previously mentioned vim -u NONE, it won't be available. If you have the NERD_tree.vim plugin installed and you type :e ., you'll get this instead:

NERDtree file explorer interface inside Vim

The experience is very similar to netrw. But we're here to talk about tab completion, so - carrying on. If you have Neovim installed, the experience is much improved: if you type :e f<TAB>, Neovim uses the Status Bar (which is on by default, but if you've turned it off Neovim will re-instate it temporarily) to show possible completions. This is still mostly backwards compatible so you still have to <TAB> through your choices, but now you have some visibility on the number of files, and the use of your arrow keys.

tab completion in Neovim

But better than that - and it works in both Old Vim and Neovim - is to add this to your ~/.vimrc: set wildmode=longest,list. Tab completion will now behave essentially like Bash, only completing as far as it can without ambiguity, and then prompting you with your options if you hit <TAB> again:

'set wildmode=longest,list' tab completion in Vim

In the image above, I started with :e f<TAB><TAB>, then typed 0<TAB><TAB> and finally 2<TAB>. This is my preferred mode, now set permanently in my ~/.vimrc (and ~/.config/nvim/init.vim is a soft link to ~/.vimrc).