获取 vim 将当前文件路径传递给我的 shell

Get vim to pass the current file path to my shell

我的 vimrc 中已经有了这个:

set shell=powershell
set shellcmdflag=-command

现在我想传递 powershell 当前文件的路径,这样它就可以 运行 它了。我试过这个:

nnoremap <C-q> :! expand('%:p')<cr>

这是我看到的权力shell:

powershell -command " expand('\path\to\my\file....

显然这行不通,因为我在里面放了那个该死的 expand()。如何将当前文件的完整路径传递给我的 shell?

另外,在有人说之前我有自己的映射用于视觉块,这就是我重新映射的原因<C-q>

只用%:p

怎么样
nnoremap <C-q> :!%:p<CR>

%:help cmdline-special)这样的特殊字符被Vim"at places where a file name can be used"自动扩展(引用自帮助)。这包括 :! 命令,因此以下内容就足够了:

nnoremap <C-q> :! %:p<CR>

现在,如果您想进行比 :p 更复杂的处理,将有两个选择:

  • 使用:executennoremap <C-q> :execute '!' expand('%:p')<CR>
  • 使用 <C-R> 和表达式寄存器:nnoremap <C-q> :! <C-r>=expand('%:p')<CR><CR>