テキストを両端揃えにする新しい関数を作成しています。いくつかのプラグインがあることは知っていますが、それらは私がやりたいことをしないので、自分で関数を作成することにしました。
前:
text_text_text_text_____
後:
text__text___text___text
私が最初にしたことは、単語の数とスペース以外の文字の数を見つけることです(テキストのすべての行について):
let @e = '' | redir @e | silent exe i.'s/'.cols.'\(\S\+\)/&/gne' | redir END
if matchstr(@e, 'match') != '' | let nrwords = matchstr(@e, '\d\+\ze') | else | continue | endif
let @e = '' | redir @e | silent exe i.'s/'.cols.'\S/&/gne' | redir END
if matchstr(@e, 'match') != '' | let nonspaces = matchstr(@e, '\d\+\ze') | else | let nonspaces = 0 | endif
次に、スペースを見つけるには:
let spaces = textwidth_I_want - nonspaces
単語間のスペースを分割する必要があります。
let SpacesBetweenWords = spaces/(str2float(nrwords)-1)
ただし、多くの場合、浮動小数点数です。
ペspaces = 34
nr.words-1 = 10
SpacesBetweenWords = 3.4
私がやりたいことは、次のように単語間のスペースを分割することです:
Spaces between words:
4 3 3 4 3 3 4 3 3 4
そしてそれらをリスト 'SpaceList' に入れます
そして、それらを単語の間に挿入します
for m in range(1,len(SpaceList))
exe i 's/^'.cols.'\(\S\+\zs\s\+\ze\S\+\)\{'.m.'}/'.repeat(' ', SpaceList[m-1]).'/'
endfor
(列 = 私のブロック選択または行全体)
私の例
3 3 3 3 3 3 3 3 3 3
(スペース = 30)ですべての整数 pe を含むリストを作成するのは簡単
ですが、単語間を区切るためにまだ 4 つのスペースがあります。
私の問題は、「これらのスペースを単語数でどのように分割できるか」です。