I have the following command in my vimrc:

execute "set path+=".getcwd()."/**"

It works well if current working directory is like /home/tamlok/my_work/. However, if the directory contains spaces, such as D:\Program Files\vim\, vim will complain.

I suppose that I can fix this using substitute() function to replace all the spaces with escaped spaces. What would an experienced vimmer do here?

Another issue is the \ in Windows. If getcwd() gets D:\Program Files\vim\, do I need to escape the \ with \\ or replace it with /?

For now I handle it like this:

let mycwd=substitute(getcwd(), '\\', '/', 'g')
let mycwd=substitute(mycwd, ' ', '\\ ', 'g')
execute "set path+=".mycwd."/**"

Is that OK? Any help is appreciated! Thanks very much!

2 Answers 2


The function you are looking for is fnameescape(). It will escape all special characters in a given string containing a path to a file to match the standard of the current operating system.

For more on this see :help fnameescape()

  • Thanks very much! Is / the same as `\` in the file path in GVim on Windows?
    – tamlok
    Commented Nov 4, 2015 at 4:13
  • 7
    True, but beware of the difference between fnameescape() and shellescape(). The former is used when passing arguments on Vim's command line, so that they are not molested by Vim. The later is used when passing arguments to external commands, so that they are not molested by the shell. On Windows there's also shellslash, which will have a major influence on the number of hairs on your head if you ever need to mess with !command and friends. Commented Nov 4, 2015 at 5:53

I had the same problem. Try just to use / in paths, and escape whitespace like this . For example I add this line in my vimrc:

set rtp=C:/Program\ Files\ (x86)/Vim/vim74/vim.exe

Than if you will command :echo &rtp you can see:

C:/Program Files (x86)/Vim/vim74/vim.exe

Note that here is no \ before whitespace.

  • Thanks very much! However, I got the path from a function call. So I think I can't control the separator in the path. An escaping function is needed here, just as what the selected answer suggests.
    – tamlok
    Commented Dec 28, 2016 at 14:21
  • @tamlok, I am sorry, but I don't use functions yet - you are deeper vimer then I :-) Share your knowledge, when you will find the answer, please) Commented Jan 14, 2017 at 17:04
  • Hi, EvergreenTree's answer is just the key to the question! :) Thanks!
    – tamlok
    Commented Jan 16, 2017 at 2:10
  • @tamlok, I am confused by search results by words "evergreentree" in Google. Perhaps, I didn't understood you right. Commented Jan 17, 2017 at 11:20
  • Hi, EvergreenTree is a user in StackExchange and posted an answer in this question. Just see above. :)
    – tamlok
    Commented Jan 18, 2017 at 1:20

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.