There are a number of ways to do this, but I would say the most useful & generic way is to use visual mode (:help visual-mode
).
In your first example:
This is one piece of text.
Here is another sentence.
I would put the cursor on the first T
(in This
), start visual mode with v, and select the text you want to replace by using l (that's a lower case L) or the arrow key, and select the text up to and including the e
in one
).
You can then delete the text with d.
Note that d doesn't actually delete the text, it copies the text to the unnamed register (""
), and then removes it from the buffer. So it's more like "cut" (Ctrl+x) as used in many applications, and not delete (also see :help ""
).
Armed with this information, we can now go to the second line, start visual mode with v again, and select the text we want to replace (in this case, Here is another
), and press p.
The p is for "put", which is Vi-speak for "paste". Because we've selected some text with visual mode, it will replace this text. But again, this text is not lost: it's put in the unnamed register (replacing the text that was there before).
We can now go to the start of the first line, and press P, this will put the text from the second line at the cursor position.
p and P both "put" (paste) text, the difference is that the former puts the text after the cursor, while the latter puts it before this cursor. I often use the wrong one by accident: remember that you can use u to undo and then use the correct one :-)
I hope it will be obvious how to use this technique in your second example :-)
Bonus tip: The motion picture
Above I told you to select the text with l or the arrow keys, but there are better ways!
For example, we could also have used v3wd. The v starts visual mode, and 3w selects the next 3 words, and the d deletes it. This is obviously a lot quicker.
In fact, you don't even need visual mode, just using 3wd will work the same. I personally prefer to start visual mode, because you can easily see what you're about to delete, and adjust if required.
Bonus tip 2: The wrath of motions
In your second example:
<paragraph>And something else here</paragraph>
<paragraph><note>Some text here</note></paragraph>
I would put the cursor on the A
of And
, and type t<. This put the cursor just before the first occurrence <
; very useful!
There's also f, which puts the cursor on the first occurrence of what you find, and you could also use 2t< to put the cursor before the second occurrence of <
.
See :help cursor-motions
for more information about cursor motions. Note that you could also use the mouse to do all of this if you've enable it, but that's of course not the true Vi way ;-)
Bonus tip 3: The search for registers
The d and p use the unnamed register by default, but you can specify a register; for example, "ad would delete ("cut') the text to the a
register (leaving the unnamed "
register alone), and "ap would put ("paste") from this register. This can be quite useful in a number of scenarios.
See :help registers
.