I would like to be able to get the current usual mode of vim inside a vim script (and not only inside the statusline). I need this functionality to build a vim script linked with ruby script to be able to change keyboard keys colors depend of vim current mode and motion state. My problem is that vim doesn't know to provide his mode state inside vim script (only inside statusline), or i failed to get it. I think it should have a point there to make vim better by provide a helpful mode() function who can really provide vim current mode (not only in normal mode and not only for statusline).
This way dev's users would be able without pain to build script to recognize current vim mode and then build interactive script. Also, i'm now able to get FocusGained (i'm not Windows user, my OS is archlinux and sometimes FreeBSD) with adding ugly/tricky vimscript and i think this point can also be increase for vim source code to be not only for gvim or Windows users.
Try to imagine you want to create a script for color rgb keyboard keys to show currenty mode and aviable keys to hit depend on mode and motion/other state... how to do that if FocusGained and mode() only works for some very limited situations ?
mode()
returns 'n', because you are in Normal. And you switch there yourself in order to execute the script. Simply recall the place where that happened. – Matt Feb 2 '20 at 9:38:help mode()
: "This is useful in the 'statusline' option or when used with remote_expr(). In most other places it always returns 'c' or 'n'." The usual way to accomplish this is to set'statusline'
to include a call to a function that doesn't produce any output but stores the actual mode somewhere (or possibly executes the external command, though you probably don't want to delay writing the status line waiting for an external command to complete...) – filbranden♦ Feb 3 '20 at 4:16autocmd SafeState
and/orSafeStateAgain
and themode()
will return different values. But if you run your script "synchronously", from mapping or such, you can only do this upon switching into normal (like<CR>
to end cmdline and execute it in normal mode). – Matt Feb 3 '20 at 7:58mode()
returns ani
just fine in insert mode. (E.g., Ctrl-R=mode()
) – muru Feb 3 '20 at 8:21