Vim Completion Based on Buffer Name

autocomplete, vim

Solution

Good idea; restricting the candidates to loaded buffers whittles down the list of candidates, and also allows for a match anywhere in the file path.

To get this, a custom insert-mode completion must be implemented. The file names can be retrieved by iterating `bufname()` over all buffer numbers from `1` to `bufnr('$')`. I've just implemented this as the BufNameComplete plugin. I hope that suits your needs (and mine).

Problem

In Vim, I often find myself in a situation where I want to complete a filename of a file that I'm editing in a separate buffer. Suppose I have two buffers open: foo.cpp and bar.h. In foo.cpp I need to #include "bar.h". I want to do something like this: foo.cpp ``` #include "b<complete filename to bar.h> ``` According to Vim documentation, I am able to use CTRL+x CTRL+f to do filename completion, but that seems to rely on Vim having a CWD equivalent to bar.h's parent directory. That's not always possible. Given that I have bar.h open in a separate buffer, is it possible to autocomplete based on buffer name?

Original source