13

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

1 Answer 1

24

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

execute 'edit' l:path

There are some good examples in :help :execute.

2
  • Works perfectly! Thanks for the fast answer! Aug 13, 2013 at 8:00
  • what about if the variable l:path has a '%' , how can you escape it?
    – vecin
    Aug 1, 2020 at 4:37

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.