tags:

views:

605

answers:

6

While in Insert Mode in Vim, is there any way to traverse the text (like moving some characters forward and backward) other than using the arrow keys ?

This is because obviously if you press h, j, k and l while in Insert mode, the actual characters are printed on screen instead of moving through the text.

The way I'm doing it atm is having to resort to ctrl + [ (Esc) and traversing the text then; but obviously that is not productive.

+1  A: 

I believe Home and End (and PageUp/PageDn) also work normally while in insert mode, but aside from that, I don't believe there are any other standard keys defined for text traversal.

Amber
but possibly isn't there any way of traversing the text using only the letter keys ?
Andreas Grech
Certainly there is, if you don't want to be able to type certain letters... ;)
hobbs
+1  A: 

Sorry but vim don't work that way.

You should switch to "normal" mode, navigate and then go back to insert again.

Johan
A: 

vim lets you map any key to pretty much anything you want. Among the many capabilities is also the ability to switch in and out of command mode, or to move the cursor in insert mode... so if you're not restricted to a default-configured vim, anything can be done.

No, I'm not good enough to give an example. If I had to, I'd pick up the manual and figure it out.

Carl Smotricz
A: 

You could use imap to map any key in insert mode to one of the cursor keys. Like so:

imap h <Left>

Now h works like in normal mode, moving the cursor. (Mapping h in this way is obviously a bad choice)

Having said that I do not think the standard way of moving around in text using VIM is "not productive". There are lots of very powerful ways of traversing the text in normal mode (like using w and b, or / and ?, or f and F, etc.)

Peter van der Heijden
I didn't say the the standard way of moving through text in vim is not productive; what i said was that it was a bit "annoying" having to go out of insert mode to go back a few characters and then having to back to insert again; but then again, maybe this is because I haven't got quite used to vim yet :)
Andreas Grech
+17  A: 

You seem to misuse vim, but that's likely that you're not very familiar with it.

The right way is to press Esc, go where you want to do a small correction, fix it, go back and keep editing. It is effective because Vim has much more movements than usual character forward/backward/up/down. After you learn more of them, this will happen to be more productive.

Here's a couple of use-cases:

  • you accidentally typed "accifentally". No problem, the sequence EscFfrdA will correct the mistake and bring you back where you've been editing it. Ff movement will bring you back to the first encountered "f" character. Compare with Ctrl+<-->->->->deldEnd, that does virtually the same in a casual editor, but takes more keystrokes, makes you move your hand out of alphanumeric space of the keyboard.
  • you accidentally typed "you accidentally typed", but want to correct it to "you intentionally typed". Then Esc2bciw will erase the word you want to fix and bring you to insert mode, so you can immediately retype it. To get back to editing, just press A instead of End, to reach which you should move your hand
  • you accidentally typed "mouse" instead of "mice". No problem - the good old Ctrl+W will delete the previous word without going out from insert mode. And it happens to be much faster to erase small word than to fix errors in it. I'm so used to it that I had closed the browser page when I was typing this message...
  • repetition count is largely underused. Before making a movement, you can type a number; and the movement will be repeated this number of times. For example, 15h will bring you 15 characters back and 4j will scroll you 4 lines down. Start using them and you'll get used soon and find out that pressing 10 times <- key is less fast than iterative approach to moving cursor, when you type 12h, notice that you made a mistake and immediately correct yourself with ll.

But, if you still want to do small text traversals without leaving insert mode, follow rson's advice and use Ctrl+O. As an example, Ctrl+OF+f will move you to previous f character and leave you in insert mode.

Pavel Shved
@Pavel, good answer! +1
Rob Wells
+1 Excellent answer and thanks for all the examples you gave!
Andreas Grech
It's also worth noting that you can use `Ctrl+o` to issue a single command in normal mode. This can at times get you where you want to be, especially when combined with the t/T and f/F movement commands.
Randy Morris
Thanks for that great tip rson!
Andreas Grech
This is the best answer to this topic, VIM is supposed to be used in normal mode most of the time, insert mode is just for - well - inserting text. If you don't fully use VIM's exceptional motion commands, you may as well use a non-modal editor.
kemp
+1  A: 

In GVim, you can use the mouse. But honestly, what's wrong with using the arrow keys? There's a reason why they are on a keyboard.

ammoQ