134

When I go to command mode and type

:!mycommand %

I get my command executed on the current file (% is expanded to the current file name). Is there a similar construct that expands the full file name (with the full path)?

I am using Windows.

2

6 Answers 6

174

:!mycommand %:p

Related:

:!cd %:p:h

8
  • 26
    How do you find these in VIM help?
    – keflavich
    Commented Jan 25, 2012 at 0:45
  • 63
    @keflavich :help filename-modifiers
    – user7675
    Commented Jan 25, 2012 at 3:38
  • to get directory which contains current file, I think that %:h is enough. Why need %:p:h here? Commented Mar 29, 2013 at 16:22
  • 1
    @HVNSweeting :%p is the absolute path to the file, rather than the relative path. :%p%h is the absolute path to the file's parent directory. Using just %:h can result in a relative path.
    – user7675
    Commented Apr 5, 2013 at 13:39
  • 2
    @HVNSweeting Shell: cd ~ ; vim .vimrc, Vim: :!echo %:h, should display '.' for the relative path.
    – user7675
    Commented Apr 6, 2013 at 1:33
66

The other two answers didn’t work for me (for some reason). However, I found that this combo displays the full path when typed in Normal mode:

Press 1 then CtrlG

Source: “Get the name of the current file” on the Vim Tips Wiki. See also the {count}CTRL-G section of :help CTRL-G.

2
  • 5
    That's a different topic. The question is about including the buffer path in a command string (thus the leading :!). Your {count}CTRL-G sequence is for displaying the full path in the UI. Commented Aug 14, 2012 at 8:16
  • 3
    @StefanMajewsky regarding the question title which in fact brought me here, this should be the chosen answer.
    – doc_id
    Commented Mar 2, 2013 at 1:15
30

Append :p, e.g.

:!mycommand %:p

And %:p:h will give you the path of the directory that the file resides in.

21

To print out the current vim filename:

: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

For example (with a vim function), to resolve() and expand() any symlinks to the absolute path to the current script <sfile>:p (instead of %:p), and then exec to source the fnameescape-ed filename contained in a function-local vim variable 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")

Notes:

References

2

Get the name of the current file http://vim.wikia.com/wiki/Get_the_name_of_the_current_file

Set_working_directory_to_the_current_file http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file

0

If you want to use the full path in your vimrc, you can use something like this:

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

This will give you a path with backslashes on Windows.

Not the answer you're looking for? Browse other questions tagged or ask your own question.