Vim 对齐文本,如何划分单词之间的空格?

Vim 对齐文本,如何划分单词之间的空格?,vim,text,format,justify,viml,Vim,Text,Format,Justify,Viml,我正在创建一个新函数来调整文本。我知道有一些插件,但它们不做我想做的事情,所以我决定自己创建一个函数 之前: text_text_text_text_____ 之后: text__text___text___text 我首先做的是查找单词数和非空格字符数(对于我文本中的每一行): 然后查找空间: let spaces = textwidth_I_want - nonspaces 我必须在单词之间划分空格: let SpacesBetweenWords = spaces/(str2float

我正在创建一个新函数来调整文本。我知道有一些插件,但它们不做我想做的事情,所以我决定自己创建一个函数

之前:

text_text_text_text_____
之后:

text__text___text___text
我首先做的是查找单词数和非空格字符数(对于我文本中的每一行):

然后查找空间:

let spaces = textwidth_I_want - nonspaces
我必须在单词之间划分空格:

let SpacesBetweenWords = spaces/(str2float(nrwords)-1)   
然而,它通常是一个浮点数。
p、 e.
空格=34

nr.words-1=10

SpacesBetweenWords=3.4

我想做的是在单词之间划分空格,如下:
单词之间的空格:
434334

并将它们放在“空间列表”列表中

然后将它们插入单词之间

 for m in range(1,len(SpaceList))
    exe i 's/^'.cols.'\(\S\+\zs\s\+\ze\S\+\)\{'.m.'}/'.repeat(' ', SpaceList[m-1]).'/'
 endfor
(cols=我的块选择或整行)

在我的示例中,创建一个包含所有整数的列表是很容易的
33
(空格=30)
但是单词之间还有4个空格。

我的问题是“如何在字数之间划分这些空格”

结果是
3.4
,这意味着,不能使相同长度的单词之间的所有距离相等。您必须调整
textwidth\u I\u I\u want
值以使结果为整数,或者将距离设置为
3
,并计算仍需要多少空格(
10*(3.4-3)=4
)。您可以将这4个空格添加到4个距离。

使用python找到了解决方案

let r = float2nr(SpacesBetweenWords)
let places = nrwords-1
"the extra spaces to put between words
let extraspaces2divide = float2nr(spaces-r*places)
"find randomly with the help of python a serie of (unique) numbers for every space to divide, numbers between 1 and the number of words
exe "py import vim,random;  Listvalue = random.sample(xrange(1,".(places+1).",1),".extraspaces2divide.");vim.command(\"let randomlist = '%s'\"% Listvalue)"

"the result of python is a string like this [1, 3, 6, 9]: remove the brackets and spaces and split the result to a list
let randomlist = substitute(randomlist, '[\[\] ]', '', 'g')
"this list is the serie of random numbers (= the place between words) where to add an extra space
let list = sort(split(randomlist, ','))

for s in range(1,nrwords-1)
  if (index(list, ''.s.'') != -1)
    let r2 = r+1
  else
    let r2 = r
  endif
  call add(SpaceList, r2)
endfor

谢谢Kent,你知道如何随机分配这4个空格(在公式中)吗?这有什么区别吗?将每个空格放在前4个距离,或最后4个距离,或随机放置?哦,是的。如果我把每一个额外的空格放在前四个距离中,我会在文本的左侧看到更多的空格。
let r = float2nr(SpacesBetweenWords)
let places = nrwords-1
"the extra spaces to put between words
let extraspaces2divide = float2nr(spaces-r*places)
"find randomly with the help of python a serie of (unique) numbers for every space to divide, numbers between 1 and the number of words
exe "py import vim,random;  Listvalue = random.sample(xrange(1,".(places+1).",1),".extraspaces2divide.");vim.command(\"let randomlist = '%s'\"% Listvalue)"

"the result of python is a string like this [1, 3, 6, 9]: remove the brackets and spaces and split the result to a list
let randomlist = substitute(randomlist, '[\[\] ]', '', 'g')
"this list is the serie of random numbers (= the place between words) where to add an extra space
let list = sort(split(randomlist, ','))

for s in range(1,nrwords-1)
  if (index(list, ''.s.'') != -1)
    let r2 = r+1
  else
    let r2 = r
  endif
  call add(SpaceList, r2)
endfor