I'm trying to cut string returned from fugitive plugin. It returns branch name of current file. By default returned string is Git(branchname) and this is why I want to get rid of odd Git(..) wrapper. So here is the code:

set statusline+=%{strpart(fugitive#statusline(), 4, 1)}

When I update .vimrc it returns error:

E540: Unclosed expression sequence: statusline+=%{strpart(fugitive#statusline(), 

What do I wrong?


Values used in :set are strings. See :h set-args:

                                                :set-args E487 E521
:se[t] {option}={value}         or
:se[t] {option}:{value}
                        Set string or number option to {value}.
                        White space between {option} and '=' is allowed and
                        will be ignored.  White space between '=' and {value}
                        is not allowed.
                        See option-backslash for using white space and
                        backslashes in {value}.

You should escape the spaces:

set statusline+=%{strpart(fugitive#statusline(),\ 4,\ 1)}

Consider the example from the help for fugitive#statusline:

set statusline=%<%f\ %h%m%r%{fugitive#statusline()}%=%-14.(%l,%c%V%)\ %P

Note how all the spaces (all two of them, anyway) are escaped.


strpart(x, 4, 1) is a single character, which is probably not what you wanted. split() might be more useful:

:echo strpart(fugitive#statusline(),4,1)
:echo split(fugitive#statusline(), '[()]')[1]

So, you probably want:

set statusline+=%{remove(split(fugitive#statusline(),'[()]'),1)}

This will error out outside git repositories. It might better to write a small function at this point:

function! Current_git_branch()
    let l:branch = split(fugitive#statusline(),'[()]')
    if len(l:branch) > 1
         return remove(l:branch, 1)
    return ""

And then:

set statusline+=%{Current_git_branch()}
  • 1
    Hm...:echo split(fugitive#statusline(), '[()]')[1] works perfectly, but when I place the last line you suggested set statusline+=%{... it returns error: E684: list index out of range: 1. What's wrong!?(( Feb 16 '16 at 10:42
  • 2
    @TimurFayzrakhmanov are you running it in a window whose directory is not in Git? I get that error if I'm not in a git repository.
    – muru
    Feb 16 '16 at 10:53
  • Right! And it always append "0" if a file isn't in git repo. Can you please suggest how to prevent this error (I mean place nothing if the file isn't in git repo)? Feb 16 '16 at 10:57
  • @TimurFayzrakhmanov see update.
    – muru
    Feb 16 '16 at 11:11

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.