无法在 vim 中将文件路径作为函数参数传递

Can't pass filepath as function argument in vim

我的 vimrc:

中有这个功能
function! MyFunc(fl)
  :!cat fl
" :!cat a:fl
" :!cat &fl
endfunction
command! -nargs=1 RunFunc :call MyFunc(<f-args>)

问题是当我在 vim 命令中 运行 :RunFunc ~/scripts/0-test 时,出现错误:

cat: f: No such file or directory
shell returned 1

我浏览过 this, this, this, and this 等各种网站,但 none 对我有用。

首先,您不需要在脚本上下文中使用该冒号:

function! MyFunc(fl)
  !cat fl
endfunction
command! -nargs=1 RunFunc :call MyFunc(<f-args>)

其次,你不能传递那样的表达式。您需要将整个内容与 :help :execute:

连接起来
function! MyFunc(fl)
  execute "!cat " .. fl
endfunction
command! -nargs=1 RunFunc :call MyFunc(<f-args>)

第三,函数参数类型为a::

function! MyFunc(fl)
  execute "!cat " .. a:fl
endfunction
command! -nargs=1 RunFunc :call MyFunc(<f-args>)

至于网站……它们没用。 Vim 附带了一份详尽的文档,当您遇到问题时,这应该是您的第一个打击,而碰巧的是,用户手册(必读)有一整章都是关于编写 vimscript 的::help usr_41.txt.