VimScript: pass the value of a local variable to edit a command

I wrote a simple function to call an edit command with a path followed by a given file name.

However, it looks like edit l:path is editing for a file named "l:path" instead of the value of the l:path variable. I think this is a trivial problem, but it’s hard to get the search results for calling the editing function in the function, and not from the vim editor.

The following code shows the correct l value: the 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 
+6
source share
1 answer

You should use :execute to pass variables to commands:

execute 'edit' l:path

There are some good examples in :help :execute .

+13
source

Source: https://habr.com/ru/post/951604/


All Articles