coder123
coder123

Reputation: 9737

How do you yank a part of a line in vi?

I've spent quite a bit of time searching for an answer, but I can only find answers for vim, which suggest using visual mode or registers. Neither of these seem like options for vi (version SVR4.0, Solaris 2.5.0). I know about D, yy, dd, which yank or delete the entire line or up to the end up the line.

I'm now wondering if it's even possible to yank/delete a part of a line: say, the part within the brackets in: I want to co[py this par]t of the line.

Upvotes: 0

Views: 511

Answers (3)

lazarus
lazarus

Reputation: 1

Use the marker feature.

For the line: “I want to co[py this par]t of the line.“, First move the cursor past the ‘r’ character using any means you want. Then mark it with ‘m1’. Next, move the cursor after the ‘o’ character. To select the text, use command “y`1”. This yanks the text between the current cursor at ‘o’ to the cursor position at marker 1. You can then move to another cursor position and copy the text with the put ‘p’ command.

Upvotes: 0

dave
dave

Reputation: 64707

You can do

y f character

Which will [y]ank until it [f]inds whatever character.

In your example. you could

f p y f r

Find p - yank until you find r

For your example

I want to co[py this part of t]he line

I usually will do

f p - find p

y - enter "yank mode"

/he line - up until you find the phrase "he line"

Enter

Where "he line" is just whatever text comes after the character, and I type until I know it's enough to select it and not something else.

Upvotes: 0

beaker
beaker

Reputation: 16810

All of the movement commands work with yank, like h, j, k, l for left, down, up, right. So if the cursor is on the first p in

I want to co[py this par]t of the line.

y11l (yank eleven ell) gives

py this par

y3w gives you the next 3 words, meaning if the closing ] is not there it'll include the final t.

If your cursor is on the [ (assuming that's actually part of the string), y% (move to matching bracket) gives:

[py this par]

Upvotes: 2

Related Questions