Search code examples
vimvi

Align columns in VI


I have a bunch of lines that I'd like to split into two columns, and get the data from each column. The data looks something like this:

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

Essentially what I'd like to do is split the line based off of the period, turn the data into two columns, and then grab the data for each column. I know this can be done in Excel, but I'm really interested in VIs solution for this problem. I know that

%s/\./

will format the string with empty spaces. But once I have the data looking like:

current_well    well_number
current_well    well_name
current_well    well_type_code
well_location   section
well_location   range

How do I grab all the values for each column so I can paste it into another application?


Solution

  • 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