I'm writting scripts for vim, and I want to let the script do different things in different mode. I know that the statusline will show the current mode, but I don't know how to get the exact current mode and store it in variables. For example, when I enter the visual-line
mode, can I get the current mode from some system variables of vim and distinct the mode from other visual
mode?
1 Answer
You are looking for the function mode()
, see :h mode()
:
mode([expr]) Return a string that indicates the current mode.
If [expr] is supplied and it evaluates to a non-zero Number or
a non-empty String (|non-zero-arg|), then the full mode is
returned, otherwise only the first letter is returned.
n Normal
no Operator-pending
v Visual by character
V Visual by line
CTRL-V Visual blockwise
s Select by character
S Select by line
CTRL-S Select blockwise
i Insert
ic Insert mode completion |compl-generic|
ix Insert mode |i_CTRL-X| completion
R Replace |R|
Rc Replace mode completion |compl-generic|
Rv Virtual Replace |gR|
Rx Replace mode |i_CTRL-X| completion
c Command-line editing
cv Vim Ex mode |gQ|
ce Normal Ex mode |Q|
r Hit-enter prompt
rm The -- more -- prompt
r? A |:confirm| query of some sort
! Shell or external command is executing
t Terminal mode: keys go to the job
So all you need to do is:
let currentMode = mode()