vimstatusline

Vim: How to detect the mode in which the user is in, for statusline?


I need a simple (or complex) way to figure out what mode the user is in. I need to know if the user is in Normal, Insert, Visual, Replace, Change etc.

I am aware of the mode() function, however I can't figure out how to make it output a full string instead of just one character.

My plan was to make a function which would dynamically change the statusline's background and foreground colors depending on what string mode() returns. Basically a function with a bunch of ifs and elseifs which would do it for me. There is a flaw with this approach though, I can't know which color the theme setup by default for that.

So basically, I need some tips/help on how to make a function that does the following:

I tried doing it, but it was a very crude way of doing it and it didn't work at all. It only set the colors once and after that it didn't react every time it changed.

Thanks for your help! :)

EDIT:

Pretty sure what I tried before that didn't work was this:

function! StatuslineModeColor()
    let s:StatuslineMode=mode()
    if s:StatuslineMode == 'n'
        hi Statusline ctermbg=blue guibg=blue
    elseif s:StatuslineMode == 'i'
        hi Statusline ctermbg=red guibg=red
    endif
endfunc

And in the statusline I put the following:

let &stl.='%{StatuslineModeColor()}'

EDIT 2:

I've figured out that basically what I need to do is find a way to grab whatever colors the theme was using before. That's if I use this solution: http://www.reddit.com/r/vim/comments/gexi6/a_smarter_statusline_code_in_comments/c1n2oo5

However this solution is not ideal in my standards, because it's not clean, or as clean as it could be since it makes up a lot of clutter. :/


Solution

  • Update Oct 2016: Since then my dotfiles have moved to https://gitlab.com/greduan/dotfiles, so the new URL for the file is: https://gitlab.com/greduan/dotfiles/blob/76e16dd8a04501db29989824af512c453550591d/vim/after/plugin/statusline.vim

    All the lines are the same.


    Since nobody came up with an answer I made my own solution, you can find it here: https://github.com/Greduan/dotfiles/blob/76e16dd8a04501db29989824af512c453550591d/vim/after/plugin/statusline.vim#L3-L42

    Basically it does the following:

    Lines 3 to 23 define a global variable with a dictionary containing all the different modes, translating it into a readable text. So n which stands for normal gets translated to Normal, i to Insert etc.

    Lines 25 to 42 define the function which will change the colors of the statusline. Currently it only supports Solarized and only if your version has this fork/pull request. If you meet these requirements you will get a red background when you're in insert mode and a green background when in any kind of visual mode, the rest of the modes get the same as normal.

    And lines 118 to 119 put the defined function in the statusline and it also outputs the current mode in a text format using the global variable defined from lines 3 to 23.

    I believe this is a much cleaner solution than the one normally used (auto commands and stuff like that I've seen). Basically the only flaw with it is that there's no way to know the theme's variables, but you can of course do hi! link StatusLine Error for example, which would make the statusline have the same syntax highlighting as your theme's errors.

    Hope this helps!