英文:
Variables in Vim Plugin
问题
I write a vim plugin to learn some vimscript.
I set the variables in the /autoload/plugin.vim
like this:
let g:configpath = '~/home/username/.vim/bundle/testplugin/templates/'
let g:config = '/config.h'
Then I want to use it within a function:
function! testplugin#template(...)
:e g:configpath . a:1 . g:config
endfunction
The goal is to open the file behind this path. a:1 is a user-given input because the subfolder is selectable by the user.
When I use echo g:configpath . a:1 . g:config
the correct path will be print out to Vim but when I use :e
to open the file Vim open a new file with the name:
"g:configpath . a:1 . g:config" [New]
How do I open the template file behind this path?
英文:
I write a vim plugin to learn some vimscript.
I set the variables in the /autoload/plugin.vim
like this:
let g:configpath = '~/home/username/.vim/bundle/testplugin/templates/'
let g:config = '/config.h'
Then I want to use it within a function:
function! testplugin#template(...)
:e g:configpath . a:1 . g:config
endfunction
The goal is to open the file behind this path. a:1 is a user-given input because the subfolder is selectable by the user.
When I use echo g:configpath . a:1 . g:config
the correct path will be print out to Vim but when I use :e
to open the file Vim open a new file with the name:
"g:configpath . a:1 . g:config" [New]
How do I open the template file behind this path?
答案1
得分: 2
:help :echo
和 :help :edit_f
处理参数的方式不同。
前者期望一个表达式,在运行时评估以生成将要被回显的文本:
:let $FOO = 'foo'
:let g:bar = 'bar'
:echo $FOO .. g:bar .. 'baz' .. eval('1+2')
foobarbaz3
后者也进行了一些评估,但仅限于扩展环境变量和在 :help cmdline-special
下记录的项目。特别需要注意的是它无法进行连接操作:
:let $DIR = '/path/to/dir'
:edit $DIR/filename
:edit %<.stories.ts
如果您想以编程方式构建文件名,您有两个选项。
- 使用
:help :execute
:
:let filename = '/path/to/filename'
:execute 'edit ' .. filename
:execute 'edit ' .. <some expression that evaluates to a filename>
- 使用环境变量:
:let $FILENAME = '/path/to/filename'
:edit $FILENAME
顺便说一下,自动加载的脚本似乎不适合定义全局选项。它们通常在 plugin/foo.vim
中定义。
英文:
:help :echo
and :help :edit_f
handle arguments differently.
The former expects an expression, which is evaluated at runtime to produce the text that will be echoed:
:let $FOO = 'foo'
:let g:bar = 'bar'
:echo $FOO .. g:bar .. 'baz' .. eval('1+2')
foobarbaz3
The latter also does some evaluation but it is limited to expanding environment variables and the items documented under :help cmdline-special
. Most notably it can't do concatenation:
:let $DIR = '/path/to/dir'
:edit $DIR/filename
:edit %<.stories.ts
If you want to build the filename programmatically, you have two options.
-
Use
:help :execute
::let filename = '/path/to/filename' :execute 'edit ' .. filename :execute 'edit ' .. <some expression that evaluates to a filename>
-
Use an environment variable:
:let $FILENAME = '/path/to/filename' :edit $FILENAME
FWIW, autoloaded scripts don't seem to be the right place for defining global options. They are usually defined in plugin/foo.vim
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论