Search code examples
vimvim-pluginneovim

How to prevent comma in VimL string concatenation?


I have an object defined like so:

let g:lightline = {
      \ 'component': {
      \   'fugitive': '%{exists("*fugitive#statusline") ? "⎇ " . fugitive#statusline() : ""}'
      \ },
  \ }

The output of fugitive#statusline() is GIT(master), so the final string eventually appears in my statusline as ⎇ ,GIT(master) with a comma.

Why is there a comma? How can we avoid the comma?

I'm using lightline.vim to customize my status line, and the whole configuration looks like this:

let g:lightline = {
      \ 'active': {
      \   'left': [
      \       [ 'mode', 'paste' ],
      \       [ 'filename', 'readonly', 'modified' ],
      \       [ 'fugitive', ],
      \   ]
      \ },
      \ 'inactive': {
      \   'left': [
      \       [ 'filename', 'readonly', 'modified' ],
      \       [ 'fugitive', ],
      \   ]
      \ },
      \ 'component': {
      \   'readonly': '%{&readonly?"x":""}',
      \   'fugitive': '%{exists("*fugitive#statusline") ? "⎇ " . fugitive#statusline() . "" : ""}'
      \ },
      \ 'component_visible_condition': {
      \   'fugitive': '(exists("*fugitive#head") && ""!=fugitive#head())'
      \ },
      \ 'separator': { 'left': '', 'right': '' },
      \ 'subseparator': { 'left': '|', 'right': '|' }
  \ }

Solution

  • This code in the fugitive plugin either prepends a comma, or encloses the element in square braces. These two styles are also offered by the built-in statusline elements.

    You can remove the undesired comma by taking only a substring ([1:]) off the result of the fugitive invocation:

    'fugitive': '%{exists("*fugitive#statusline") ? "⎇ " . fugitive#statusline()[1:] : ""}'