Skip to content

Instantly share code, notes, and snippets.

Last active September 13, 2022 13:15
Show Gist options
  • Save ahmedelgabri/b9127dfe36ba86f4496c8c28eb65ef2b to your computer and use it in GitHub Desktop.
Save ahmedelgabri/b9127dfe36ba86f4496c8c28eb65ef2b to your computer and use it in GitHub Desktop.
Trying to build my vim statusline without plugins
" Statusline & Tabline/Buffer line
" Dynamically getting the fg/bg colors from the current colorscheme, returns hex which is enough for me to use in Neovim
" Needs to figure out how to return cterm values too
let fgcolor=synIDattr(synIDtrans(hlID("Normal")), "fg", "gui")
let bgcolor=synIDattr(synIDtrans(hlID("Normal")), "bg", "gui")
" Tabline/Buffer line
set showtabline=2
set tabline="%1T"
" Statusline
let g:currentmode={
\ 'n' : 'N ',
\ 'no' : 'N·Operator Pending ',
\ 'v' : 'V ',
\ 'V' : 'V·Line ',
\ 'x22' : 'V·Block ',
\ 's' : 'Select ',
\ 'S' : 'S·Line ',
\ 'x19' : 'S·Block ',
\ 'i' : 'I ',
\ 'R' : 'R ',
\ 'Rv' : 'V·Replace ',
\ 'c' : 'Command ',
\ 'cv' : 'Vim Ex ',
\ 'ce' : 'Ex ',
\ 'r' : 'Prompt ',
\ 'rm' : 'More ',
\ 'r?' : 'Confirm ',
\ '!' : 'Shell ',
\ 't' : 'Terminal '
highlight User1 cterm=None gui=None ctermfg=007 guifg=fgcolor
highlight User2 cterm=None gui=None ctermfg=008 guifg=bgcolor
highlight User3 cterm=None gui=None ctermfg=008 guifg=bgcolor
highlight User4 cterm=None gui=None ctermfg=008 guifg=bgcolor
highlight User5 cterm=None gui=None ctermfg=008 guifg=bgcolor
highlight User7 cterm=None gui=None ctermfg=008 guifg=bgcolor
highlight User8 cterm=None gui=None ctermfg=008 guifg=bgcolor
highlight User9 cterm=None gui=None ctermfg=007 guifg=fgcolor
" Automatically change the statusline color depending on mode
function! ChangeStatuslineColor()
if (mode() =~# '\v(n|no)')
exe 'hi! StatusLine ctermfg=008 guifg=fgcolor gui=None cterm=None'
elseif (mode() =~# '\v(v|V)' || g:currentmode[mode()] ==# 'V·Block' || get(g:currentmode, mode(), '') ==# 't')
exe 'hi! StatusLine ctermfg=005 guifg=#00ff00 gui=None cterm=None'
elseif (mode() ==# 'i')
exe 'hi! StatusLine ctermfg=004 guifg=#6CBCE8 gui=None cterm=None'
exe 'hi! StatusLine ctermfg=006 guifg=orange gui=None cterm=None'
return ''
" Find out current buffer's size and output it.
function! FileSize()
let bytes = getfsize(expand('%:p'))
if (bytes >= 1024)
let kbytes = bytes / 1024
if (exists('kbytes') && kbytes >= 1000)
let mbytes = kbytes / 1000
if bytes <= 0
return '0'
if (exists('mbytes'))
return mbytes . 'MB '
elseif (exists('kbytes'))
return kbytes . 'KB '
return bytes . 'B '
function! ReadOnly()
if &readonly || !&modifiable
return ''
return ''
function! GitInfo()
let git = fugitive#head()
if git != ''
return ' '.fugitive#head()
return ''
set laststatus=2
set statusline=
set statusline+=%{ChangeStatuslineColor()} " Changing the statusline color
set statusline+=%0*\ %{toupper(g:currentmode[mode()])} " Current mode
set statusline+=%8*\ [%n] " buffernr
set statusline+=%8*\ %{GitInfo()} " Git Branch name requires
set statusline+=%8*\ %<%F\ %{ReadOnly()}\ %m\ %w\ " File+path
set statusline+=%*
set statusline+=%9*\ %= " Space
set statusline+=%8*\ %y\ " FileType
set statusline+=%7*\ %{(&fenc!=''?&fenc:&enc)}\[%{&ff}]\ " Encoding & Fileformat
set statusline+=%8*\ %-3(%{FileSize()}%) " File size
set statusline+=%0*\ %3p%%\ \ %l:\ %3c\ " Rownumber/total (%)
Copy link

I get
E117: Unknown function: fugitive#head
Any idea why?

Copy link

@gabrielziegler3 you need to have installed.

From what I see also there are also calls to functions from these plugins., &

Just to be very clear. This is a very very very old version of my statusline. I don't use these anymore. (only fugitive, I still use this plugin) my current statusline lives here

I have updated the gist to remove these calls.

Copy link

Starsign68 commented May 28, 2021

You could also use something like:

let g:currentmode = { 'n': 'NORMAL', 'no': 'N·OP·PEND', 'v': 'VISUAL', 'V': 'V·LINE', '': 'V·BLOCK', 's': 'SELECT', 'S': 'S·LINE', '': 'S·BLOCK', 'i': 'INSERT', 'R': 'REPLACE', 'Rv': 'V·REPLACE', 'c': 'COMMAND', 'cv': 'VIM EX', 'ce': 'EX', 'r': 'PROMPT', 'rm': 'MORE', 'r?': 'CONFIRM', '!': 'SHELL', 't': 'TERMINAL'}
let g:modegroups = { 'n': 'NRM', 'no': 'NRM', 'v': 'VIS', 'V': 'VIS', '': 'VIS', 's': 'OTH', 'S': 'OTH', '': 'OTH', 'i': 'INS', 'R': 'INS', 'Rv': 'INS', 'c': 'OTH', 'cv': 'OTH', 'ce': 'OTH', 'r': 'OTH', 'rm': 'OTH', 'r?': 'OTH', '!': 'OTH', 't': 'OTH'}
function! Modetheme(group)
if g:modegroups[mode()]==a:group
let g:colthm = g:currentmode[mode()]
let g:lineModes = g:colthm
return "  ".g:lineModes." "
return ''
set statusline=                                " Init.
set statusline+=%3*%{(Modetheme('NRM'))}       " Color 'User3'
set statusline+=%7*%{(Modetheme('VIS'))}       " Color 'User7'
set statusline+=%5*%{(Modetheme('INS'))}       " Color 'User5'
set statusline+=%1*%{(Modetheme('OTH'))}       " Color 'User1'
set statusline+=%1*                            " Color
set statusline+=\                              " Space
set statusline+=%(%8*%m%1*\ %)                 " Modified [+] / [-] flag
set statusline+=[%Y]                              
set statusline+=\ %t                               
set statusline+=%1*                            " Color
set statusline+=%=                             " Right align
set statusline+=%1*                            " Color
set statusline+=\                              " Space
set statusline+=[%{strftime(\"%H:%M\")}]\ 
set statusline+=[%1.5l:%1.5L] " [%3p%%]      
au ColorScheme,FocusGained * hi StatuslineNC   cterm=reverse gui=reverse 
au ColorScheme,FocusGained * hi User1          cterm=bold ctermfg=30  ctermbg=254 guifg=#2E3440 guibg=#5C6370 gui=bold,standout 
au ColorScheme,FocusGained * hi User2          cterm=bold ctermfg=246 ctermbg=251 guifg=#909090 guibg=#c8c8c8 gui=bold 
au ColorScheme,FocusGained * hi User3          cterm=bold ctermfg=30  ctermbg=254 guifg=#2E3440 guibg=#AFE805 gui=bold 
au ColorScheme,FocusGained * hi User4          cterm=bold ctermfg=30  ctermbg=254 guifg=#ECEFF4 guibg=#8BB804 gui=bold 
au ColorScheme,FocusGained * hi User5          cterm=bold ctermfg=166 ctermbg=254 guifg=#2E3440 guibg=#F59C00 gui=bold 
au ColorScheme,FocusGained * hi User6          cterm=bold ctermfg=166 ctermbg=254 guifg=#ECEFF4 guibg=#FF6B12 gui=bold 
au ColorScheme,FocusGained * hi User7          cterm=bold ctermfg=166 ctermbg=254 guifg=#2e3440 guibg=#3BC8F7 gui=bold 
au ColorScheme,FocusGained * hi User8          cterm=bold ctermfg=166 ctermbg=254 guifg=#ECEFF4 guibg=#037499 gui=bold 
aug END 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment