Vim file name completion relative to current file

pathvim

In Vim, when I am in insert mode, and I want to use file-name completion, I want the search path to be relative to the current file, not the current working directory. For instance, if in insert mode I type the following:

./^X^F

The completion menu is showing me files in the :pwd, but I would rather see the files in %:p:h listed.

Is there an option that I can set to fix this, and if not what do you suggest as a workaround?

Best Answer

If you

:set autochdir

you'll get the behavior you desire. However, if you need to keep the working directory (e.g. to easily open other project files), you'd have to save / restore the CWD with autocmds:

:autocmd InsertEnter * let save_cwd = getcwd() | set autochdir
:autocmd InsertLeave * set noautochdir | execute 'cd' fnameescape(save_cwd)
Related Question