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

让人想犯罪 __ 提交于 2019-12-03 13:17:20

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!