I'm returning to Vim after using Emacs for a few years. I'm trying to grok Vim by reading and watching tutorials, as well as actually using it daily.When editing text, I find the Emacs commands of Alt-F, Alt-B, Ctrl-a, Ctrl-e, Alt-D and Alt-Backspace to be very useful and intuitive, allowing for quick and easy local edits while working with text (words/code).What is the Vim-way of implementing similar functionality? Returning to NORMAL mode to issue a single motion, only to re-enter INSERT mode is not optimal.Example 1: When in INSERT mode to insert a ( at the start of an expression, how should I move to the end of the expression or the line to insert the corresponding ) in Vim?Example 2: When editing text, one may often want to delete a word forwards or backwards while writing or editing a sentence or code. It would be unusual to have a perfectly-formed sentence/code in one's mind, ready to type once-off ahead of time; often a sentence changes as one is working with the text. The restrictions of INSERT mode are making this kind of editing difficult for me in Vim. I feel unable to effectively manipulate the text in insert mode, always finding myself having to switch back to NORMAL mode for simple movements or deleting words.PS: Alt-F and -B move the cursor forward or backward one word. Ctrl-a and -e move the cursor to the start or end of line. Alt-F deletes the word under the cursor. Alt-Backspace deletes the word before the cursor.
Grok Vim motions and simple edits
insert mode;cursor motions;normal mode
1You 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>0or, if start of line means first non-blank character of lineinoremap <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 reversethat 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.2Your 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 likelet 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 typef=wcf[MyFunction(<Esc>$r)Briefly, this saysf= - 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 likelet 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.)3For 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 withinoremap <C-S-w> <C-o>dwAgain, <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