You are not logged in.

#1 2012-12-05 17:31:18

ayekat
Member
From: UTC+1
Registered: 2011-01-17
Posts: 757
Website

[SOLVED] [vim] get current mode

Hi

I'm trying to configure my vim statusbar, and depending on the current mode I want to change parts of the line:

Normal:
screen_vim_statusline_normal.png

Insert mode:
screen_vim_statusline_insert.png

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)

Offline

#2 2012-12-05 19:33:23

HolyGuacamole
Member
Registered: 2012-03-29
Posts: 6

Re: [SOLVED] [vim] get current mode

Hi ayekat,

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.

HolyGuac

Offline

#3 2012-12-05 20:12:30

ayekat
Member
From: UTC+1
Registered: 2011-01-17
Posts: 757
Website

Re: [SOLVED] [vim] get current mode

HolyGuacamole wrote:

Hope that helps.

HolyGuac


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!) smile

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 smile (=> current version of my vimrc)

But yeah, I still find it weird, how mode() behaves...

Offline

#4 2012-12-05 22:59:39

HolyGuacamole
Member
Registered: 2012-03-29
Posts: 6

Re: [SOLVED] [vim] get current mode

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).

HolyGuac

Last edited by HolyGuacamole (2012-12-05 23:01:40)

Offline

#5 2012-12-06 16:18:57

ayekat
Member
From: UTC+1
Registered: 2011-01-17
Posts: 757
Website

Re: [SOLVED] [vim] get current mode

Hi

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 smile
I like to do things by my own that already exist - just to learn.

But thanks anyway!

Offline

#6 2012-12-06 18:13:21

bohoomil
Member
Registered: 2010-09-04
Posts: 2,376
Website

Re: [SOLVED] [vim] get current mode

Check this:

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)


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

Board footer

Powered by FluxBB