Pressing 1 followed by Ctrl + G shows the full path of the current file.
The best Linux command to get file path is using pwd command. To use this command, type “pwd” into your terminal and press enter. This command will print the current working directory. The output will be the file path.
By default, Vim only shows the filename of the currently open file in the status line and the command line below it. Sometimes, you might want to view the full path of the file. To do that, press 1 followed by Ctrl-G . The full path of the file is displayed in the command line at the bottom.
We can use the utilities – which, command, type, and whereis to find the path of a Linux command.
:!mycommand %:p
Related:
:!cd %:p:h
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
.
Append :p
, e.g.
:!mycommand %:p
And %:p:h
will give you the path of the directory that the file resides in.
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:
:help fnamemodify
%:p
(buffer file) / <sfile>:p
(script file)expand('<sfile>')
does not work from within a functionl:varname
does not work; so, exec
- string concatenation - and fnameescape
:messages
prints the most-recent 200 vim [error,] messagesReferences
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With