Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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.

like image 489
Ciano Avatar asked Mar 10 '26 10:03

Ciano


2 Answers

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.

like image 59
Ingo Karkat Avatar answered Mar 12 '26 23:03

Ingo Karkat


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.

like image 22
romainl Avatar answered Mar 13 '26 00:03

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!