I wonder if there's a way to make the Alt (or another key) work like Ctrl-o, but for as long as the key is pressed. For example, if you're in insert mode and want to move 10 lines down and 2 words forward, you could hold Alt, press 10jww and then release the Alt key. It is much faster than pressing Ctrl-o before each movement or leaving insert mode and having to enter it back again.
I did some remappings to use the main movement keys from insert mode while holding Alt:
inoremap <A-h> <C-o>h
inoremap <A-j> <C-o>j
inoremap <A-k> <C-o>k
inoremap <A-l> <C-o>l
inoremap <A-w> <C-o>w
inoremap <A-e> <C-o>e
inoremap <A-b> <C-o>b
But that is limited, if I wanted to use numbers to move multiple steps for example, I would need to map a new key binding for each movement and for each number.
If it was possible to do something like enter normal mode on Alt (keydown) and go back to insert on Alt (keyup), then all normal mode key bindings should be available only by holding one key.
Related
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.
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
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
For a quick overview, I map <s-space> to <esc> so I can more easily cancel out of things without moving my hand to the escape key. For example, when I want out of insert mode, or to cancel something.
inoremap <esc> <nop> " to force me to stop using <esc>
cnoremap <esc> <nop>
nnoremap <s-space> <nop>
onoremap <s-space> <esc>
inoremap <s-space> <esc>
However, if I press 'r' vim is waiting for a character, and when I press <s-space>, I end up replacing with a space, instead of canceling the replace operation. Is it possible for mappings to work after pressing 'r' once, while waiting for a character?
Thanks!
If you find you've accidentally pressed r, how about just tapping uu?
This will make the replacement but immediately undo it again, without you having to move your hands from the letter keys.
I want to move one word forward in insert mode.
Why this doesn't work:
inoremap ,w <esc>w
But this works(back one word):
inoremap ,b <esc>b
That's because when the cursor is at the beginning of a word, the <Esc> will move the cursor one character left (this is a bit unintuitive, but default vi behavior), and the w will only move to the original position.
This should work:
inoremap ,w <esc>ww
mapping critique
I don't particularly like your mappings:
starting it with , adds a delay whenever you type a comma
do you really need a command that leaves insert mode and moves the cursor? <Esc>b achieves the same and also is two keystrokes (many users remap the <Esc> key to be in a less cumbersome position)
if you really must navigate in insert mode, there's already <C-Left> / <C-Right>.