I am trying to map my space key to make the Vim go into insert mode from normal mode.
The reason I want to do this is because sometimes I forget that I'm in normal mode and start typing as if I'm in insert mode. When I press the space key in between or even in the start of the page, it moves down and something or the other types due to the possibility of a press a or i in what I just typed.
So to avoid this I want to map my space key to insert mode from normal mode as we press i to do so.
I tried the following:
map space :i
map <space> :i
But these doesnt seem to work.
You're mixing up the modes in your mappings; that's an important concept in Vim. Though there's a :startinsert Ex command in Vim (where your mapping would indeed start with a :), it's more straightforward to use the normal mode i command:
:nnoremap <Space> i
You only want a normal mode mapping here, so :nmap, not :map; cp. :help map-modes. And see :help key-notation for why it's written <Space>.
Finally: You should always use :noremap; it makes the mapping immune to remapping and recursion.
strange requirement, but, you have your reason. :)
try this line out:
nnoremap <space> i
Related
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.
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
Is there a default key for scrolling in insert mode? I know that I could just
:imap <F5> <ESC><C-e>a
:imap <F6> <ESC><C-y>a
but I'm wondering if there's any key binding there by default.
For completeness, there are two dedicated commands for scrolling in insert mode, CTRLXCTRLE and CTRLXCTRLY. They are probably the proper ones to map.
See the documentation here: :h i_CTRL-X_CTRL-E.
In normal mode CTRLE and CTRLY do the same thing, I use them quite often.
In insert mode, type Ctrl-o, then type zz.
It'll set current line in the middle of the screen.
Actually, you can type any command.
Background:
Sometimes when editing in vim it is possible to have extra characters in a file that the user did not expect to be there because he was in "insert mode" when in a hurry and rushing to get something finished.
Fortunately, even if the user is rushing, pressing ESC a couple of times is always sufficient to get them out of insert mode and into normal mode, with no surprises.
Question:
Is there a key binding that works the same way for insert mode? Pressing "i" can get you into insert mode, but if you press it multiple times, you will start inserting the letter "i" into the file.
Goal:
The goal is to have some key binding for getting back into insert mode that the user can even press multiple times with eyes closed, and still not worry about "surprises" of unexpected characters being put into the file.
<C-o>i should do the trick. <C-o> gets you temporarily to normal mode, but for only one command, if that command is "go to insert mode" than well, you simply return there.
Edit: I could reproduce your error message now, and it seems the easiest thing to do is this:
:nmap <C-i> i
:imap <C-i> <C-o>i
If do not map <C-i> in insert mode, but in normal mode only, then repeatedly hitting <C-i> will be idempotent.
Thanks to Benoit for mentioning that <C-i> inserts a tab in insert mode.
You should do a mapping that behaves differently in the distinct modes:
:inoremap <F1> <NOP>
:nnoremap <F1> i
:vnoremap <F1> <esc>i
:cnoremap <F1> <C-C>i
:onoremap <F1> <esc>i
Hitting F1 will go to insert mode then.
You can also toggle the 'insertmode' setting (:set insertmode): in this mode, the Insert mode is the default mode (to which you switch with Escape, and you go to normal mode with CTRL-L.
The answer given by bitmask works, but it apparently has the side-effect of producing the error message:
E37: No write since last change (add ! to override)
Unless you have configured your vimrc to turn that message off.
But another alternative that seems to work without producing error messages:
CTRL-C i
Which seems to work on standard vim.
Is there any key combination that simulate the Del in Vim insert mode? For the Backspace, there is the Ctrl-H which is very convenient, and make it easier than pushing the far away Backspace button.
Take a look at http://vimdoc.sourceforge.net/htmldoc/insert.html There are a few more built-in key combinations for various tasks.
Also you can set your own mappings using .vimrc for example your given example is just
imap ^H <Left><Del>
On my vim installation, Del in insert mode Just Works. :help i_<Del>
If Del isn't doing what you want, you can try :fixdel. :help :fixdel has a good explanation of what that tries to fix.
If you simply wanted to simulate Del via another Ctrl-key mapping (e.g. Ctrl-D), I'd recommend the following mapping:
imap <C-D> <C-O>x
Ctrl-O in insert mode will allow you to run a single normal mode command and automatically return back to insert mode. x deletes the key under the cursor.
You can map keys yourself in vim, including insert mode. The following article reveals more details:
Mapping keys in VIM