2

I got some columns that look like this:

9  normal    tri key     __tri_key
26 32     throw ex   __throw_ex
42 normal     row lend     __row_lend
98 normal   race car    __race_car
101 32      math chk      __math_chk     

How can I make them like this (via command of course).

9     normal    tri key     __tri_key
26    32        throw ex    __throw_ex
42    normal    row lend    __row_lend
98    normal    race car    __race_car
101   32        math chk    __math_chk  
  • 2
    In your example you have spaces as both field separator and "normal" space. So it could be hard to pack all functionality into a general purpose command. Anyway, check [this question](https://vi.stackexchange.com/questions/20658/align-a-block-of-code-on-the-basis-of-a-single-character). – Matt Aug 15 '19 at 04:38
  • Yes, definitely check out the answer to the question @Matt linked to! – filbranden Aug 15 '19 at 05:15
  • https://stackoverflow.com/search?q=%5Bvim%5D+columns – phd Aug 15 '19 at 11:21
  • @Matt can we assume that the field separator is a tab ;)? –  Aug 15 '19 at 19:05
  • actual keystrokes: V}!column -t – TitouanT Aug 16 '19 at 10:03

2 Answers2

0

I have a function called AlignText that works like if you were using the "column" command. If you pass a separator it will use it as separator, if not it will function like normal column command.

" https://stackoverflow.com/questions/57093175/
" https://vi.stackexchange.com/questions/2410/
if !exists('*AlignText')
    function! AlignText(...) range
        if a:0 < 1
            execute a:firstline . ',' . a:lastline . '!column -t'

        else
            execute a:firstline . ',' . a:lastline . '!column -t -s' . a:1 . ' -o' . a:1
        endif
    endfunction
endif
command! -range=% -nargs=? Align <line1>,<line2>call AlignText(<f-args>)

If you select a portion it will work on that if not the whole file will be changed

vip ........................... visual inner paragraph
:'<,'>Align ................... apply change on selected paragraph

You can copy the function and the "command!" to the clipboard and run on vim:

:@+ ....................... will put the function into vim memory
vip ....................... select your block of text
:'<,'>Align ............... voila, text aligned

The actual "column" command can be used this way:

vip ....................... select the paragraph
:'<,'>!column -t 
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
0

For alignment, there are three well-known plugins:

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324