armando
armando

Reputation: 1480

vim select text

I have a file which looks like this:

1 148 1  4
2 333 1  3
3 534 2  3
4 772 g  7
5 921 p  2

I want to yank text from line 1 to 5 and from column 1 to 7:

1 148 1  
2 333 1  
3 534 2  
4 772 g  
5 921 p   

can I do that from the vim command-line? If I type

:1,5ya a

the entire line is yanked into register "a" and I want just certain columns.

Thanks.

Upvotes: 5

Views: 335

Answers (5)

sehe
sehe

Reputation: 393809

I'd do

C-vG2wy

and be done (start from the top of the document with gg)

Of course, if you wanted it in register a, add "a:

C-vG2w"ay

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172758

You can execute any command on the command line, here with the help of :normal:

:execute "normal! 1G^\<C-v>6l5j\"ay"

This builds a blockwise selection, then yanks it to register a. The :execute is used so that the \<C-v> notation can be used instead of literally inserting it. It also allows you to replace the hard-coded limits with variables.

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172758

Another approach, more suitable for a script:

:let @a = join(map(getline(1, 5), 'matchstr(v:val, ".*\\%<9v")'), "\n")

It retrieves the lines as a List (getline()), then matches the first 7 virtual columns through the special /\%<v regular expression atom, and assigns that (join()ed as a string) to the register @a.

Upvotes: 0

Michael Kristofik
Michael Kristofik

Reputation: 35208

You can't do that in a simple way from the vim command line. :y is a linewise command - it only affects whole lines. What you're looking for is considered blockwise. The blockwise commands involve Visual mode. So the best you can do is:

  1. CTRL-V to start visual mode.
  2. Highlight the text you want.
  3. "ay to yank the highlighted text into register a.

Upvotes: 1

evil otto
evil otto

Reputation: 10582

Not really general, but this should work:

:1,5y|put|-4,.s/\(.\{7\}\).*/\1/|-4,.d a

Yank 5 lines, put (copy) them, delete everything after the desired columns, delete them into the buffer.

Upvotes: 0

Related Questions