How to enable min. from XX Space between two lines in Vim through replacement?

I have a text file with 2 lines in each line with a different length. For better reading, I want the second line lined up with the following lines.

eg. Before:

bla foo
barbla barfoo
foblaaaa Bablofoo

After:

bla      foo
barbla   barfoo
foblaaaa Bablofoo

Is this possible with regex (e.g. s /.../.../ g) in Vim to format the file? Like the following, but with dynamics, including the necessary spaces:

s/^\(\w\+\)\s*\(.*\)$/\1\t\t\t\2/g
+3
source share
2 answers

This probably doesn't work in the regular expression, since you need to calculate the maximum width for the first word, iterate through the file first, then start with spaces.

If you really want to avoid Tabular, as @Prince Goulash suggested, here is an interesting and simple solution:

let n = system("awk '{ if (length($1) > L) { L = length($1) }}; END { print L }' ".expand("%:p"))
%s/\s\+/\=repeat(' ', n+1-system("awk '{ print length($1) }'", getline('.')))

n awk-. , . expand("%:p") : , .

- . , . , , n ( ) (awk help again!) ( 2 - , ).

.


Tabular, :

:Tab / 

, .

: Tabular?

+1

Align . " Vim ", Vim : 1,2

:let m=0|g/\ze\>/let m=max([m,searchpos(@/,'c')[1]])
:%s//\=repeat(' ',m-col('.'))

- ( , , \>). . :global , ( ). \ze, , , (. :help \ze). :global, , : , .

, ,

:let m=max([m,searchpos(@/,'c')[1]])

searchpos() , parent :global . @/, (. :help "/). , :global /, . c searchpos() (:global ), , , . searchpos() , , - . , :global. searchpos() , . , , , [1]. , . , m .

:%s//\=repeat(' ',m-col('.'))

, , , , m, . , (. :global ) (. :help sub-replace-\=),

repeat(' ',m-col('.'))

repeat() ( ) , . , m-col('.') , (col('.') ).


1 .

:let m=0|exe'g/\ze\>/let m=max([m,searchpos(@/,"c")[1]])'|%s//\=repeat(' ',m-col('.'))

2 , .

+1

All Articles