Certainly! Below is a comprehensive Vimscript configuration for your init.vim that sets up a Neovim status line with all the elements you've specified. This status line leverages Nerd Font icons, uses the default colors from any theme by utilizing existing highlight groups, and organizes the information in a clean and stylish manner.
Step-by-Step Setup
-
Ensure Nerd Fonts are Installed:
Make sure you have a Nerd Font installed and set as your terminal's font to properly display the icons. You can download Nerd Fonts from here.
-
Add the Following Configuration to Your init.vim:
" Enable the status line
set laststatus=2
" Define Nerd Font icons
let g:status_icons = {
\ 'mode': {
\ 'n': ' ', " Normal mode
\ 'i': ' ', " Insert mode
\ 'v': ' ', " Visual mode
\ 'V': ' ',
\ "\<C-V>": ' ',
\ 'c': ' ', " Command mode
\ 'R': 'ﯩ ', " Replace mode
\ 't': ' ', " Terminal mode
\ },
\ 'modified': '✎', " Modified flag
\ 'readonly': '', " Readonly flag
\ 'help': '', " Help flag
\ 'error': ' ', " Error icon
\ 'buffer': ' ', " Buffer number
\ 'window': ' ', " Window number
\ }
" Function to retrieve the current mode's icon
function! GetModeIcon()
let l:mode = mode()[0]
return get(g:status_icons.mode, l:mode, ' ')
endfunction
" Function to get the error count from diagnostics
function! GetErrorCount()
if exists('*nvim_diagnostic')
" Get the number of errors in the current buffer
let errors = len(nvim_diagnostic.get(0, { 'severity': vim.diagnostic.severity.ERROR }))
return errors > 0 ? g:status_icons.error . errors : ''
else
" Fallback: You can extend this to use the quickfix list or other sources
return ''
endif
endfunction
" Function to display the modified flag
function! GetModifiedFlag()
return &modifiable && &modified ? ' ' . g:status_icons.modified : ''
endfunction
" Function to display the readonly flag
function! GetReadonlyFlag()
return &readonly ? ' ' . g:status_icons.readonly : ''
endfunction
" Function to display the help flag
function! GetHelpFlag()
return &buftype ==# 'help' ? ' ' . g:status_icons.help : ''
endfunction
" Function to show window and buffer numbers
function! GetWindowBufferInfo()
return ' ' . g:status_icons.window . 'w' . winnr() . ': ' . g:status_icons.buffer . 'b' . bufnr('%')
endfunction
" Assemble the status line
set statusline=
\ '%#StatusLine#' . " Use the default StatusLine highlight group
\ '%{GetModeIcon()}' . ' ' . " Mode icon
\ '%f' . ' ' . " Full file path
\ '[%y]' . ' ' . " File type
\ '%{GetErrorCount()}' . " Error count
\ '%{GetModifiedFlag()}' . " Modified flag
\ '%{GetReadonlyFlag()}' . " Readonly flag
\ '%{GetHelpFlag()}' . " Help flag
\ '%=' . " Split between left and right
\ '%{GetWindowBufferInfo()}' " Window and buffer info
" Optional: Adjust the StatusLine highlight group if needed
" This ensures that the status line uses the default colors from your theme
" Uncomment and modify if you want to customize further
" hi StatusLine ctermfg=none ctermbg=none guifg=none guibg=none
" Optional: Enable always showing the status line
set laststatus=2
Explanation of the Configuration
-
Icons Definition (g:status_icons):
- Modes: Each editor mode (Normal, Insert, Visual, etc.) is assigned a specific Nerd Font icon.
- Flags and Indicators: Icons for modified buffers, read-only status, help buffers, errors, buffer number, and window number are defined.
-
Mode Icon Function (GetModeIcon):
- Determines the current mode using
mode() and returns the corresponding icon. Defaults to a generic icon if the mode isn't explicitly handled.
-
Error Count Function (GetErrorCount):
- Utilizes Neovim's built-in LSP diagnostics to count the number of errors in the current buffer.
- Returns an error icon followed by the count if there are any errors.
- Note: Requires Neovim 0.5+ with LSP configured. You can extend this function to fetch errors from other sources like the quickfix list if needed.
-
Flag Functions (GetModifiedFlag, GetReadonlyFlag, GetHelpFlag):
- Each function checks a specific buffer state and returns the corresponding icon if the condition is met.
-
Window and Buffer Info Function (GetWindowBufferInfo):
- Displays the current window number (
winnr()) and buffer number (bufnr('%')) with their respective icons.
-
Assembling the Status Line:
%#StatusLine#: Applies the default StatusLine highlight group, ensuring theme colors are used.
%{GetModeIcon()}: Inserts the current mode icon.
%f: Displays the full file path.
[%y]: Shows the file type inside square brackets.
%{GetErrorCount()}: Adds the error count if there are any errors.
%{GetModifiedFlag()}, %{GetReadonlyFlag()}, %{GetHelpFlag()}: Append their respective flags based on buffer state.
%=: Splits the status line, pushing the remaining content to the right.
%{GetWindowBufferInfo()}: Displays window and buffer numbers on the right side.
-
Customization Tips
-
Changing Icons:
If you want different icons, simply replace the icons in the g:status_icons dictionary with other Nerd Font glyphs. You can find available icons on the Nerd Fonts Cheat Sheet.
-
Adding More Information:
You can extend the status line by adding more functions and including them in the set statusline line. For example, adding Git branch information or encoding format.
-
Adjusting Layout:
The %= symbol splits the status line into left and right sections. You can rearrange or add more %= splits if you want different sections aligned differently.
-
Handling More Modes:
If you use additional modes or want more detailed mode indicators, extend the g:status_icons.mode dictionary accordingly.
Example Screenshot
While I can't display screenshots here, the configured status line will appear with the mode icon on the left, followed by the file path, file type, error count, and various flags. On the right, you'll see the window and buffer numbers, all aligned neatly with default theme colors and Nerd Font icons enhancing the visual appeal.
Final Notes
This configuration provides a robust and stylish status line tailored to your specifications. It maintains compatibility with any color theme by relying on the default StatusLine highlight group and enhances it with meaningful icons and information. Feel free to tweak and extend it further to suit your workflow!
If you encounter any issues or have specific customization requests, feel free to ask!