Vim mapping Cntrl-i to toggle between normal and insert - vim

inoremap <C-i> <Esc> " cntrl-i to switch to normal
nnoremap <C-i> a "cntrl-i to switch to insert
Tried the following to have cntrl-i toggle between normal and insert modes. However, when I toggle into insert mode it works but pastes everything after a, this happens also when replacing a with i. It fails completely when in insert mode and just inserts a tab.
Alternatively if anybody has better suggestions for a mapping to toggle between both, I'm all ears. I wanted to do caps lock however couldn't find a mapping for it in the vim docs.

Dont put comments after maps:
inoremap <C-i> <Esc>
nnoremap <C-i> i

Related

How to exit vim after change command on movement

Let's say I change a word with cw. That leaves me in insert mode.
I find it counter intuitive that if I move to a different line, I'm still in insert mode.
I would like to exit insert mode without pressing ESC, for example when I move to different line with arrow keys.
How could I do this?
You can remap the arrow keys to automatically leave insert mode:
inoremap <Up> <Esc><Up>
inoremap <Right> <Esc><Right>
inoremap <Down> <Esc><Down>
inoremap <Left> <Esc><Left>
You may also want to remap <PageDown> and <PageUp>, <ScrollWheelDown>, and <ScrollWheelDown> if you use those.
One downside of this is that it may not work well with some plugins. That is, the plugin will work fine, but it may move the cursor without leaving insert mode as you expect it to. Plus, if you get used to this you may find using a Vim without these mappings (on a server, or someone else's computer) to be frustrating.

Vim: Remap Ctrl-W in insert mode to behave as it does in normal mode

I would like Ctrl-W to allow me to switch windows even when I am in insert mode. How can I make this change?
My motivation is to not need to press escape before shifting windows.
For <c-w><c-j> (as an example), you can do:
inoremap <c-w><c-j> <esc><c-w><c-j>gi
Then you can repeat this kind of mapping for every command you use:
inoremap <c-w><c-k> <esc><c-w><c-k>gi
inoremap <c-w><c-w> <esc><c-w><c-w>gi
inoremap <c-w>+ <esc><c-w>+gi
inoremap <c-w>- <esc><c-w>-gi
...
If you choose this simple solution, then you can finally add this mapping to inhibit the native <c-w> key (= delete the last word):
inoremap <c-w> <nop>
More "smart" solutions could be written, but they would imply a bit more code.
Note 1: as noted in the comments, the mappings to choose depend on which mode you want to reach after the keystroke: the suffix gi in the commands above means that you want to go back to insert mode in the new window; but you can remove this suffix if you want to be in normal mode.
Note 2: the suffix gi could be simply i, depending on the case : see :h i and :h gi

Assigning keys to function in insert mode also in vim

I am trying to bind control-F to find dialog box in vim. Following code in .vimrc works:
:map <C-F> :promptfind<CR>
However, it works only in the command mode. How can I set it up so that it also works in insert mode?
In insertmode, your binding will just insert :promptfind and then move to the new line. Use <C-O> to execute a single command-mode operation before going back to insert mode (:help i_ctrl-o), or <Esc> in its place to go to command mode and stay there afterwards.
:nnoremap <C-F> :promptfind<CR>
:inoremap <C-F> <C-O>:promptfind<CR>

Adding to mapping rather than completely remapping

I'm working on a plugin to allow bracket completion (I know it's available, it's more of a learning exercise). To properly implement it, I need to add to the backspace mapping. However, as it's an important key, I'd rather keep the existing functionality and just add to it rather than reimplementing the functionality. The steps would basically be when in insert mode and press backspace, execute the original backspace key, then check for some conditions and maybe remove more characters.
I've tried something like imap <backspace> <backspace><call_func_here>, but that doesn't seem to work. Again, I know I could remap backspace to just the function and try to recreate the backspace functionality, but I'd prefer to not do that.
Is this possible in vim?
I think what you are trying to do is the following:
inoremap <silent> <BS> <BS><C-o>:call MyFunction()<CR>
inoremap allows to create a non recurrent mapping in insert mode (it is often a good idea to use nore in your mappings). :h :inoremap
<silent> precise that the mapping will not be echoed on the command line (You will not see :call MyFunction() in the command line) :h :map-silent
<BS> is the reference to the backspace key that you want to remap.
The second <BS> is here to issue a backspace in insert mode
<C-o> switches to normal mode for only a command. :h i_CTRL-O
:call MyFunction() is the call to your function the way you would do it in normal mode.
<CR> correspond to the Enter key which validate the call to your function.

Map key to toggle between normal mode and insert mode in Vim

I'd like to use two "controls" as a toggle key to switch between normal mode and insert mode in Vim. So I add the following two lines into my .vimrc
nmap <C-><C-> i
imap <C-><C-> <ESC>
But it doesn't work. What's wrong with the above two lines?
It seems you are trying to map Ctrl+Space to toggle insert mode.
nnoremap <C-space> i
imap <C-space> <Esc>
(Came from this Vim tip (marked obsolete, but there's a link to a more rich document on avoiding which includes the tip).)
Remember that this is not guaranteed to work across all terminals and platforms. Some terminals and platforms may eat a given Ctrl+something shortcut, while others don't, so find one that works in your environment.
nnoremap <silent><C-space> :startinsert
inoremap <silent><C-space> <C-O>:stopinsert
That's definitely not going to work. You could use an F key instead.
nnoremap <C-SPACE> i
inoremap <C-SPACE> <ESC>l
works perfectly with GVIM 7.4

Resources