Vim命令和映射

3

嗨,我想制作一个VIM映射,以便在按下F2时运行脚本。我正在努力为正在使用vim编辑的文件获取完整路径,以在命令函数中使用。

nnoremap <F2> :! php /home/kbuczynski/diff_re.php fix <full file path here> flagArg

有什么想法吗?
2个回答

5

您可以使用以下命令获取当前缓冲区的完整路径:

expand('%:p')

你的映射可能如下所示(您还需要使用execute函数):
function! CallPHP()
    execute '!php /home/kbuczynski/diff_re.php fix '.expand('%:p').' flagArg'
endfunction
nnoremap <F2> :call CallPHP()<CR>

这里有一份关于处理文件路径的好文档。


是的,我能看到它在工作,但出于某种原因它会执行以下操作: php /home/kbuczynski/diff_re.php fix expand('/home/file.php') flagArg 这导致脚本无法运行。 - NashPL

3

如果没有其他变量需要注入,您不需要使用:executeexpand()。您应该可以直接使用%:p

nnoremap <F2> :!php /home/kbuczynski/diff_re.php fix %:p flagArg<cr>

如果您需要在其他地方读取 flagArg,那么您将需要使用:execute

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接