如何让和执行在同一行?

How to let and execute in the same line?

我试图在 Vim 中为 Perl 做一个简单的设置并使用了这个:

function! PerlEnvSetup()
    nnoremap <F10> :let f=expand("%")|vnew|execute '.!perl "' . f . '"'<CR>
endf

autocmd FileType perl :call PerlEnvSetup()<CR>

但是当我做vim XX.pl时,底部的错误是:

Error detected while processing function PerlEnvSetup:
line    1:
E121: Undefined variable: f
E15: Invalid expression: '.!perl "' . f . '"'<CR>
Error detected while processing FileType Auto commands for "perl":
E488: Trailing characters

而 window 一分为二(除非我按 F10,否则不会发生这种情况)。我在这里做错了什么?

您看到的是 | 用于分隔命令(因此明确结束了您的 :nnoremap 命令。

因此您想要使用下面的帮助中描述的特殊习语 <Bar> :h map_bar

还有一个选项:

function! PerlExecuteCurrentFile()
    let f=expand("%")
    vnew
    execute '.!perl "' . f . '"'
endf

function! PerlEnvSetup()
    nnoremap <F10> :call PerlExecuteCurrentFile()<CR>
endf