2

I have a lot of projects of the form:

/home/karl/source/project-name/further/hierarchy/...

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()???
endfunc

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 Apr 14 '16 at 17:06
3

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', '')
        \ : '...'
endfunction

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. – Karl Bielefeldt Apr 15 '16 at 14:53
2

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?') . "]"
    else
        return ""
endfunc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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