Advertisement
Guest User

Vim statusline from scratch. Without any plugins. v2

a guest
Feb 5th, 2021
2,836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 9.49 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 statusline 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 statusline - ~100ms. Without any statusline - ~98ms.
  9.  
  10. " 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 vimrc.
  11. " e.g. source "~/.config/vim/statusline.vim"
  12.  
  13.  
  14.  
  15. " Color highlighting groups
  16. " Add this AFTER `colorscheme` option in your vimrc. Otherwise your colorscheme will clear this highlightings. OR use ColorScheme autocommand. VERY IMPORTANT.
  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. " Define 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. " Create 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. " Disable showmode - i.e. Don't show mode like --INSERT-- in current statusline.
  67. set noshowmode
  68.  
  69. " Enable GUI colors for terminals (Some terminals may not support this, so you'll have to *manually* set color pallet for tui colors. Lie tuibg=255, tuifg=120, etc.).
  70. set termguicolors
  71.  
  72.  
  73.  
  74. " Understand statusline elements
  75.  
  76. " %{StslineMode()}  = Output of a function
  77. " %#StslinePriColorBG# = Highlight group
  78. " %F, %c, etc. are variables which contain value like - current file path, current colums, etc.
  79. " %{&readonly?\"\ \":\"\"} = If file is readonly ? Then "Lock icon" Else : "Nothing"
  80. " %{get(b:,'coc_git_status',b:GitBranch)}    = If b:coc_git_status efists, then it's value, else value of b:GitBranch
  81. " &filetype, things starting with & are also like variables with info.
  82. " \ - Is for escaping a space. \" is for escaping a double quote.
  83. " %{&fenc!='utf-8'?\"\ \":''}   = If file encoding is NOT!= 'utf-8' ? THEN output a "Space" else : no character
  84.  
  85.  
  86.  
  87. " Define active statusline
  88.  
  89. function! ActivateStatusline()
  90. call GetFileType()
  91. setlocal statusline=%#StslinePriColorBG#\ %{StslineMode()}%#StslineSecColorBG#%{get(b:,'coc_git_status',b:GitBranch)}%{get(b:,'coc_git_blame','')}%#StslineBackColorFGPriColorBG#%#StslinePriColorFG#\ %{&readonly?\"\ \":\"\"}%F\ %#StslineModColorFG#%{&modified?\"\ \":\"\"}%=%#StslinePriColorFG#\ %{b:FiletypeIcon}%{&filetype}\ %#StslineSecColorFG#%#StslineSecColorBG#%{&fenc!='utf-8'?\"\ \":''}%{&fenc!='utf-8'?&fenc:''}%{&fenc!='utf-8'?\"\ \":''}%#StslinePriColorFGSecColorBG#%#StslinePriColorBG#\ %p\%%\ %#StslinePriColorBGBold#%l%#StslinePriColorBG#/%L\ :%c\
  92. endfunction
  93.  
  94.  
  95.  
  96. " Define Inactive statusline
  97.  
  98. function! DeactivateStatusline()
  99.  
  100. if !exists("b:GitBranch") || b:GitBranch == ''
  101. setlocal statusline=%#StslineSecColorBG#\ INACTIVE\ %#StslineSecColorBG#%{get(b:,'coc_git_statusline',b:GitBranch)}%{get(b:,'coc_git_blame','')}%#StslineBackColorFGSecColorBG#%#StslineBackColorBG#\ %{&readonly?\"\ \":\"\"}%F\ %#StslineModColorFG#%{&modified?\"\ \":\"\"}%=%#StslineBackColorBG#\ %{b:FiletypeIcon}%{&filetype}\ %#StslineSecColorFGBackColorBG#%#StslineSecColorBG#\ %p\%%\ %l/%L\ :%c\
  102.  
  103. else
  104. setlocal statusline=%#StslineSecColorBG#%{get(b:,'coc_git_statusline',b:GitBranch)}%{get(b:,'coc_git_blame','')}%#StslineBackColorFGSecColorBG#%#StslineBackColorBG#\ %{&readonly?\"\ \":\"\"}%F\ %#StslineModColorFG#%{&modified?\"\ \":\"\"}%=%#StslineBackColorBG#\ %{b:FiletypeIcon}%{&filetype}\ %#StslineSecColorFGBackColorBG#%#StslineSecColorBG#\ %p\%%\ %l/%L\ :%c\
  105. endif
  106.  
  107. endfunction
  108.  
  109.  
  110.  
  111. " Get Statusline mode & also set primary color for that mode
  112. function! StslineMode()
  113.  
  114.     let l:CurrentMode=mode()
  115.  
  116.     if l:CurrentMode==#"n"
  117.         let g:StslinePriColor     = g:StslineColorGreen
  118.         let b:CurrentMode = "NORMAL "
  119.  
  120.     elseif l:CurrentMode==#"i"
  121.         let g:StslinePriColor     = g:StslineColorViolet
  122.         let b:CurrentMode = "INSERT "
  123.  
  124.     elseif l:CurrentMode==#"c"
  125.         let g:StslinePriColor     = g:StslineColorYellow
  126.  
  127.         let b:CurrentMode = "COMMAND "
  128.  
  129.     elseif l:CurrentMode==#"v"
  130.         let g:StslinePriColor     = g:StslineColorBlue
  131.         let b:CurrentMode = "VISUAL "
  132.  
  133.     elseif l:CurrentMode==#"V"
  134.         let g:StslinePriColor     = g:StslineColorBlue
  135.         let b:CurrentMode = "V-LINE "
  136.  
  137.     elseif l:CurrentMode==#"\<C-v>"
  138.         let g:StslinePriColor     = g:StslineColorBlue
  139.         let b:CurrentMode = "V-BLOCK "
  140.  
  141.     elseif l:CurrentMode==#"R"
  142.         let g:StslinePriColor     = g:StslineColorViolet
  143.         let b:CurrentMode = "REPLACE "
  144.  
  145.     elseif l:CurrentMode==#"s"
  146.         let g:StslinePriColor     = g:StslineColorBlue
  147.         let b:CurrentMode = "SELECT "
  148.  
  149.     elseif l:CurrentMode==#"t"
  150.         let g:StslinePriColor     =g:StslineColorYellow
  151.         let b:CurrentMode = "TERM "
  152.  
  153.     elseif l:CurrentMode==#"!"
  154.         let g:StslinePriColor     = g:StslineColorYellow
  155.         let b:CurrentMode = "SHELL "
  156.  
  157.     endif
  158.  
  159.  
  160.     call UpdateStslineColors()
  161.    
  162.     return b:CurrentMode
  163.  
  164. endfunction
  165.  
  166.  
  167.  
  168. " Update colors. Recreate highlight groups with new Primary color value.
  169. function! UpdateStslineColors()
  170.  
  171. execute 'highlight StslinePriColorBG           guifg=' . g:StslineOnPriColor ' guibg=' . g:StslinePriColor
  172. execute 'highlight StslinePriColorBGBold       guifg=' . g:StslineOnPriColor ' guibg=' . g:StslinePriColor ' gui=bold'
  173. execute 'highlight StslinePriColorFG           guifg=' . g:StslinePriColor   ' guibg=' . g:StslineBackColor
  174. execute 'highlight StslinePriColorFGSecColorBG guifg=' . g:StslinePriColor   ' guibg=' . g:StslineSecColor
  175. execute 'highlight StslineSecColorFGPriColorBG guifg=' . g:StslineSecColor   ' guibg=' . g:StslinePriColor
  176.  
  177. if !exists("b:GitBranch") || b:GitBranch == ''
  178. execute 'highlight StslineBackColorFGPriColorBG guifg=' . g:StslineBackColor ' guibg=' . g:StslinePriColor
  179. endif
  180.  
  181. endfunction
  182.  
  183.  
  184.  
  185. " Get git branch name
  186.  
  187. function! GetGitBranch()
  188. let b:GitBranch=""
  189. try
  190.     let l:dir=expand('%:p:h')
  191.     let l:gitrevparse = system("git -C ".l:dir." rev-parse --abbrev-ref HEAD")
  192.     if !v:shell_error
  193.         let b:GitBranch="   ".substitute(l:gitrevparse, '\n', '', 'g')." "
  194.         execute 'highlight StslineBackColorFGPriColorBG guifg=' . g:StslineBackColor ' guibg=' . g:StslineSecColor
  195.     endif
  196. catch
  197. endtry
  198. endfunction
  199.  
  200.  
  201.  
  202. " Get filetype & custom icon. Put your most used file types first for optimized performance.
  203.  
  204. function! GetFileType()
  205.  
  206. if &filetype == 'typescript'
  207. let b:FiletypeIcon = ' '
  208.  
  209. elseif &filetype == 'html'
  210. let b:FiletypeIcon = ' '
  211.  
  212. elseif &filetype == 'scss'
  213. let b:FiletypeIcon = ' '
  214.  
  215. elseif &filetype == 'css'
  216. let b:FiletypeIcon = ' '
  217.  
  218. elseif &filetype == 'javascript'
  219. let b:FiletypeIcon = ' '
  220.  
  221. elseif &filetype == 'javascriptreact'
  222. let b:FiletypeIcon = ' '
  223.  
  224. elseif &filetype == 'markdown'
  225. let b:FiletypeIcon = ' '
  226.  
  227. elseif &filetype == 'sh' || &filetype == 'zsh'
  228. let b:FiletypeIcon = ' '
  229.  
  230. elseif &filetype == 'vim'
  231. let b:FiletypeIcon = ' '
  232.  
  233. elseif &filetype == ''
  234. let b:FiletypeIcon = ''
  235.  
  236. elseif &filetype == 'rust'
  237. let b:FiletypeIcon = ' '
  238.  
  239. elseif &filetype == 'ruby'
  240. let b:FiletypeIcon = ' '
  241.  
  242. elseif &filetype == 'cpp'
  243. let b:FiletypeIcon = ' '
  244.  
  245. elseif &filetype == 'c'
  246. let b:FiletypeIcon = ' '
  247.  
  248. elseif &filetype == 'go'
  249. let b:FiletypeIcon = ' '
  250.  
  251. elseif &filetype == 'lua'
  252. let b:FiletypeIcon = ' '
  253.  
  254. elseif &filetype == 'haskel'
  255. let b:FiletypeIcon = ' '
  256.  
  257. else
  258. let b:FiletypeIcon = ' '
  259.  
  260. endif
  261. endfunction
  262.  
  263.  
  264.  
  265. " Get git branch name after entering a buffer
  266. augroup GetGitBranch
  267.     autocmd!
  268.     autocmd BufEnter * call GetGitBranch()
  269. augroup END
  270.  
  271.  
  272. " Set active / inactive statusline after entering, leaving buffer
  273. augroup SetStslineline
  274.     autocmd!
  275.     autocmd BufEnter,WinEnter * call ActivateStatusline()
  276.     autocmd BufLeave,WinLeave * call DeactivateStatusline()
  277. augroup END
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement