How do I prepend a value to the beginning of a Vim list option like path or runtimepath?
Answer
:set path^=./include
Explanation
Vim's :set command supports four operators for modifying list-type and string options: += (append), -= (remove), ^= (prepend), and = (replace). The ^= operator is the least commonly known — it adds a value to the front of the option rather than the back.
How it works
For string options (including comma-separated lists), :set {option}^={value} inserts the value at the beginning of the current option string. This is important when order matters:
path— Vim searches directories in order, so prepending ensures your directory is checked first withgfand:findruntimepath— plugin/script directories loaded in order, so prepending guarantees early loading
For number options, ^= instead multiplies by the given value (the ^ represents power/exponentiation).
Example
Suppose path is currently set to .,/usr/include:
:set path^=./include
Now path becomes ./include,.,/usr/include. Vim will search ./include first when you use gf to open a file or :find to locate one.
Tips
- Use
:set path?to check the current value before and after modifying it - All four operators work the same way across list options:
runtimepath,path,cdpath,tags,suffixes, etc. - Combine
^=with:setlocalto prepend only for the current buffer::setlocal path^=./src - To remove a prepended value later, use
:set path-=./include