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.
Related
I would like to be able to setup MacVim so that it switches back to Normal mode after saving a buffer. For example: lets say I am adding some text in Insert mode and I hit "Command + S" to save, I would like to be in Normal mode after the save operation has completed.
Is this possible?
Note: Incase the above is unclear, I do not want to spend more time in Insert mode, but less. I would like to exit Insert mode automatically upon save.
Adding the following lines to your .gvimrc will disable MacVim's [Cmd + S] shortcut, and switch the mode back to Normal before saving.
It will also block Vim from entering Insert mode when hitting [Cmd] + [S] keys (as this would have activated the substitute command). Note: you will still be able to substitute hitting the [S] key as usual.
" Disable MacVim save shortcut
macmenu File.Save key=<nop>
" Exit to Normal mode upon [Cmd+S]
inoremap <D-s> <Esc>:w<CR><Right>
vnoremap <D-s> <Esc>:w<CR>
" Save in Normal mode (block Substitute)
nnoremap <D-s> :w<CR>
Thanks to #Amadan for pointing me in the right direction.
Wanting to typically be in insert mode is using Vim wrong. If it works for you, that's fine; but you will never reach the potential of Vim while sticking to that habit; might as well use Notepad++/SublimeText3/...
If you really really want it, you can have something like :inoremap <C-s> <C-o>:w<CR> to save and stay in insert mode, or :inoremap <C-s> <Esc>:w<CR> to save and move to normal mode. (It will work only on graphical Vim, as terminal Vim typically won't ever receive Ctrl-S.)
I want to be able to save a file in vim while in the insert mode. I thought of using the following shortcut:
inoremap <leader>w <Esc>:w<cr>
While the shortcut saves the file in the insert mode, it leaves the cursor one spot ahead of where the cursor would be if I physically typed out the keys
Esc :w followed by Enter. This is a problem because when I use the shortcut whenever I am at the end of a line, it takes me to the next line, and I have to then come back to the spot where I initiated the save.
Any help would be appreciated on how I can map <leader>w to the exact actions that occur in Vim when I physically type out the Esc :w followed by Enter key sequence.
I should add that if I instead use the following key-mapping, things work exactly as I want:
inoremap <C-s> <esc>:w<CR>
However, I would like to avoid pressing CTRL and s at the same time. It is possible there is some problem with the <leader>, but I cannot figure out what it is (I am using , as my leader key).
Though one could discuss the suitability of your insert-mode mapping, the root cause of your problem is a trailing space in the mapping definition; i.e. Vim reads this as follows:
inoremap <leader>w <Esc>:w<cr><Space>
You'll even see this in the :imap <Leader>w output! <Space> in normal mode advances the cursor one to the right (like l); that explains the unexpected move.
Try this instead:
inoremap <silent> <leader>w <C-o>:w<CR>
The idea is Ctrl-o can be used to run commands directly from insert mode. See :help i_CTRL-O for details.
Why not simply doing
inoremap <leader>w <Esc>h:w<cr>
(not the additional h for going back one character)?
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’ve installed the Snippets Vim plugin. Suppose I’m editing a C file. When I type if<tab> in insert mode, the text expands to
if (/* condition */)
{
}
and now, I’m in select mode (indicated in the status line by --select--).
I have a mapping in my ~/.vimrc: snoremap <c-f> <bs>, which means I want to use Ctrl-f to delete the text when I’m in select mode.
However, when I type <c-f> to delete the text, the text is deleted as expected, but I am dropped into normal mode instead of insert mode as I want.
So, is anyone able to figure this out?
And more—add this mappping:
vnoremap <c-f> <bs>
<c-f> doesn't work at all. Please help with this also.
The help for select mode (:help Select-mode) says:
Printable characters, <NL> and <CR> cause the selection to be deleted, and
Vim enters Insert mode. The typed character is inserted.
I don’t see any other case where a command in select mode ends by entering insert mode (although of course I could be missing something).
So this could be a peculiarity of the plugin, but it also could just be how select mode handles commands. In the meantime, you can add i to the end of your <c-f> mapping:
snoremap <c-f> <bs>i
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