Skip to content

Instantly share code, notes, and snippets.

@ahmedelgabri
Last active September 13, 2022 13:15
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
" https://github.com/Greduan/dotfiles/blob/76e16dd8a04501db29989824af512c453550591d/vim/after/plugin/statusline.vim
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'
else
exe 'hi! StatusLine ctermfg=006 guifg=orange gui=None cterm=None'
endif
return ''
endfunction
" Find out current buffer's size and output it.
function! FileSize()
let bytes = getfsize(expand('%:p'))
if (bytes >= 1024)
let kbytes = bytes / 1024
endif
if (exists('kbytes') && kbytes >= 1000)
let mbytes = kbytes / 1000
endif
if bytes <= 0
return '0'
endif
if (exists('mbytes'))
return mbytes . 'MB '
elseif (exists('kbytes'))
return kbytes . 'KB '
else
return bytes . 'B '
endif
endfunction
function! ReadOnly()
if &readonly || !&modifiable
return ''
else
return ''
endfunction
function! GitInfo()
let git = fugitive#head()
if git != ''
return ' '.fugitive#head()
else
return ''
endfunction
" http://stackoverflow.com/a/10416234/213124
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 https://github.com/tpope/vim-fugitive
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 (%)
@Starsign68
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." "
else
return ''
endif
endfunction
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%%]      
aug COLOR
au! 
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