1

I notice that when in insert, pressing <C-o> will take me to normal mode, but once I finish pressing a sequence of keys, I get taken back to insert mode. I find this very useful, but I want a way of programatically detecting this, so I can reflect the difference in my statusline. The help page for mode() only mentions normal mode, whereas I can see with set showmode, vim displays -- (insert) -- instead of -- INSERT -- so clearly it differentiates between the two to some extent.

Note; I have set noshowmode set because I want to have just my statusline display the mode. I am looking for a way to do this without using showmode.

1

The help page for mode() only mentions normal mode

That's not true. The help page (:h mode()) is quite complete. You have to supply an optional argument for the mode() function.

let s:mode = mode(1)
if s:mode[0] ==# 'n'
    if s:mode[1] ==# 'i'
        " normal using i_CTRL-O
    elseif s:mode[1] ==# 'o'
        " normal Operator-pending
    else
        " just normal
    endif
endif

If :h mode() page does not mention niI, niR etc. then this version of Vim does not support this feature and should be updated first.

  • when I do this from insert-normal it doesn't seem to work. I saw the part about the optional non-zero argument, but it doesn't seem to behave that way on my machine. – myc3lium Oct 8 at 18:03
  • as in, mode(1) in insert-normal just returns 'n' rather than 'ni' – myc3lium Oct 8 at 18:09
  • @myc3lium That's strange. If I do in insert mode <C-O>:echo mode(1) it prints niI just as expected. – Matt Oct 8 at 18:11
  • would the version of vim have anything to do with it? If returning 'ni' was introduced later on as an extension to mode's behaviour... I'm using vim 7.4, so. – myc3lium Oct 8 at 18:19
  • @myc3lium You should read mode() help page as it is in your version of Vim. – Matt Oct 8 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.