I know that I can print whole path by Ctrlr = expand('%:p') in insert mode. I can also print head/tail with expand('%:h'), expand('%:t').

I want to print the filename with it's parent directory. Like if the full path was /home/torsho/maalamaal/src/maalamaal/urls.py it would insert maalamaal/urls.py

  • Do you actually want the filename and exactly one parent directory, or do you want the path relative to the src/ directory?
    – tommcdo
    Mar 13, 2018 at 12:55

2 Answers 2


You can use multiple modifiers in a single call to expand, so you can find the parent directory by asking for the tail of the head.

Therefore, you can use:

expand('%:h:t') . '/' . expand('%:t')

If you need to do this frequently, you could perhaps use a mapping so you don't have to type it in all the time:

inoremap <F5> <C-R>=expand('%:h:t') . '/' . expand('%:t')<CR>

If you want it to use a backslash as the path separator on Windows, you can check the operating system with has():

inoremap <F5> <C-R>=expand('%:h:t') . (has('win32') ? '\' : '/') . expand('%:t')<CR>

You can have a look at :h filename-modifiers for a list of the modifiers you can use with expand(). A possible solution is the following:


It uses the full path modifier :p and then the substitution modifier :s to keep only the last directory in the path and the filename + extension.

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.