1

I am trying to set an option in my init.vim or .vimrc. Specifically, it is the path variable, and I need to set it to a value that contains spaces; for example, this could be because several of the directories in my Windows path contain spaces.

Here are all the things I tried that did not work (some of them set the path to an empty variable, others of them failed with an error message on invalid characters):

set path = /c/Program Files/exampledir
set path = /c/Program\ Files/exampledir
set path = /c/Program\\ Files/exampledir
set path = '/c/Program Files/exampledir'
set path = '/c/Program\ Files/exampledir'
set path = "/c/Program Files/exampledir"
set path = "/c/Program\ Files/exampledir"
set path = "'/c/Program Files/exampledir'"
set path = "'/c/Program\ Files/exampledir'"

As far as I can tell, some of the attempts using double quotes might not be working because Vim interprets a double quote as a comment character (:help :comment).

Some of the attempts with backslashes might not be working because backslashes themselves are maybe getting escaped or parsed out.

0

2 Answers 2

1

In :set: you must write \ for spaces. See :help option-backslash (also from :help :set and scrolling).

                            *option-backslash*
To include white space in a string option value it has to be preceded with a
backslash.  To include a backslash you have to use two.  Effectively this
means that the number of backslashes in an option value is halved (rounded
down).
In options 'path', 'cdpath', and 'tags', spaces have to be preceded with three
backslashes instead for compatibility with version 3.0 where the options can
be separated by either commas or spaces.
Comma-separated options like 'backupdir' and 'tags' will also require commas
to be escaped with two backslashes, whereas this is not needed for
non-comma-separated ones like 'makeprg'.
When setting options using |:let| and |literal-string|, you need to use one
fewer layer of backslash.
A few examples: >
   :set makeprg=make\ file      results in "make file"
   :let &makeprg='make file'        (same as above)
   :set makeprg=make\\\ file        results in "make\ file"
   :set tags=tags\ /usr/tags        results in "tags" and "/usr/tags"
   :set tags=tags\\\ file       results in "tags file"
   :let &tags='tags\ file'      (same as above)

   :set makeprg=make,file       results in "make,file"
   :set makeprg=make\\,file     results in "make\,file"
   :set tags=tags,file          results in "tags" and "file"
   :set tags=tags\\,file        results in "tags,file"
   :let &tags='tags\,file'      (same as above)

For 'path' specifically, there's an extra caveat :

<   - Spaces can also be used to separate directory names (for backwards
      compatibility with version 3.0).  To have a space in a directory
      name, precede it with an extra backslash, and escape the space: >
        :set path=.,/dir/with\\\ space

You can also use &-notation (documented at :help let-&):

:let &{option-name} = {expr1}           *:let-option* *:let-&*
            Set option {option-name} to the result of the
            expression {expr1}.  A String or Number value is
            always converted to the type of the option.
            For an option local to a window or buffer the effect
            is just like using the |:set| command: both the local
            value and the global value are changed.
            Example: >
                :let &path = &path .. ',/usr/local/include'
<           This also works for terminal codes in the form t_xx.
            But only for alphanumerical names.  Example: >
                :let &t_k1 = "\<Esc>[234;"
<           When the code does not exist yet it will be created as
            a terminal key code, there is no error.

So the example would be written in one of these two ways:

set path=/c/Program\\\ Files/exampledir
let &path = '/c/Program\ Files/exampledir'

With neovim's Lua, you could write

vim.opt.path = '/c/Program Files/exampledir'

Finally, the question "How do I navigate to topics in Vim's documentation?" provides a number of tips for finding out how to do something in Vim.

0

It is perhaps easiest to do this with Lua. If you use neovim, you can port over your entire init.vim file to Lua and save it as init.lua instead (nvim will load from one or the other, but throw an error about conflicting configs if you try to load from both). In Vim with +lua or neovim, you could instead use one lua command in your vimscript file, like so:

:lua vim.opt.path = '/c/Program Files/exampledir'

As a comment pointed out, there is more information at :help 'path': spaces and backslashes should be both be backslash-escaped to work. Also, the = in :set can be preceded but not followed by space (:help :set=):

set path=/c/Program\\\ Files/exampledir

As per another comment, there is a way to set the option with an expression, which allows more straightforward quoting:

let &path='/c/Program Files/exampledir'
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.