I have a lot of projects of the form:


I would like to extract just the project-name to put into my statusline. I use autochdir, so I know I can create a function that calls getcwd() and extracts that part. However, I haven't been able to figure out how to do the parsing after that point.

function ProjectName()
  return ???getcwd()???

set statusline=%t\ %m%r%{ProjectName()}%{fugitive#statusline()}%=%l,%c\ [0x%B]\ %P

If I edit something outside that folder, I'd like to leave the project blank. Can someone fill in the ??? for me?

  • 5
    :help fnamemodify
    – romainl
    Commented Apr 14, 2016 at 17:06

2 Answers 2


I think your solution might work, but I think this is a slightly cleaner and more readable version:

function ProjectName()
  let l:path = expand('%:p')
  return (l:path =~# '^/home/karl/source/.\+')
        \ ? substitute(l:path, '^/home/karl/source/\([^/]*\)/.*', '\1', '')
        \ : '...'

Also, it seems you add the brackets [...] both through the function and through the statusline option. I would think this should only come through the function.

  • 1
    Yes, I removed the brackets from the statusline later, because I want nothing to show up when I'm outside of the source directory. Your version also has the bonus of fixing a weird bug when I :e /home/karl/source. Thanks very much. Commented Apr 15, 2016 at 14:53

Thanks to @romainl's comment for pointing me in the right direction. Here's my solution:

function ProjectName()
    if expand("%:p") =~ "^/home/karl/source/[^/]*/"
        return "[" . fnamemodify('', ':p:s?/home/karl/source/\([^/]*\)/.*?\1?') . "]"
        return ""

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.