vimtricks.wiki Concise Vim tricks, one at a time.

How do I prepend a value to the front of a comma-separated Vim option like path or runtimepath?

Answer

:set path^=./src

Explanation

Vim's :set command supports three operators for modifying list-style options: += appends, -= removes, and ^= prepends. Because options like 'path', 'runtimepath', and 'wildignore' are searched left to right, prepending with ^= gives your new value the highest priority.

How it works

  • :set option+=value — appends value to the end of the list
  • :set option^=value — inserts value at the beginning of the list
  • :set option-=value — removes value from anywhere in the list

For 'path', Vim searches directories in order when you use gf, :find, or tab-completion on :e. Prepending a project-specific directory ensures it is checked first, before the generic fallbacks.

Example

Add the src/ directory at highest priority so :find finds project files immediately:

:set path^=./src

Before: path = .,/usr/include,,

After: path = ./src,.,/usr/include,,

Similarly, to load your local plugin overrides before the system runtime:

:set runtimepath^=~/.vim/local

Tips

  • Check the current value with :set path? or :echo &path
  • The ^= operator is also valid for 'tags', 'packpath', and any option documented as a comma-separated list
  • In Neovim, use :lua vim.opt.path:prepend('./src') for the equivalent Lua API

Next

How do I replace each match with an incrementing number using a counter variable in Vim?