10

In vim, if I enter and leave edit mode without doing anything, the caret ends up one character to the left. And if I enter and leave append mode, the caret moves forwards and then backwards.

Any way to configure vim to leave the caret alone in these cases?

Ideally I just want to always enter append mode, but without moving the caret when I enter or exit the mode.

(Currently I usually use insert mode because it doesn't mess up my caret position upon entry. That is, except when I need to append to the end of the line, in which case I swear at vim for behaving in such an archaic fashion, press Esc and enter append mode.)

6
  • 1
    The answers to this question are good. I just want to point out that if they're correct in their assumptions that you use i to go to edit mode, then that's what's causing the moving backwards. If you use 'a' (not A) it will work as you expect.
    – johnny
    Jan 7, 2011 at 9:12
  • @johnny I don't think it will. I'm usually in "normal" mode, moving around the file and stuff. When I need to make an edit, I position the caret first, then press "a", and then find that I had positioned it wrongly, because it moves to the right. Given that I spend 99% of my editing time in modern editors, there is no chance I will ever remember to position one char to the left before pressing "a". Which is why I was hoping for a macro or something.
    – RomanSt
    Jan 7, 2011 at 11:47
  • It's a question of habits. It's indeed annoying, but the first years only. You'll eventually learn to press i or a without think about it, and may be even s, S, ci", etc. instead of the limited i and a. Jan 7, 2011 at 16:02
  • 1
    I understand your frustration but. Try instead to think about what you're doing. Ask yourself if there's a better way of doing it (than using Vim as a regular texteditor, that is). I've found with Vim there always is a better way, the Vim way of doing things. We can help but we have to know what you're doing when running into problems. Do you for example go into edit mode when you want to delete something? Don't, there's 'x' for deleting characters. Want to replace a character, then there's 'r'. Just think about what you want to achieve.
    – johnny
    Jan 7, 2011 at 17:34
  • @johnny I understand what you're talking about, but it requires too much relearning to make sense for me. I spend 99% of my editing time in Visual Studio and I love it. I just know I won't relearn this, so no point wasting time.
    – RomanSt
    Jan 8, 2011 at 2:40

4 Answers 4

10

This question gets asked frequently among new vi/Vim users, and the answer is that while in normal mode, the cursor is always "on" a character, but in insert mode the cursor is always "between" two characters (remember, the end-of-line is a character). You can't really see this illustrated as well in console Vim, but in the GUI you'll notice the cursor becomes a bar between two characters when you enter insert mode, instead of a block over a character when you are in normal mode.

So what you're seeing is not necessarily the cursor moving one character back when you leave insert mode, but merely moving onto a character. The only safe direction of movement is to the left (or back). Thus, you have more than one way of entering insert mode:

  • "a" enters insert mode with the cursor "between" the character the cursor was on and the next character to the right.
  • "i" enters insert mode with the cursor "between" the character the cursor was on and the previous character to the left.

Some people have made efforts to suppress this "movement" that they don't like, but it inevitably interferes with plugins and other Vim scripts they want to run in the future.

My suggestion is to get used to using the "a" and "i" (and "A" and "I") commands in the appropriate circumstances.

The "o" and "O" commands are also useful to learn. See:

:help a
:help i
:help A
:help I
:help o
:help O

Edit: If you're still determined to change this behavior, try this tip: Prevent escape from moving the cursor one character to the left

7
  • 1
    I understand what you're saying, but "i"/"Esc"/"i"/"Esc" causes actual overall net movement of the caret. I really think VIM is simply not for me.
    – RomanSt
    Jan 7, 2011 at 1:40
  • 2
    @romkyns: I think what you should do is use a instead of i. Jan 7, 2011 at 4:27
  • 1
    @Dennis agreed, "a" is a bit better because it allows the caret to be before the first char and after the last char in a line - which "i" arbitrarily disallows. But I tried that; what happens is that I always misplace the caret prior to pressing "a" - so I have to immediately move the caret one position to the left. Every single time. It got tiring so I switched to "i".
    – RomanSt
    Jan 7, 2011 at 11:50
  • @Heptite Could you perhaps point me at one such flawed implementation of something that suppresses the movement? The chances of me wanting to use plugins or other scripts in the future are essentially zero.
    – RomanSt
    Jan 7, 2011 at 11:51
  • 1
    @romkyns, it (doesn't) works both ways. Remapping <esc> will indeed prevent most plugin using <esc>to work correctly, but also, it will continue to annoy you when you'll be using other standard features that return to normal mode the same way <esc>does. Learning to use the right key on the fly is the solution that scales/works in the long run. Jan 7, 2011 at 16:05
1

I think by carat you mean your cursor and if you are using "i" this will insert directly where your cursor is, "a" (which I believe you are looking for) moves the cursor one character to the right. Also, shift-i(I) will go to the begining of the line and shift-a(A) will go all the way to the end of the line.

If I have misunderstood your question I apologize.

1

Since remapping <ESC> is considered 'risky' I just combine with one of the Avoid the escape key approaches and have the following in my .vimrc:

 inoremap ii <ESC>l

Note: Make sure there are no space characters on the end of the line.

Simple, but fully certified as "works fine for me" as that is exactly what I'd otherwise have to type by hand.

Whether I use a or i I still get the caret moving back behaviour and it bugs the hell out of me as more often than not then next thing I want to type is d$ to delete to the end of a line, etc.

1
  • Amazingly simple solution. Best I've come across. Sep 24, 2014 at 3:53
1

Here's my solution:

au InsertLeave * call cursor([getpos('.')[1], getpos('.')[2]+1])

It's funny. This same question exists on all three sites: SO, SU, Unix+Linux.

1
  • I don't find it funny :) Fortunately I've found an editor that doesn't drive me nuts (diakonos), so this is no longer an issue. Still, upvoted for those who like vim.
    – RomanSt
    Jun 12, 2013 at 12:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .