tags:

views:

100

answers:

4

I often need to enter insert mode just to make changes on a specific line, and would like know if there is a way to enter insert mode in a way that when I am done editing on that line, hitting [Return] gets me back to normal mode.

I am aware of the 'r' command for replacing a single character, I guess what I want is something like that but for a line.

A: 

does it work?

:imap <Enter>   <Esc><Enter>
ali
Then he'll never be able to enter a new line again...
meagar
A: 

At first glance it sounds like you want R but the rest of the question makes it seem like you're also wanting to have an easier time replacing characters under the cursor, avoid Esc or maybe insert mode in general. So here are some random suggestions that may help:

Delete the current character and land in insert mode: s

Delete the word under the cursor and say in command mode: diw

Delete the whole line and stay in command mode: dd

If you hate having to reach up to the Esc key, you can make it so something else - like hitting j twice in a row from insert mode - does an Esc: imap jj <esc>

Or you can have vim automatically switch back to normal mode after a few seconds of inactivity: *au CursorHoldI * stopinsert*

Replace all occurrences of a pattern on the current line: V to select the line visually then type :s/foo/bar/g

When you're in insert-mode, and want to quickly escape to command-mode to just run one command and be automatically put back in insert mode again, type Ctrl+o while in insert mode, then run the :command.

rkulla
A: 

If like me you find reaching the ESC key a bit of a stretch, then use Ctrl-[ - Vim has that defined as an alias to it by default.

Dave Kirby
Actually it's not vim doing that, it's ASCII :) `[` is 0x5b, escape is 0x1b, and "control" works by clever bitmasking. (To see the pattern, note that `A` is 0x41, and control-A is 0x01). For bonus points, figure out `^@` and `^?`. :)
hobbs
+2  A: 

The following mapping makes the key combination gi enter insert mode just like i, but hitting enter/return in this insert mode will return you to normal mode. Just add the following lines to your .vimrc:

autocmd! InsertLeave * silent! iunmap <CR>
nnoremap gi :inoremap <C-V><CR> <C-V><ESC><CR>i

Edit: fixed < and > in mapping

too much php
Thanks, this is exactly what I wanted!
Tony