代码之家  ›  专栏  ›  技术社区  ›  skeept

如何展开当前文件的完整路径以传递给vim中的命令?

vim
  •  117
  • skeept  · 技术社区  · 15 年前

    当我进入命令模式并输入

    :!mycommand %
    

    我在当前文件上执行命令( % 扩展到当前文件名)。 是否有类似的结构来扩展完整文件名(使用完整路径)?

    我在用窗户。

    6 回复  |  直到 7 年前
        1
  •  154
  •   Annika Backstrom    15 年前

    :!mycommand %:p

    :!cd %:p:h

        2
  •  64
  •   Rory O'Kane Erce    11 年前
        3
  •  29
  •   martin clayton egrunin    15 年前

    :p

    :!mycommand %:p
    

    %:p:h

        4
  •  7
  •   derenio Wes Turner    7 年前

    :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
    

    resolve() expand() <sfile>:p %:p exec source fnameescape 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")
    

        6
  •  0
  •   Matthias Braun AdamSkywalker    11 年前

    vimrc

    let vimFiles = '$HOME/.vim'
    let absPath  = expand(vimFiles . '/subDir')