Skip to content

Configuration Examples and Snippets

André Sbrocco Figueiredo edited this page May 12, 2020 · 16 revisions

This page can be used to share configuration examples, that can be used to integrate vim-airline with other plugins, or describes special configurations.

Feel free to add your special configuration, if it fixes a need.

Create an airline extension

Read :help airline-writing-extensions and see example.vim for how you'd create an extension. You could create a PR to add this extension to vim-airline or include it as part of your plugin. Either way, it must be in a autoload/airline/extensions/ folder.

To prevent vim-airline from loading your extension when your plugin is not loaded, edit airline#extensions#load() in autoload/airline/extensions.vim.

Dynamically change accent of a vim-airline section (requires AsyncRun)

asciicast

    augroup vimrc
      " Auto rebuild C/C++ project when source file is updated, asynchronously
      autocmd BufWritePost *.c,*.cpp,*.h
                  \ let dir=expand('<amatch>:p:h') |
                  \ if filereadable(dir.'/Makefile') || filereadable(dir.'/makefile') |
                  \   execute 'AsyncRun -cwd=<root> make -j8' |
                  \ endif
      " Auto toggle the quickfix window
      autocmd User AsyncRunStop
                  \ if g:asyncrun_status=='failure' |
                  \   execute('call asyncrun#quickfix_toggle(8, 1)') |
                  \ else |
                  \   execute('call asyncrun#quickfix_toggle(8, 0)') |
                  \ endif
    augroup END

    " Define new accents
    function! AirlineThemePatch(palette)
      " [ guifg, guibg, ctermfg, ctermbg, opts ].
      " See "help attr-list" for valid values for the "opt" value.
      " http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim
      let a:palette.accents.running = [ '', '', '', '', '' ]
      let a:palette.accents.success = [ '#00ff00', '' , 'green', '', '' ]
      let a:palette.accents.failure = [ '#ff0000', '' , 'red', '', '' ]
    endfunction
    let g:airline_theme_patch_func = 'AirlineThemePatch'
    
    
    " Change color of the relevant section according to g:asyncrun_status, a global variable exposed by AsyncRun
    " 'running': default, 'success': green, 'failure': red
    let g:async_status_old = ''
    function! Get_asyncrun_running()

      let async_status = g:asyncrun_status
      if async_status != g:async_status_old

        if async_status == 'running'
          call airline#parts#define_accent('asyncrun_status', 'running')
        elseif async_status == 'success'
          call airline#parts#define_accent('asyncrun_status', 'success')
        elseif async_status == 'failure'
          call airline#parts#define_accent('asyncrun_status', 'failure')
        endif

        let g:airline_section_x = airline#section#create(['asyncrun_status'])
        AirlineRefresh
        let g:async_status_old = async_status

      endif

      return async_status

    endfunction
    
    call airline#parts#define_function('asyncrun_status', 'Get_asyncrun_running')
    let g:airline_section_x = airline#section#create(['asyncrun_status'])

Integration with vim-obsession

Prepend a '$' when obsession is enabled (origin)

function! AirlineInit()
    let g:airline_section_z = airline#section#create(['%{ObsessionStatus(''$'', '''')}', 'windowswap', '%3p%% ', 'linenr', ':%3v '])
endfunction
autocmd User AirlineAfterInit call AirlineInit()

Overriding the inactive statusline

add_inactive_statusline_func (counterpart to add_statusline_func) can be used to add to (or completely override) the inactive statusline:

function! Render_Only_File(...)
  let builder = a:1
  let context = a:2

  call builder.add_section('file', '!! %F')

  return 0   " the default: draw the rest of the statusline
  return -1  " do not modify the statusline
  return 1   " modify the statusline with the current contents of the builder
endfunction
call airline#add_inactive_statusline_func('Render_Only_File')

More information on the use of the builder and context arguments can be found in :help sections airline-funcrefs and airline-pipeline.

A different example: Add the window number in front of the mode

function! WindowNumber(...)
    let builder = a:1
    let context = a:2
    call builder.add_section('airline_b', '%{tabpagewinnr(tabpagenr())}')
    return 0
endfunction

call airline#add_statusline_func('WindowNumber')
call airline#add_inactive_statusline_func('WindowNumber')

That will look like this (note however that the tagbar extension does not apply the inactive funcref, because it handles setting the statusline itself. 2017-01-07 6 07 40

Human readable Line number (with thousands separators)

This adds comma as thousands separator to the line number (issue #1382)

function! MyLineNumber()
  return substitute(line('.'), '\d\@<=\(\(\d\{3\}\)\+\)$', ',&', 'g'). ' | '.
    \    substitute(line('$'), '\d\@<=\(\(\d\{3\}\)\+\)$', ',&', 'g')
endfunction

call airline#parts#define('linenr', {'function': 'MyLineNumber', 'accents': 'bold'})

let g:airline_section_z = airline#section#create(['%3p%%: ', 'linenr', ':%3v'])

Integrate with CMake build system

This example adds the CMAKE_BUILD_TYPE and a user defined cmake variable by peeking into the CMakeCache.txt

function! CMakeStat()
  let l:cmake_build_dir = get(g:, 'cmake_build_dir', 'build')
  let l:build_dir = finddir(l:cmake_build_dir, '.;')

  let l:retstr = ""
  if l:build_dir != ""
      if filereadable(build_dir . '/CMakeCache.txt')
          let cmcache = readfile(build_dir . '/CMakeCache.txt')
          for line in cmcache
              if line =~ "CMAKE_BUILD_TYPE"
                  let value = reverse(split(line, '='))[0]
                  let retstr = retstr . value . " "
              elseif line =~ "RUN_TESTS"
                  let value = reverse(split(line, '='))[0]
                  let retstr = retstr . "T" . value . " "
              endif
          endfor
      endif
  endif
  return substitute(retstr, '^\s*\(.\{-}\)\s*$', '\1', '')
endfunction

call airline#parts#define('cmake', {'function': 'CMakeStat'})
let g:airline_section_b = airline#section#create_left(['cmake'])