Vim key mappings in insert mode - vim

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

Related

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>

Select a different keystroke than colon ':' to enter command-line mode in vi

Is there any way to achieve that remapping? I looked a bit into
map
and
inoremap
but they appear to affect within a given mode, not how to enter a given mode.
You could use nmap, e.g.,
:nmap ; :
to map semicolon to colon. The 'n' in nmap indicates normal mode.
If you wanted to use, say, the <F2> function key to enter command-line mode from insert mode, you can do:
:imap <F2> <Esc>:
There's an exhaustive 3-part tutorial about key mappings in vim here.

Create a command in vim for all modes

If I want to remap <C-s> to :w<CR> I'd have to do something like this
nnoremap <C-s> :w<CR>
inoremap <C-s> <Esc>:w<CR>
since insert mode would requre escaping to normal mode before entering the command (sure, another <Esc> wouldn't kill anything, but it's ugly, my terminal bell goes off and with all the other modes available [n, i, v, s, x, c and o] there are plenty of cases where extra <Esc> wouldn't cut it).
Is there an easy way to map a command "for all modes" in Vim?
You can get quite close by taking advantage of the CTRL-\ CTRL-N command. CTRL-\ CTRL-N goes to Normal mode from any mode.
We can define just two mappings with identical right-hand side to cover Normal, Visual, Select, Operator-pending, Insert, and Command-line mode.
noremap <C-S> <C-\><C-N>:write<CR>
noremap! <C-S> <C-\><C-N>:write<CR>
See :h CTRL-\_CTRL-N.
There is no easy "One mapping to rule them all" way to do such a thing. On the plus side when you do make all your mappings you put them in your ~/.vimrc and forget about it.
However I must say it is the Vim Way to do a save from normal mode (as a matter a fact do most thing from normal mode). If you did it form insert mode for example you would be breaking up your undo block if you wanted to exit from insert mode, save, and then reinsert insert mode (See :h i_Ctrl-o). Not to mention such a mapping may affect the . command which is super handy.
You may also want to avoid the <c-s> key on the terminal because it will often trigger terminal's software flow control (XON/XOFF). You can disable this for your terminal by using stty -ixon.

Creating new mappings in Vim

I am trying to create a mapping the allows me to execute my current node.js file when I press comma + n. I am trying to use the following:
:map <cn> :!node .
When I type this in it simply jumps my cursor to a random line in my current file. How do I create this mapping?
You've specified the mapping keys in the wrong format, see :help key-notation. To trigger the mapping via , followed by N, use this:
:nnoremap ,n :!node %<CR>
Additional notes:
You should use :noremap; it makes the mapping immune to remapping and recursion.
Likewise, you should be as specific in the modes, so :nmap instead of :map for normal mode only.
A mapping works as typed. As you invoke an Ex command from normal mode, you need to conclude command-line mode via <CR>, just as you would press Enter when typing this interactively.
You probably want to pass the current file to node; that's done by the special % identifier, not by .. See :help cmdline-special

Vim EasyMotion: Insert mode after movement?

I have remapped the EasyMotion commands ,,w and ,,b with the following:
imap ,w <ESC><leader><leader>w
imap ,b <ESC><leader><leader>b
This way i'm able to use EasyMotion in Insert mode and navigate quickly without entering Normal mode.
Though, after the move, Vim stays in Normal mode (naturally). How can I specify that after EasyMotion's
employment, Vim should enter Insert mode, in order to continue typing without delay?
Thanx!
You can use <C-o> to execute one normal mode command from insert mode. Once this command has been executed, you will be returned to insert mode:
imap ,w <C-o><leader><leader>w
imap ,b <C-o><leader><leader>b

Resources