I'm trying to save the current relative path of my file and line number into my clipboard. I would like to have application/models/my_model.rb:34

But I don't understand how expand works.

They have this example :

            :let &tags = expand("%:p:h") . "/tags"

But I failed when I want to combine relative path expand("%:h") and file_name expand("%:t").

:echom expand("%:h:t") . ':' . line(".")  

returns model:34

Any ideas ?

  • 1
    I don't get it ... %:h is not the relative path - it's (the path - filename). Wouldn't just expand('%') . ':' . line(".") do? For example, I get .vim/vimrc:1 when I run vim .vim/vimrc from ~.
    – muru
    Mar 18, 2016 at 17:06

2 Answers 2


You should just use expand('%').

If I do:

cd ~/..; vim ~/.vim/vimrc

Then, in Vim:

:echo join([expand('%'),  line(".")], ':')

Or with cd ~/Documents; vim ~/.vim/vimrc:

:echo join([expand('%'),  line(".")], ':') join([expand('%:h'), expand('%:t')], '/')
/home/muru/.vim/vimrc:1 /home/muru/.vim/vimrc

expand('%:h') does not offer any advantages here. Doing expand("%:h") . '/' . expand("%:t") is just going to give you back expand('%'), with the added problem that you lose portability - / is not always the correct separator.

Side note: join() is a convenient function for joining strings with separators. Not necessary here, where we have only two items, but could be useful if you add more strings.

  • I like how you used join() that's a good reminder that this function can be pretty useful.
    – statox
    Mar 18, 2016 at 22:24

One way to do it to combine expand :

:echom expand("%:h") . '/' . expand("%:t") . ':' . line(".")    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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