I'm trying to configure my vim statusbar, and depending on the current mode I want to change parts of the line:
Now I would also like to configure visual, replace, etc., and I have tried mode() that should (according to the vim docs) return a string describing the current mode:
function DrawStatusline() let currentmode = mode() if currentmode = 'v' " do something for visual mode elseif currentmode = 'i' " do something for insert mode else " do something for normal mode (default) endif endfunction
However mode() only seems to return 'n'; in a forum I have read that vim always changes to normal mode for executing mode(), which means that used like this it isn't really useful.
I don't know if I'm using it wrong (maybe I mode() isn't the right choice), but could anyone give me a hint how I could achieve my goal?
Thanks in advance!
Last edited by ayekat (2015-01-24 20:40:53)
You have an error in your if-conditions (= instead of ==). Therefor only the else statement is executed.
function DrawStatusline() let currentmode = mode() " Check for (any) visual mode if currentmode =~? '.*v' " do something for visual mode elseif currentmode == 'i' " do something for insert mode endif " no else needed :) " do something for normal mode (default) endfunction
Hope that helps.
Hope that helps.
Hey, thanks for pointing out, but I actually made the typo when writing the comment, but not in the actual .vimrc (I know, a sin!)
But even with your proposition ( =~? '.*v'), it did not work.
I have found a - not too elegant - alternative way, where DrawStatusline takes a string as argument that tells what mode I am in, and map v, V and ^V like this:
noremap <silent> v :call DrawStatusline('V')<CR>v noremap <silent> V :call DrawStatusline('V')<CR>V noremap <silent> <C-v> :call DrawStatusline('V')<CR><C-v> " And then: au! InsertEnter * call DrawStatusline('I') au! InsertLeave * call DrawStatusline('N')
It isn't super elegant, but it works so far (=> current version of my vimrc)
But yeah, I still find it weird, how mode() behaves...
Try the following Vim script. I don't know why your attempt doesn't work, but for me mode() returns also the different visual-modes.
function! DrawStatusline() let currentmode = mode() hi User1 ctermbg=black ctermfg=darkgrey hi User6 ctermbg=darkgreen ctermfg=green let mode_name='NORMAL' " Check for (any) visual mode if currentmode ==? 'v' || currentmode == '[MOVE CURSOR INSIDE THESE BRACKETS AND HIT ciSINGLEQUOTE<Ctrl-q><Ctrl-v><Esc>]' hi User1 ctermbg=darkcyan ctermfg=darkgrey hi User6 ctermbg=darkcyan ctermfg=cyan let mode_name='VISUAL' elseif currentmode == 'i' hi User1 ctermbg=darkyellow ctermfg=darkgrey hi User6 ctermbg=darkyellow ctermfg=yellow let mode_name='INSERT' endif return '%1*' . mode_name . '%6*' endfunction set statusline=%!DrawStatusline()
However I would recommend to try the plugin Powerline, which does exactly what you are trying to achieve (and much more).
Last edited by HolyGuacamole (2012-12-05 23:01:40)
I tested your code, now it actually changes the text correctly. However the colour needs another keypress to change.
And yes, I have already tried powerline, but I wanted to do it on my own once
I like to do things by my own that already exist - just to learn.
But thanks anyway!
function! ModeMsg(mode) if a:mode == 'i' hi ModeMsg ctermbg=4 ctermfg=232 elseif a:mode == 'r' hi ModeMsg ctermbg=3 ctermfg=232 else hi ModeMsg ctermbg=5 ctermfg=232 endif endfunction au InsertEnter * call ModeMsg(v:insertmode) au InsertLeave * hi ModeMsg ctermbg=11 ctermfg=232 " default col hi ModeMsg ctermbg=11 ctermfg=232
Edit: working partly, unfortunately...
Last edited by bohoomil (2012-12-06 18:36:32)