Guest User

Vim statusline from scratch. No plugins used.

a guest
Feb 5th, 2021
1,777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 8.06 KB | None | 0 0
  1. "" This was made by Reddit user u/SamLovesNotion. Also with the help of - https://tdaly.co.uk/projects/vim-statusline-generator/ for learning the syntax. Sorry for English & grammar, this post was made in hurry.""
  2.  
  3. " Images - https://www.reddit.com/r/vim/comments/ld8h2j/i_made_a_status_line_from_scratch_no_plugins_used/
  4. " I have used Nerd icon fonts. Icons won't work without them. https://github.com/ryanoasis/nerd-fonts/
  5.  
  6. " This statuline looks exactly like Vim Airline (even more customizable & powerful) & loads faster than Vim airline. Only take few ms to load.
  7.  
  8. " STARTUP TIME - With Vim Airline - ~250ms. With this statuline - ~100ms. Without any statuline - ~97ms.
  9.  
  10. """""""""""""
  11.  
  12. " Add all of this at the end of your vimrc OR Create separate file like 'statusline.vim' & 'colorsgroup.vim' & source those files in your main vimc.
  13. " source "~/.config/vim/statusline.vim"
  14.  
  15. " Color highlighting groups
  16. " Add this AFTER `set colorscheme` option in your vimrc. Otherwise your colorscheme will clear this highlightings.
  17.  
  18. " Color pallet
  19. " Green  - #2BBB4F (BG) - #080808 (FG)
  20. " Blue   - #4799EB
  21. " Violet - #986FEC
  22. " Yellow - #D7A542
  23. " Orange - #EB754D
  24. " Grey1  - #202020
  25. " Grey   - #303030
  26.  
  27.  
  28. " Difeine color variables
  29. let g:StslineColorGreen  = "#2BBB4F"
  30. let g:StslineColorBlue   = "#4799EB"
  31. let g:StslineColorViolet = "#986FEC"
  32. let g:StslineColorYellow = "#D7A542"
  33. let g:StslineColorOrange = "#EB754D"
  34.  
  35. let g:StslineColorLight  = "#C0C0C0"
  36. let g:StslineColorDark   = "#080808"
  37. let g:StslineColorDark1  = "#181818"
  38. let g:StslineColorDark2  = "#202020"
  39. let g:StslineColorDark3  = "#303030"
  40.  
  41.  
  42. " Define colors
  43. let g:StslineBackColor   = g:StslineColorDark2
  44. let g:StslineOnBackColor = g:StslineColorLight
  45. "let g:StslinePriColor   = g:StslineColorGreen
  46. let g:StslineOnPriColor  = g:StslineColorDark
  47. let g:StslineSecColor    = g:StslineColorDark3
  48. let g:StslineOnSecColor  = g:StslineColorLight
  49.  
  50.  
  51. " Crete highlight groups
  52. execute 'highlight StslineSecColorFG guifg=' . g:StslineSecColor   ' guibg=' . g:StslineBackColor
  53. execute 'highlight StslineSecColorBG guifg=' . g:StslineColorLight ' guibg=' . g:StslineSecColor
  54. execute 'highlight StslineBackColorBG guifg=' . g:StslineColorLight ' guibg=' . g:StslineBackColor
  55. execute 'highlight StslineBackColorFGSecColorBG guifg=' . g:StslineBackColor ' guibg=' . g:StslineSecColor
  56. execute 'highlight StslineSecColorFGBackColorBG guifg=' . g:StslineSecColor ' guibg=' . g:StslineBackColor
  57. execute 'highlight StslineModColorFG guifg=' . g:StslineColorYellow ' guibg=' . g:StslineBackColor
  58.  
  59.  
  60.  
  61. " Statusline
  62.  
  63. " Enable statusline
  64. set laststatus=2
  65.  
  66.  
  67. " Understnd statusline elements
  68.  
  69. " %{StslineMode()}  = Output of a function
  70. " %#StslinePriColorBG# = Highlight group
  71. " %F, %c, etc. are variables which contain value like - current file path, current colums, etc.
  72. " %{&readonly?\"\ \":\"\"} = If file is readonly ? Then "Lock icon" Else : "Nothing"
  73. " %{get(b:,'coc_git_status',b:gitbranch)}    = If b:coc_git_status efists, then it's value, else value of b:gitbranch
  74. " &filetype, things starting with & are also like variables with info.
  75. " \ - Is for escaping a space. \" is for escaping a double quote.
  76. " %{&fenc!='utf-8'?\"\ \":''}   = If file encoding is NOT!= 'utf-8' ? THEN output a "Space" else : no character
  77.  
  78.  
  79.  
  80. " Define active statusline
  81.  
  82. function! ActivateStatusline()
  83. setlocal statusline=%#StslinePriColorBG#\ %{StslineMode()}%#StslineSecColorBG#%{get(b:,'coc_git_status',b:gitbranch)}%{get(b:,'coc_git_blame','')}%#StslineBackColorFGPriColorBG#%#StslinePriColorFG#\ %{&readonly?\"\ \":\"\"}%F\ %#StslineModColorFG#%{&modified?\"\ \":\"\"}%=%#StslinePriColorFG#\ %{&filetype!=''?'\ ':''}%{&filetype}\ %#StslineSecColorFG#%#StslineSecColorBG#%{&fenc!='utf-8'?\"\ \":''}%{&fenc!='utf-8'?&fenc:''}%{&fenc!='utf-8'?\"\ \":''}%#StslinePriColorFGSecColorBG#%#StslinePriColorBG#\ %p\%%\ %#StslinePriColorBGBold#%l%#StslinePriColorBG#/%L\ :%c\
  84. endfunction
  85.  
  86.  
  87.  
  88. " Define Inactive statusline
  89.  
  90. function! DeactivateStatusline()
  91.  
  92. if !exists("b:gitbranch") || b:gitbranch == ''
  93. setlocal statusline=%#StslineSecColorBG#\ INACTIVE\ %#StslineSecColorBG#%{get(b:,'coc_git_statusline',b:gitbranch)}%{get(b:,'coc_git_blame','')}%#StslineBackColorFGSecColorBG#%#StslineBackColorBG#\ %{&readonly?\"\ \":\"\"}%F\ %#StslineModColorFG#%{&modified?\"\ \":\"\"}%=%#StslineBackColorBG#\ %{&filetype!=''?'\ ':''}%{&filetype}\ %#StslineSecColorFGBackColorBG#%#StslineSecColorBG#\ %p\%%\ %l/%L\ :%c\
  94.  
  95. else
  96. setlocal statusline=%#StslineSecColorBG#%{get(b:,'coc_git_statusline',b:gitbranch)}%{get(b:,'coc_git_blame','')}%#StslineBackColorFGSecColorBG#%#StslineBackColorBG#\ %{&readonly?\"\ \":\"\"}%F\ %#StslineModColorFG#%{&modified?\"\ \":\"\"}%=%#StslineBackColorBG#\ %{&filetype!=''?'\ ':''}%{&filetype}\ %#StslineSecColorFGBackColorBG#%#StslineSecColorBG#\ %p\%%\ %l/%L\ :%c\
  97. endif
  98.  
  99. endfunction
  100.  
  101.  
  102.  
  103. " Get Statusline mode & also set primary color for that that mode
  104. function! StslineMode()
  105.  
  106.     let l:CurrentMode=mode()
  107.  
  108.     if l:CurrentMode==#"n"
  109.         let g:StslinePriColor     = g:StslineColorGreen
  110.         let b:CurrentMode = "NORMAL "
  111.  
  112.     elseif l:CurrentMode==#"i"
  113.         let g:StslinePriColor     = g:StslineColorViolet
  114.         let b:CurrentMode = "INSERT "
  115.  
  116.     elseif l:CurrentMode==#"c"
  117.         let g:StslinePriColor     = g:StslineColorYellow
  118.  
  119.         let b:CurrentMode = "COMMAND "
  120.  
  121.     elseif l:CurrentMode==#"v"
  122.         let g:StslinePriColor     = g:StslineColorBlue
  123.         let b:CurrentMode = "VISUAL "
  124.  
  125.     elseif l:CurrentMode==#"V"
  126.         let g:StslinePriColor     = g:StslineColorBlue
  127.         let b:CurrentMode = "V-LINE "
  128.  
  129.     elseif l:CurrentMode==#"\<C-v>"
  130.         let g:StslinePriColor     = g:StslineColorBlue
  131.         let b:CurrentMode = "V-BLOCK "
  132.  
  133.     elseif l:CurrentMode==#"R"
  134.         let g:StslinePriColor     = g:StslineColorViolet
  135.         let b:CurrentMode = "REPLACE "
  136.  
  137.     elseif l:CurrentMode==#"s"
  138.         let g:StslinePriColor     = g:StslineColorBlue
  139.         let b:CurrentMode = "SELECT "
  140.  
  141.     elseif l:CurrentMode==#"t"
  142.         let g:StslinePriColor     =g:StslineColorYellow
  143.         let b:CurrentMode = "TERM "
  144.  
  145.     elseif l:CurrentMode==#"!"
  146.         let g:StslinePriColor     = g:StslineColorYellow
  147.         let b:CurrentMode = "SHELL "
  148.  
  149.     endif
  150.  
  151.  
  152.     call UpdateStslineColors()
  153.  
  154.     if expand('%:y') == 'help'
  155.         let b:CurrentMode = 'HELP'
  156.     endif
  157.    
  158.     return b:CurrentMode
  159.  
  160. endfunction
  161.  
  162.  
  163.  
  164. " Update colors. Recreate highlight groups with new Primary color value.
  165. function! UpdateStslineColors()
  166.  
  167. execute 'highlight StslinePriColorBG           guifg=' . g:StslineOnPriColor ' guibg=' . g:StslinePriColor
  168. execute 'highlight StslinePriColorBGBold       guifg=' . g:StslineOnPriColor ' guibg=' . g:StslinePriColor ' gui=bold'
  169. execute 'highlight StslinePriColorFG           guifg=' . g:StslinePriColor   ' guibg=' . g:StslineBackColor
  170. execute 'highlight StslinePriColorFGSecColorBG guifg=' . g:StslinePriColor   ' guibg=' . g:StslineSecColor
  171. execute 'highlight StslineSecColorFGPriColorBG guifg=' . g:StslineSecColor   ' guibg=' . g:StslinePriColor
  172.  
  173. if !exists("b:gitbranch") || b:gitbranch == ''
  174. execute 'highlight StslineBackColorFGPriColorBG guifg=' . g:StslineBackColor ' guibg=' . g:StslinePriColor
  175. endif
  176.  
  177. endfunction
  178.  
  179.  
  180.  
  181. " Get git branch name
  182.  
  183. function! GetGitBranch()
  184. let b:gitbranch=""
  185. try
  186.     let l:dir=expand('%:p:h')
  187.     let l:gitrevparse = system("git -C ".l:dir." rev-parse --abbrev-ref HEAD")
  188.     if !v:shell_error
  189.         let b:gitbranch="   ".substitute(l:gitrevparse, '\n', '', 'g')." "
  190.         execute 'highlight StslineBackColorFGPriColorBG guifg=' . g:StslineBackColor ' guibg=' . g:StslineSecColor
  191.     endif
  192. catch
  193. endtry
  194. endfunction
  195.  
  196.  
  197.  
  198. " Get git branch name after entering a buffer
  199. augroup GetGitBranch
  200.     autocmd!
  201.     autocmd BufEnter * call GetGitBranch()
  202. augroup END
  203.  
  204.  
  205. " Set active / inactive statusline after entering, leaving buffer
  206. augroup SetStslineline
  207.     autocmd!
  208.     autocmd BufEnter,WinEnter * call ActivateStatusline()
  209.     autocmd BufLeave,WinLeave * call DeactivateStatusline()
  210. augroup END
  211.  
Advertisement
Add Comment
Please, Sign In to add comment