Ciano
Ciano

Reputation: 554

In Vim block-mode how do I most efficiently select a column?

Let's say I have a text file that has something like:

1   aa    1234
2   bbbb  5678
3   cc    9abc

If I put my cursor on the first 'a' in the string 'aa' and hit ctrl-v to enter block mode, is there a set of key-strokes that will move my cursor down to the last line in the same column and move my cursor to the right so that the longest word in the column is fully selected ('bbbb' in this case)?

What I currently do is enter column mode on the first 'a' in the string 'aa' and then down arrow 2, and then right arrow 4. This seems really inefficient and I'm sure there's a better way to do it.

Also, the last line in my example might not be the last in my file. I could have something like:

1   aa    1234
2   bbbb  5678
3   cc    9abc

4   dd    1234
5   eeeee 5678
6   ff    9abc

For this question, I'm only interested in selecting the entire column in rows 1-3.

Upvotes: 3

Views: 649

Answers (4)

Kent
Kent

Reputation: 195029

I don't know what you want to do after you selected your target column. But it could be done in this way:

faWh<c-v>jB

if your cursor is already on the first a, just

Wh<c-v>jB

the idea is, move cursor(*) to 1 aa *1234 then enter block-wise selection mode, select the target column backwards.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172510

Have a look at textobj-word-column.vim. It provides text objects for columns. For your example, you'd just position the cursor anywhere in the middle column and do vic.

Upvotes: 2

romainl
romainl

Reputation: 196476

Selection is often not necessary in Vim. Even defining an area to work on, actually.

Vim, unlike spreadsheet programs, only knows about character cell-based columns; not "logical" columns. If your ragged column is not at the end of the line you simply won't be able to visually select it.

But your goal is not to select that column, is it?

Do you want to remove it?

:,+2norm dw

Do you want to append a semicolon?

:,+2norm ea;

Do you want to surround it with double quotes? Surround.vim to the rescue:

:,+2norm ysiw"

Do you want to cut it and paste it somewhere else?

:,+2 norm "Wdw

And so on.

Upvotes: 2

zmo
zmo

Reputation: 24812

I don't see how to select based on the longest on the first column, but here's how I'd take out the full column, spaces included:

ggfa    ; get to line 1, first column of the table

C-v}k   ; select all the wanted lines as a block selection

$Bh     ; unselect last block

x       ; take out selection

and in case you worry about that, you can always realign your columns using the Tabular.vim plugin.

Upvotes: 0

Related Questions