1
You can achieve those or better (pinky) command mappings very quickly. I'm not recommending this solution, but as an example, if you want to move the cursor to the beginning of line with <C-a>
you can execute the following line after hitting :
from normal mode (or add the line to your vimrc)
inoremap <C-a> <C-o>0
or, if start of line means first non-blank character of line
inoremap <C-a> <C-o>^
Curious thing, I just tested these mappings in the buffer in which I'm writing this answer, and I was confused for a moment because they didn't work. Then I realized I had forgotten to enter insert mode. You seem to find it a pain to leave insert mode to execute normal mode commands. You may find after using Vim for a while that it's the reverse–that you linger in normal mode while you're thinking and only make excursions to insert mode for particular occasions. Vim is probably only (or mostly) worth learning if you find that you do more editing than writing text.
So one part of an answer to your question about executing normal mode commands quickly from insert mode is <C-o>
, which lets you leave insert mode temporarily to execute one normal mode command, then reenter. Read about it at :help i_Ctrl-o
. This is useful for one-off conveniences. A more sustainable route is probably to try to embrace normal mode as a default for navigation and editing, and only enter insert mode when you actually want to insert new characters by hand into the buffer.
2
Your examples are very general so it's hard to give specific answers. I'm going to make up a specific example in hopes it fits or is translatable to something you encounter. For your number 1, it depends on how you know where the closing parenthesis is supposed to go. let's say I have a line like
let cchar = s:cchar["atx"]
This is meant to be a line of VimL, vim's scripting language, and it (or something like it) occurs in a Vim syntax file I am currently disagreeing with. Let's say I want to change the right hand part of this expression to a function call, perhaps because the function provides defaults when the dictionary s:cchar
has not been assigned a value yet for the "atx"
key. With my cursor (in normal mode) on the first character, 'l', I might type
f=wcf[MyFunction(<Esc>$r)
Briefly, this says
f=
- forward to '=' (I could have said fs
, but I probably wouldn't pause long enough to see that the 's' I want to reach is the first s on the line, so I'd go for the character that is obviously unique, and continue from there)
w
- next word, now my cursor is on the 's' of 's:cchar'
cf[
- change to forward to '['
at this point the line looks like
let cchar = |"atx"]
where |
is the cursor, and I am in insert mode. I then type MyFunction(
and leave insert mode (<Esc>
).
$
- move cursor to end of line. I could have used E
for end-of-big-word or f]
for forward-to-']' or some other motion, but $
occurred to me first.
r)
- replace character under cursor with a ')'
Now I have changed the assignment expression from assigning from a script-local dictionary to using the return value of a function, and in doing so I introduced parentheses. I don't know how your particular need for parentheses looks, perhaps it's very different, in which case there will be a different string of commands to achieve it. If I paused to think longer about my made up example I might find more efficient ways to achieve that also, but who has time to pause and think.
(Try it, copy-paste (sorry, yank-put) the text and the command to a buffer, and with your cursor on the first character, 'f', of the command hit y$
. Now the command is in your unnamed register. Move to the beginning of the first line and hit :normal <C-r>"
to a) enter command line mode, b) begin executing a normal mode command from the command line, and c) to paste (atchoo put) the unnamed register into the command line as the normal mode command to be executed. The only thing you need to change in the command is to replace the phony <Esc>
series of characters (because you copy-pasted atchoo from your browser) with the actual Escape key code, so delete it and hit <C-v>
and then your Esc key. It will look sort of like ^[
. Now hit return to run the command. Or... just type the string of keys to test the commands, I just wanted to show you that you can interact with normal mode from cmdline mode as well, and <C-v>
, and <C-r>
, and all the other glories of Wonderland. Also, see :help Cmdline-mode
.)
3
For deleting a word backwards you can use the standard mapping <C-w>
(see :help i_Ctrl-w
). I don't know if there is a default mapping to delete a word forwards, but I suppose you could make one with
inoremap <C-S-w> <C-o>dw
Again, <C-o>
will take you out of insert mode for a one-off command and dw
will "delete" over the "word" motion. See :help d
, :help w
. If the forward words you delete are not actual words but class.function#names, you may want to use W
(see :help W
). If you're already mostly in normal mode you don't need these mappings, just do dw
, db
or friends.
Some help chapters to look closer at
:help <C-o>
:help Insert
:help f
and the following sections (F
,t
,T
,;
and ,
)
:help word-motions
vim
when editing you spend most of your time in normal mode not in insert mode.