Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: how to get the mode I'm currently in

Tags:

vim

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?

like image 945
lithuak Avatar asked May 30 '12 21:05

lithuak


People also ask

How do you switch between modes in Vim?

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”.

How do I go back to normal mode in Vim?

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.

How do I exit Visual mode in Vim?

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.

What are the 2 modes of Vim?

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)


1 Answers

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.

like image 74
Geoff Reedy Avatar answered Oct 23 '22 11:10

Geoff Reedy