path setting for c++ include headers for vim

If there's a limited number of locations, a simple conditional in ~/.vimrc will do:

if isdirectory('/usr/include/c++/4.6')
    set path+=/usr/include/c++/4.6
elseif isdirectory(...

If you have a lot of different systems, and don't want to maintain all variations in a central place, you can move the system-dependent settings to a separate, local-only file, and invoke that from your ~/.vimrc, like this:

" Source system-specific .vimrc first.
if filereadable(expand('~/local/.vimrc'))
    source ~/local/.vimrc
endif

I recently had the same problem, so here is my solution for documentation purposes:

1) I added the following to my .bashrc:

# add additional search paths to vim.
VIM_EXTPATHS="$HOME/.vim.extpaths"
if [ ! -e "$VIM_EXTPATHS" ] || [ "/usr/bin/cpp" -nt "$VIM_EXTPATHS" ]; then
    echo | cpp -v 2>&1 | \
    awk '/^#include </ { state=1 } /End of search list/ { state=0 } /^ / && state { print "set path+=" substr($0, 2) "/**2" }' > $VIM_EXTPATHS
fi

2) I added the following to my .vimrc:

" add extra paths.
let s:extpaths=expand("$HOME/.vim.extpaths")
if filereadable(s:extpaths)
    execute "source ".s:extpaths
endif

On my system, the contents of the .vim.extpaths file are as follows:

set path+=/usr/lib/gcc/x86_64-linux-gnu/8/include/**2
set path+=/usr/local/include/**2
set path+=/usr/lib/gcc/x86_64-linux-gnu/8/include-fixed/**2
set path+=/usr/include/x86_64-linux-gnu/**2
set path+=/usr/include/**2

The **2 means that ViM will search two directories deep inside these directories. Now gf will find all the C++ headers I need. If you increase the depth, searches will take a lot more time, so don't set this number too high.

@note: for #include <chrono>, ViM will go to /usr/include/boost/chrono, which, funny enough, is a directory. I wonder why go file will open a directory, maybe this should be reported as a bug. To get to the correct chrono header you have to type 2gf.