Robert Webb wrote:
> Hi,
>
> I just wrote a simple function in vim script and needed 12 backslashes
> to escape a space! I haven't sat down and tried to figure out exactly
> why it needs that many, but it won't work with less.
>
> It's a useful function too, so here it is:
>
> """"""""""""""""""""""""
> func! AddDir(setting, path)
> if isdirectory(a:path)
> exec "set " . a:setting "+=" . substitute(a:path, " ", "\\\\\\\\\\\\ ",
> "g")
> endif
> endfunc
> """"""""""""""""""""""""
LOL. I like it! But I don't, too. This is one of the things that does
annoy me about Vimscript. But it's shared by pretty much every language
that has the same concept of escape characters. Here's the breakdown:
1. The :set command needs a backslash before a space. One backslash.
2. The 'path' option needs a backslash before a space, because for
compatibility a space is an alternative to a comma. Since :set needs
this escaped too, we have three now.
3. In the substitute replacement, backslashes must be escaped, so double
each. Six now.
4. In double quotes, you need to escape backslashes. Double them again.
Twelve.
Step 4 can easily be avoided by using single quotes, leaving six.
exec "set " . a:setting "+=" . substitute(a:path, " ", '\\\\\\ ', "g")
And for options other than 'path', you will only need two.
Ben.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---