If I save the result of
:call mode() in a variable, how can I restore that mode later?
You can't set an arbitrary mode programmatically, or at least not with a dedicated function like
:call setmode('n'), which doesn't exist.
What you can do, however, is…
- start insert mode with
- start replace mode with
- like the two methods above, but switch to another mode from insert or replace mode,
- execute a normal mode command with
:help :normal, which should leave you in normal mode,
- same as above but execute a normal mode command that switches to another mode, like
vfor visual mode,
- and other similar kinds of tricks…