0

I don't understand why is Vim pasting after the cursor; for example in this example (where the cursor is at the start):

|The quick brown fox jumps over a lazy dog
^

I would like to exchange fox and dog:

  • ff – Find fox
  • yw – Yank fox
  • fd – Find dog
  • p – Paste

Which gives me:

The quick brown fox jumps over a lazy dfo|xog
                                         ^

In the same vein, if I do:

i<Esc>i<Esc>i<Esc>

The cursor goes backward.

How can I improve on this? Because I find the this behaviour very awkward.

  • I think the difference was not important 40 years ago. Now it is simply not important. I started to use vi in the middle 90s. Afaik there are shortcuts or settings to paste before the cursor, but I was lazy until now to check them. ;-) – peterh - Reinstate Monica Jul 23 '18 at 15:35
  • Btw, I think your question is not so bad, it simply requires the true answer and so is it. IMHO vi should follow the common standards, so if pasting customs changed with the w$ world, vi should follow them. Btw, vi is older than the Internet... – peterh - Reinstate Monica Jul 23 '18 at 15:37
  • It is not really true that vi is older than the internet. vi was written in 1976 but by 1975 Berkeley already had the NCP protocol, precursor to TCP/IP. ARPANET itself was around even earlier. – Mass Jul 23 '18 at 16:06
7

See @statox answer for p, P, i and a.

Background: This has to do with the way the cursor works in a terminal (and hence originally the typewriter). The terminal is a grid of character cells and the cursor is on one of these cells. It is never between two cells. This is handled differently in modern GUIs where the cursor in a text field normally is between two characters (and there are no character cells).

So when you live in a world of character cells on a grid and the cursor is on one of these cells, where does the next character you type go? The answer that people came up with was:

  1. either you overwrite the cell you are on, this is what a typewriter does (and this is replace mode)
  2. or you push the contents of the current cell and all the cells right of it one cell to the right and insert the character at the current cell, this is a logical extension of what a typewriter feels like when the cursor is just after the last typed character on a line with only blanks to the right (this is insert mode)

Afterwards the cursor might be moved one cell to the right.

Link: Wiki page.

  • You could also mention I and A which insert at the start of the line and at the end. – Alexis Wilke Jul 23 '18 at 21:29
  • So you mean, nowadays, nobody should use i, but a because the way the cursor works on modern terminals right? – nowox Jul 23 '18 at 22:30
  • 1
    @nowox No you can still use all the different commands as you want. I was just giving background to understand the "why" in your question. – Lucas Jul 23 '18 at 23:33
  • @AlexisWilke I could but I don't think that they are relevant to the question "why does vim paste after the cursor?" or the extended question "Why does the cursor and text insertion behave unexpected?" – Lucas Jul 23 '18 at 23:35
6

If you read :h p you'll see the next section :h P:

                            *p* *put* *E353*
["x]p           Put the text [from register x] after the cursor
            [count] times.  {Vi: no count}

                            *P*
["x]P           Put the text [from register x] before the cursor
            [count] times.  {Vi: no count}

For the second part of your question maybe you're looking for :h a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.