Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VimScript: pass value of local variable to edit command

Tags:

vim

I have written a simple function to invoke edit command with a path followed by the given filename.

However it looks like edit l:path invokes edit for file named "l:path" instead of l:path variable value. I guess this is a trivial issue, but it's hard to get search results for calling edit command in function, not from the vim editor.

Following code show the proper value of l:path when I change edit to echon.

command! -nargs=1 E call EditAtCurrentPath(<f-args>)

function! EditAtCurrentPath(filename)
    let l:path=expand('%:p:h').'/'.a:filename
    edit l:path
endfunction
like image 381
Tomasz Wójcik Avatar asked Aug 13 '13 07:08

Tomasz Wójcik


Video Answer


1 Answers

You have to use :execute to pass variables to commands:

execute 'edit' l:path

There are some good examples in :help :execute.

like image 154
mhinz Avatar answered Oct 19 '22 03:10

mhinz