当我进入命令模式并键入
:!mycommand %
Run Code Online (Sandbox Code Playgroud)
我在当前文件上执行了我的命令(%扩展为当前文件名).是否有类似的构造扩展完整文件名(使用完整路径)?
我正在使用Windows.
Ann*_*rom 158
:!mycommand %:p
有关:
:!cd %:p:h
Umb*_*ule 64
其他两个答案对我不起作用(出于某种原因).但是,我发现此组合在正常模式下键入时显示完整路径:
1然后按CtrlG
来源:Vim Tips Wiki上的" 获取当前文件的名称 ".参见{count}CTRL-G第:help CTRL-G.
要打印出当前的vim文件名:
:help expand
:echo expand("%:p") " absolute path
:echo expand("%:p:h") " absolute path dirname
:echo expand("%:p:h:h")" absolute path dirname dirname
:echo expand("%:.") " relative path
:echo expand("%:.:h") " relative path dirname
:echo expand("%:.:h:h")" relative path dirname dirname
:echo expand("<sfile>:p") " absolute path to [this] vimscript
:help filename-modifiers
Run Code Online (Sandbox Code Playgroud)
例如(使用vim函数),to resolve()和expand()任何符号链接到当前脚本的绝对路径<sfile>:p(而不是%:p),然后exec到函数本地vim变量中包含source的fnameescape-ed文件名l:vimrcfilename:
" g:__sfile__dirname -- directory containing this vimrc script
" after symlinks
" ~dirname(abspath(realpath(__file__)))
let g:__sfile__dirname=fnamemodify(resolve(expand("<sfile>:p")), ":h")
" Source_dotvim(filename) -- source dirname(this_vimrc)/filename
function Source_dotvim(filename)
let l:vimrcfilename=g:__sfile__dirname . "/" . a:filename
if filereadable(l:vimrcfilename) && !empty(l:vimrcfilename)
"source s:vimrcfilename "this doesn't work, so exec:
exec "source " . fnameescape(l:vimrcfilename)
else
echo l:vimrcfilename . " empty or not found."
endif
endfunction
call Source_dotvim("vimrc.local.01-env.vimrc")
Run Code Online (Sandbox Code Playgroud)
笔记:
:help fnamemodify%:p缓冲文件)/ <sfile>:p(脚本文件)expand('<sfile>') 在函数中不起作用l:varname不起作用; 所以,exec- 字符串连接 - 和fnameescape:messages 打印最新的200个vim [error,]消息参考