wds
wds

Reputation: 32283

File name expansion in vim when using set option=value

I'm trying to add an autocmd to vim that will execute whenever I open a file in a certain subdirectory and that sets the search path. Unfortunately path name expansion doesn't seem to work inside a set command.

Specifically I'd like to have a line like this in my vimrc:

setlocal path+=**;%:p:h

But this will just give me the literal value. Just calling expand() doesn't work either. Is there a way to get variable expansion to work here?

Upvotes: 1

Views: 388

Answers (3)

ZyX
ZyX

Reputation: 53624

Use

let &l:path.=(empty(&l:path)?(''):(',')).'**;'.escape(expand('%:p:h'), ',\*; ')

. This is much cleaner then using :execute 'setlocal path', especially knowing that fnameescape() was designed to escape paths for commands, not for options and I can say it is not really safe to use it here: it definitely is not going to escape comma and semicolon and add additional escape for space (one for escaping for :set, one for the option itself). (empty(&l:path)?(''):(',')) is here to imitate the behavior of set+=.

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172590

There's no need for the expansion of the current file's directory; just adding . to path will do. From the help:

  • To search relative to the directory of the current file, use:
    :set path=.

Upvotes: 3

Raimondi
Raimondi

Reputation: 5303

What about:

execute 'setlocal path +=**;' . fnameescape(expand('%:p:h'))

Upvotes: 5

Related Questions