vimstatusline

The vim statusline wouldn't change mode text


I wrote a simple vim statusline plugin by my self, just add color and mode in the statusline, and after finishing it, it all went well, but now the statusline wouldn't change as I switch mode. And I put the script file in ~/.vim/plugin/ The vim version is 7.4 Huge I did this in virtual mathine with a centos7 OS Here are my code:

hi NormalColor guifg=Black guibg=Green ctermbg=46 ctermfg=0
hi InsertColor guifg=Black guibg=Cyan ctermbg=51 ctermfg=0
hi ReplaceColor guifg=Black guibg=maroon1 ctermbg=165 ctermfg=0
hi VisualColor guifg=Black guibg=Orange ctermbg=202 ctermfg=0
hi DefaultColor guifg=Black guibg=Orange ctermbg=202 ctermfg=0
hi DefaultLineColor guifg=Black guibg=Orange ctermbg=187 ctermfg=0

let s:mode_map = {
\   'n': '  NORMAL  ',
\   'i': '  INSERT  ',
\   'R': '  REPLACE  ',
\   'v': '  VISUAL  ',
\   'V': '  V-LINE  ',
\   "\<C-v>": '  V-BLOCK  ',
\   'c': '  COMMAND  ',
\   's': '  SELECT  ',
\   'S': '  S-LINE  ',
\   "\<C-s>": '  S-BLOCK  ',
\   't': '  TERMINAL '
\ }

let s:mode_color = {
\    'n': '%#NormalColor#',
\    'i': '%#InsertColor#',
\    'R': '%#ReplaceColor#',
\    'v': '%#VisualColor#',
\    'V': '%#VisualLineColor#',
\    "\<C-v>": '%#VisualBlockColor#',
\    'c': '%#CommandColor#',
\    's': '%#SelectColor#',
\    'S': '%#SelectLineColor#',
\    "\<C-s>": '%#SelectBlockColor#',
\    't': '%#TerminalColor#'
\ }
let s:line_default_color = '%#DefaultLineColor#'
let s:default_statusline_label = ' %f %M %y%=%3l/%-5L %p%% '

function! SetDefaultStatuslineLabel()
    return ' %f %M %y%=%3l/%-5L %p%% '
endfunction

function! GetModeName()
    return get(s:mode_map, mode(), '')
endfunction

function! GetModeColor()
    return get(s:mode_color, mode(), 'DefaultColor')
endfunction

let s:mode_partition = GetModeColor().GetModeName()
function! ConcatAll()
    return s:mode_partition.
    \   s:line_default_color.
    \   s:default_statusline_label
endfunction

" set statusline=%!ConcatAll()
augroup Statusline
    autocmd!
    autocmd VimEnter,WinEnter,BufWinEnter * set statusline=%!ConcatAll()
augroup END

Solution

  • You are setting the name of the mode at the time the autocmd gets executed.

    If you want to update the mode dynamically with your custom function you need to call them at runtime. To do so wrap the call in %{}, e.g. let s:mode_partition = '%{ GetModeColor().GetModeName() }'