Muhammad Fhadli
Muhammad Fhadli

Reputation: 361

How to add custom text to vi airline

How can I add custom text to vim airline. like word evgenimba in the right botton of the picture below? I have tried to search with various keyword but could not find the solution enter image description here

Upvotes: 2

Views: 602

Answers (1)

Bailey Brightman
Bailey Brightman

Reputation: 133

Wow, I can understand why you asked here, getting any info on this was an emmense pain.

This is possible with a few lines added into your init.vim though it may necessitate some minor loss of function or re-organizing of status line contents

You cannot create a new section in vim-airline as explained in this issue on gh however you can alter, add onto, and re-order the current default sections in the vim-airline statusline to add text or otherwise change the statusline

To start, you should choose a section you would like to alter. I found section y the best candidate for this as I don't have any use for it's current function. You will have to move this section to the end though, which I will explain later.

The default order is as shown, with the section names over them. You can find the codes for each function with :help 'statusline' Visual Representation of Statusline Order

Here are the individual sections pulled from :help airline and what is inside them by default

  let g:airline_section_a       (mode, crypt, paste, spell, iminsert)
  let g:airline_section_b       (hunks, branch)[*]
  let g:airline_section_c       (bufferline or filename, readonly)
  let g:airline_section_gutter  (csv)
  let g:airline_section_x       (tagbar, filetype, virtualenv)
  let g:airline_section_y       (fileencoding, fileformat, 'bom', 'eol')
  let g:airline_section_z       (percentage, line number, column number)
  let g:airline_section_error   (ycm_error_count, syntastic-err, eclim,
                                 languageclient_error_count)
  let g:airline_section_warning (ycm_warning_count, syntastic-warn,
                                 languageclient_warning_count, whitespace)

Be sure to note, when changing from the default you will replace the entire section and thus need to re-add any part you wanted to keep. You can find all the codes with the command help 'statusline'

To answer your question, one can change the vim airline section with the following configuration inside your init.vim

"Create a funtion to get the sections contents, name is irrelevant
function! GetSectionContents()
  " Will be refreshed often
  return 'evgenimba'
endfunction

"Create vim-airline part
call airline#parts#define_function('partName','GetSectionContent')

"Replace section y with output of parts function
let g:airline_section_y = airline#section#create_right(['','partName'])

// move section y in the right side array to the end of the statusline
let g:airline#extensions#default#layout = [
    \ [ 'a', 'b', 'c' ],
    \ [ 'x', 'z', 'error', 'warning', 'y' ]
    \ ]

Here is a screenshot of what I was able to achieve vim config final with hello world as the output text

Some good reading on how you can configure a section further is available in :help airline-advanced-configuration

Upvotes: 3

Related Questions