0

We're formatting our branch name like feature/HD-1234_tiny_description where "1234" refers to a Jira ticket number. I'd like to only display this number in vim-airline (along with vim-fugitive)

According to the docs:

vim-airline will display the branch-indicator together with the branch name in the statusline, if one of the following plugins is installed:

if a string is provided, it should be the name of a function that takes a string and returns the desired value

let g:airline#extensions#branch#format = 'CustomBranchName'
function! CustomBranchName(name)
    return '[' . a:name . ']'
endfunctionlet

So I'm naively tried

let g:airline#extensions#branch#format = 'JiraTicketNumber'
function! JiraTicketNumber(name)
  return matchstr(name, '/feature\/hd[-_]{1}(\d{4})/i')[1]
endfunction

which returns an error undefined variable : name (roughly translated).

Could anyone give me an helping hand ?


Ideally I'd like to return the ticket number only if the branch name starts by "feature" (so fix/... and chore/... would be simply truncated via let g:airline#extensions#branch#format = 1)

Should this work ?

let g:airline#extensions#branch#format = 'JiraTicketNumber'
function! JiraTicketNumber(name)
  if name =~ 'feature'
    return matchstr(name, '/feature\/hd[-_]{1}(\d{4})/i')[1]
  else
    return name
  endif
endfunction

1 Answer 1

2

Notice that in the example they use a:name to refer to the argument; in VimScript prior to vim9script you must prefix arguments with a: because you reference them in the "argument namespace" (actually a dictionary).

Try (where I've also touched up the pattern, the =~[#] operator, and used the matchlist function)

let g:airline#extensions#branch#format = 'JiraTicketNumber'
function JiraTicketNumber(name) abort
  if a:name =~# 'feature'
    return matchlist(a:name, 'feature\/HD[-_]\(\d\{4}\)')[1]
  else
    return a:name
  endif
endfunction
1
  • It works like a charm ! I'll have a closer look at what you touched up. Thanks for the introduction to vimscript :)
    – Sumak
    Commented Sep 22, 2022 at 7:13

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.