I'm writing a plugin for commenting the code in special way and it should act a bit different when called in visual or normal modes.
Is there a function or some way to determine the mode in which the user is (was) when the function was called?
Changing the modes The most commonly used command to enter in to insert mode is “i”. To shift back to normal mode, press Esc. To switch to the visual mode from Normal mode, different commands are v, V, Shift + v, and Ctrl + v. The most commonly used command to enter in to insert mode is “v”.
Use Alt/Meta In a Terminal If you use Vim in a terminal, simply press alt/meta+normal_mode_key. Most terminals send an escape character followed by the normal_mode_key that you pressed, removing the need to press escape yourself.
Visual mode is a mode in which the Vim (not Vi) editor allows you to select text, either by character, by line, or by block (you may then perform various actions on the selected text). Pressing Esc would exit this mode and return you command/normal mode. You may also type v in character visual mode to leave this mode.
List of modes: Command mode - issues commands to Vim (use Esc to get to Command mode) Execute mode - executes commands in Vim (use colon : to get to Execute mode) Insert mode - inserts text in a file (use i to get to Insert mode)
There's
mode([expr]) Return a string that indicates the current mode.
http://vimdoc.sourceforge.net/htmldoc/eval.html#mode%28%29
but that might not work since you're probably going to start command mode in the rhs of the mapping.
A more robust way would be to set up slightly different mappings like
nmap <Leader>c :call MyFunc('n')<CR>
vmap <Leader>c :call MyFunc('v')<CR>
and use the argument value to know what mode the user was in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With