Align columns in VI

The linux column command works well for creating the columns

:%!column -s . -t

Then use block copy.


One option is to use this Align plugin to line up the periods so you can more easily select a column in Visual Block mode. e.g. if you do this:

:%Align \.

You'll end up with:

current_well  . well_number
current_well  . well_name
current_well  . well_type_code
well_location . section
well_location . range

If you don't want to use a plugin, try padding your columns with spaces. e.g. for your text:

:%s/\v(.*)\.(.*)/\=printf("%-16s %s", submatch(1), submatch(2))/

That'll leave you with:

current_well     well_number
current_well     well_name
current_well     well_type_code
well_location    section
well_location    range

Then you can Ctrl-V and select a column of text to copy. Just make sure you pick a column width wider than your widest value.


I've run into this problem a number of times. Changing the dots to tabs will line them up mostly nice, but not really. It's true that all the columns will start on a tabstop, but there's no guarantee that they'll be on the same tabstop. For instance, if this was your original text:

lt1tab.value1
gt1tabstop.value2

and you do:

%s/\./\t/g

and assuming a tabstop is 8 spaces, you'll get:

lt1tab  value1
gt1tabstop    value2

What you might want to do instead is remove everything but the last column (or whichever column you want). For instance:

%s/^.*\.//

will leave you with:

value1
value2

Which you can easily copy and paste.


If you don't have to use Vim, you can use unix's cut to do what you want:

cut -f2 -d. input_file > output_file

Tags:

Vi

Vim