Toggle insert/navigation modes by pressing insert key? - vim

By default when I press insert key, vim switches to the INSERT mode.
If I press insert key again, vim switches to the REPLACE mode.
I want to toggle INSERT/NAVIGATION modes by pressing insert key.
I.e.: press insert key -> get INSERT mode. Press insert key again -> get back NAVIGATION mode.
How do I get that?

To change the meaning of the <Insert> key (that's how it's spelled in Vim's key notation), you define an insert mode mapping. Since you want to leave insert mode, you map the key to <Esc>, which takes you out of insert mode.
:inoremap <Insert> <Esc>

imap <insert> <esc>

Related

How do I code <tab> to get over/out of parenthesis/quote in vim

how do I code .vimrc using key to get out of parenthesis in vim
ex) in insert mode
before:
if (Sample Code|) {}
" ^cursor
and using tab
after:
if (Sample Code)| {}
" ^here's the cursor
Personally I would just hit Escape+la by force
of habit. A single key alternative is the right arrow key. If you really want
tab to do the job of the right arrow key, you could use the following mapping:
inoremap <TAB> <C-o><RIGHT>
It use the CTRL+o from insert mode to execute a single
normal mode command before returning to insert mode (see :help i_ctrl-o)

How do I get the google cloud shell to recognize the Esc key correctly in vim

The Esc key is heavily used in vim to get back to normal mode from insert, command or visual mode. When you press the Esc key inside vim on the cloud shell the cursor changes shape and vim blocks. If you continually press on Esc key or if you click on the cursor itself then vim works again.
I am in the habit of using the Esc key for changing back to normal in vim and so this makes using vim on the cloud shell very painful.
I do not have to use the Esc key (I can use Ctrl+c or Ctrl+[ and that works except that the banner prompt still claims vim is in INSERT mode when it has switched to NORMAL mode) but it is hard to change my habits.
vim cursor in INSERT mode just before pressing Esc key
vim cursor becomes hollowed out just after Esc is pressed but still in INSERT mode
NORMAL mode can be obtained by pressing Esc two or three more times and waiting
I have investigated remapping the Esc key inside vim but this messes up vim and I get strange behaviour (see warning not to change Esc key mapping in How to disable Esc and cursor keys in vim).
The backspace key does not work in vim either but that can be fixed inside .vimrc with:
inoremap <bs> <left><del>
If there is no fix for the Esc key mapping then I will have to force myself to learn not to use the Esc key.
The best alternative to the Esc key on the cloud shell seems to be Alt+Space - as that invokes a return to NORMAL mode and switches off the --INSERT mode message on the bottom too.
You'll have to hit escape twice (quickly) to enter ESC mode.

Vim: How to place two newlines and then put cursor in insert mode on first newline using a shortkey?

I want to achieve the following using a key shortcut in Vim:
hi
| <-my cursor is here
there
press shift+o
hi
| <-my cursor is here
there
press enter
hi
| <-my cursor is here
there
The following mapping should be enough
nn <Leader>o O<CR><Esc>
Note
<Leader> is a "special key" that you can set (cf. :h <Leader>)
you can choose your own key combination to trigger the mapping, not necessarily <Leader>o
<Esc> is to go back to normal mode. Given How O and o work maybe you want to remain in insert mode? If so, remove <Esc>.
You are looking for :help mapping. Mapping keys is when you map one key or several keys to act like other keys. In your case, this is a map you might use:
nnoremap (your chosen key(s)) O<CR>
This means that in normal mode, when you hit your chosen key(s), vim will instead act as though you hit O and then <CR>. <CR> is how you indicate the 'Enter/Return' key.
This is assuming you meant you wanted to start from normal mode.

nmap to simulate shift-insert in insert mode

In vim I would like to create a key map for gp in normal mode so that it switches to insert mode and simulate the shift+insert key press and goes back to normal mode.
here is what I tried:
nmap gp i<S-Insert><esc>
All it does is insert the text <S-Insert> instead of pressing executing shift+insert.
I've looked at Paste in insert mode? but the I can't get the contents of what I'm pasting from a buffer.
You have to enter the control characters directly. You do this by pressing <C-V><Wanted Character> in insert mode.
For more on this see i_CTRL-V.

Vim key mappings in insert mode

I have edited my .vimrc file and mapped some commands. They are only working in normal mode. Is there any way to map commands in insert mode? (e.g. commands involved with special keys such as Ctrl) For example, can I copy in insert mode using Ctrl+c?
The first letter in the :map commands determines which modes (:h map-modes) they apply to. So :nnoremap is for normal mode, and :inoremap for insert mode.
You usually cant' just use the same right-hand side; you need to consider that you're in a different mode. To invoke a (normal mode) command from insert mode:
prepend <Esc> if you want to stay in normal mode after the mapping
prepend <C-o> if you want to continue in insert mode after the mapping; this command switches to normal mode for just one command
For example, to map :w to <C-s>, you'd use this: :nnoremap <C-s> :w<CR>. The corresponding insert mode mapping (staying there) is:
:inoremap <C-s> <C-o>:w<CR
See :help imap. You can map keys, including keys with control, to various things within insert mode. For instance, if you wanted to copy the current word in insert mode with Ctrl+c you could use
inoremap <C-c> <esc>yiwea

Resources