Vanilla Vim Statusline

Why Powerline plugins are often unnecessary

For many, the use of plugins such as powerline and airline within Vim are de facto standard. They produce fancy statuslines that indicate a few main things; the mode you're currently in (insert, normal, visual), any version control information (such as the current Git branch), the filename, file type, file encoding and current directory. Don't get me wrong, some of these statusline plugins do look good. They're customisable with themes and colours, link to other plugins well and introduce special fonts that'll make your fellow Vimmers ask "What's that at the bottom of your screen, and how do I get it?".

My issue with these plugins is that a lot of the time, they are bloated. They include special extra features that not everyone turns on, and even the supposedly streamlined versions such as vim-airline that claims to be 'lighter than air' as it uses solely Vimscript - as opposed to powerline's Python code - are a bit bulky. Even to install these plugins, a plugin manager is necessary if you don't want to compile and configure the code from source.

As an extremely lightweight alternative to these plugins, Vim's own statusline can be modified to achieve (most) of the same things. Everything listed above can be achieved with a few simple lines added to the bottom of your pre-existing .vimrc. My current statusline looks like this:

This statusline boasts of customisable colours, mode displays, the current working directory, the current buffer's file path, the current line number and total lines, file type and Git information. To get all of this information into a single statusline, my current .vimrc contains:

"" statusline
set laststatus=2
set statusline=                          " left align
set statusline+=%2*\                     " blank char
set statusline+=%2*\%{StatuslineMode()}
set statusline+=%2*\
set statusline+=%1*\ <<
set statusline+=%1*\ %f                  " short filename              
set statusline+=%1*\ >>
set statusline+=%=                       " right align
set statusline+=%*
set statusline+=%3*\%h%m%r               " file flags (help, read-only, modified)
set statusline+=%4*\%{b:gitbranch}       " include git branch
set statusline+=%3*\%.25F                " long filename (trimmed to 25 chars)
set statusline+=%3*\::
set statusline+=%3*\%l/%L\\|             " line count
set statusline+=%3*\%y                   " file type
hi User1 ctermbg=black ctermfg=grey guibg=black guifg=grey
hi User2 ctermbg=green ctermfg=black guibg=green guifg=black
hi User3 ctermbg=black ctermfg=lightgreen guibg=black guifg=lightgreen

"" statusline functions
function! StatuslineMode()
    let l:mode=mode()
    if l:mode==#"n"
        return "NORMAL"
    elseif l:mode==?"v"
        return "VISUAL"
    elseif l:mode==#"i"
        return "INSERT"
    elseif l:mode==#"R"
        return "REPLACE"

function! StatuslineGitBranch()
  let b:gitbranch=""
  if &modifiable
      lcd %:p:h
    let l:gitrevparse=system("git rev-parse --abbrev-ref HEAD")
    lcd -
    if l:gitrevparse!~"fatal: not a git repository"
      let b:gitbranch="(".substitute(l:gitrevparse, '\n', '', 'g').") "

augroup GetGitBranch
  autocmd VimEnter,WinEnter,BufEnter * call StatuslineGitBranch()
augroup END

This doesn't look like much code and you're right, it's not. That's the beauty of using Vim's existing statusline functions. Most of what you want from a statusline can be displayed without downloading a plugin (or even a plugin manager to boot). The two custom functions at the end to convert the mode into a string and get the status of the current Git branch are extras, and can be modified to your liking if you want to change the VCS that Vim reads from. The colours of the statusline can also be modified to your liking by simply changing the 'User1', 'User2' and 'User3' colours to another set of values.

If you want an easy way to customise your Vim statusline without any configuration, check out this post: Vim Statusline Generator