23

Say I am running Vim and pwd returns

/home/rafid/myproject

And say I am currently editing the file

/home/rafid/myproject/website/editpage.php

Is there any command that returns this for me?

website/editpage.php

That is, the path of the file relative to the current folder.

Pops
  • 30,199
  • 37
  • 136
  • 151
Rafid
  • 18,991
  • 23
  • 72
  • 108

7 Answers7

36

Although expand('%') often works, there are rare occasions where it does not. But you can force Vim to always present the relative path by calling fnamemodify:

:echo fnamemodify(expand("%"), ":~:.")

From the manual:

    :.      Reduce file name to be relative to current directory, if
            possible.  File name is unmodified if it is not below the
            current directory.
            For maximum shortness, use ":~:.".

The :~ is optional. It will reduce the path relative to your home folder if possible (~/...). (Unfortunately that only works on your home; it won't turn /home/fred into ~fred if you aren't logged in as fred.)

As Adam pointed out the comments, this can be shortened to:

:echo expand("%:~:.")

Reference: :h expand<Tab> and :h fnamem<Tab>


If you are limited for space (e.g. using this in your statusline), and can manage with "fuzzy" information about where the file is located, then check out pathshorten() which compresses folder names down to one character:

:echo pathshorten('~/.vim/autoload/myfile.vim')
~/.v/a/myfile.vim

Reference: :h pathsh<Tab>

joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
  • 2
    Thanks for pointing me in the right direction. Just for the record, looks like the modifiers can also be expanded directly using `:echo expand("%:~:.")`. More information can be found at `:help filename-modifiers`. – Adam Byrtek May 24 '20 at 00:58
  • Thank you for this answer! I am working in vim and trying to execute a command inside a docker container (run a specific test file) so the absolute path won't work for me. Docker can't see the full path and my test runner inside the container throws an error because it can't find the file. `fnamemodify` worked perfectly! – latortuga Jul 22 '20 at 15:26
11

Another option would be to write a vim function. Here's my humble attempt:

function! Relpath(filename)
    let cwd = getcwd()
    let s = substitute(a:filename, l:cwd . "/" , "", "")
    return s
endfunction

You call Relpath with any full path name, and it will strip the current directory name from its argument.

For example, try :echo Relpath(expand("%:p")) (the :p modifier asks Vim to return the full path). Obviously, this is not necessary in your case, since % by itself returns relative path. However, it might come in handy in other cases.

nimrodm
  • 23,081
  • 7
  • 58
  • 59
5

This works for me :

:echo expand("%")
icecrime
  • 74,451
  • 13
  • 99
  • 111
  • 4
    You can also use `%` in insert mode to insert the current file name. – Peter Rincker Dec 24 '10 at 16:50
  • 3
    This does not always work and is dependent on how you initially opened the file. See @joeytwiddle 's answer to this question for a more complete solution. – senbrow Apr 25 '16 at 20:16
1

Blockquote This works for me :
:echo expand("%")

This is only working if you opened that file with a relative file:

for vi ./foo, expand("%") will be ./foo

but

for vi /tmp/foo expand("%") will be /tmp/foo
CansasCity
  • 47
  • 2
  • 3
  • 1
    Not here. If I am in `/tmp` and do `vi /tmp/foo` then Vim works out the relativepath, and `expand("%")` shows me `foo`. **However** if I keep vim open in `/tmp` and do `:e /tmp/bar` **then** `expand("%")` will show the full path. – joeytwiddle Apr 06 '14 at 17:17
1

if you use autocmd to always set the current directory of the buffer that you are working on ( cd %:p:h ) then you can just type :cd

Lloyd Moore
  • 3,117
  • 1
  • 32
  • 32
  • to the next person landing on this page - check out this vim tip for more details on setting cd with autocmd http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file – molicule Jun 12 '11 at 00:26
0

Yes, you can use

:args

This will give you the filename of the current file, for informational purposes.

kvista
  • 5,039
  • 1
  • 23
  • 25
0

A workaround can be :cd . which seems to re-evaluate the path relative-ness. I agree this is very annoying though.

Hans
  • 2,230
  • 23
  • 23