Matt Gregory
Matt Gregory

Reputation: 8652

Vim: How do I chdir to path in a variable

I have a path stored in a variable (say l:s) and want to execute lcd l:s in a vim script, but it tells me the path "l:s" doesn't exist. What is the problem here, because vim resolves variable names in other ex commands just fine (echo, etc.). I don't understand the difference.

Upvotes: 2

Views: 1227

Answers (2)

Jim Stewart
Jim Stewart

Reputation: 17323

You can use exe and construct the command:

let s:some_dir_name = "foo"
exe "lcd " . s:some_dir_name

That'll evaluate the variable s:some_dir_name and execute the command lcd foo.

(I didn't use l:s from your question because that's not a legal variable name, but I think you get the idea.)

Upvotes: 4

Matt Gregory
Matt Gregory

Reputation: 8652

Vim lets you set environment variables within a script, and these work with :cd and :lcd. For example:

function foo()
    let $SOME_PATH = '/some/path'
    lcd $SOME_PATH
endfunction

Upvotes: 1

Related Questions