“Vim插件中的变量”

huangapple go评论59阅读模式
英文:

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 = &#39;foo&#39;
:let g:bar = &#39;bar&#39;
:echo $FOO .. g:bar .. &#39;baz&#39; .. eval(&#39;1+2&#39;)
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 = &#39;/path/to/dir&#39;
:edit $DIR/filename
:edit %&lt;.stories.ts

If you want to build the filename programmatically, you have two options.

  • Use :help :execute:

    :let filename = &#39;/path/to/filename&#39;
    :execute &#39;edit &#39; .. filename
    :execute &#39;edit &#39; .. &lt;some expression that evaluates to a filename&gt;
    
  • Use an environment variable:

    :let $FILENAME = &#39;/path/to/filename&#39;
    :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.

huangapple
  • 本文由 发表于 2023年6月26日 16:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定