vim regex 仅用一个空格替换多个连续空格 [英] vim regex replace multiple consecutive spaces with only one space

查看:30
本文介绍了vim regex 仅用一个空格替换多个连续空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用具有可变数量的空格作为单词分隔符的文本文件(像 Word 这样的文本处理器会这样做,以公平地分配由于某些字体中的字母大小不同而导致的空白数量,并且他们将这个烦人的可变数量的即使另存为纯文本时也有空格).

我想自动化用单个空格替换这些具有可变长度的空格序列的过程.我怀疑正则表达式可以做到,但段落开头也有空格(通常是其中四个,但并非总是如此),我希望保持不变,所以基本上我的正则表达式也不应该触及前导空格和这个增加了复杂性.

我正在使用 vim,所以如果可行的话,vim 正则表达式方言中的正则表达式对我非常有用.

我目前的进度如下:

:%s/\+//g

但它不能正常工作.

我也在考虑编写一个 vim 脚本,它可以逐行解析文本行,逐个字符处理每一行并跳过第一行之后的空格,但我觉得这有点矫枉过正.

解决方案

出于实用主义的考虑,我倾向于将其作为一个三个阶段的过程来进行:

:g/^/s//XYZZYPARA/g:g/\+/s///g:g/^XYZZYPARA/s///g

我不怀疑可能有更好的方法(也许使用宏,甚至是纯正则表达式),但我通常在匆忙时发现这种方法有效.当然,如果您有以 XYZZYPARA 开头的行,您可能需要调整字符串 :-)

转身就够了:

 这是一个新的段落跨越两条线.这也是如此,但在一行上.

进入:

 这是一个新的段落跨越两条线.这也是如此,但在一行上.

<小时><块引用>

旁白:如果您想知道为什么我使用 :g 而不是 :s,那主要是习惯.:g 可以做 :s 可以做的一切,还有更多.它实际上是一种在选定行上执行任意命令的方法.在这种情况下,要执行的命令恰好是 s,因此没有真正的区别,但是,如果您想成为 vi 高级用户,则应该查看 :g 在某个时候.

I often work with text files which have a variable amount of whitespaces as word separators (text processors like Word do this, to distribute fairly the whitespace amount due to different sizes of letters in certain fonts and they put this annoying variable amount of spaces even when saving as plain text).

I would like to automate the process of replacing these sequences of whitespaces that have variable length with single spaces. I suspect a regex could do it, but there are also whitespaces at the beginning of paragraphs (usually four of them, but not always), which I would want to let unchanged, so basically my regex should also not touch the leading whitespaces and this adds to the complexity.

I'm using vim, so a regex in the vim regex dialect would be very useful to me, if this is doable.

My current progress looks like this:

:%s/ \+/ /g

but it doesn't work correctly.

I'm also considering to write a vim script that could parse text lines one by one, process each line char by char and skip the whitespaces after the first one, but I have a feeling this would be overkill.

解决方案

In the interests of pragmatism, I tend to just do it as a three-stage process:

:g/^    /s//XYZZYPARA/g
:g/ \+/s// /g
:g/^XYZZYPARA/s//    /g

I don't doubt that there may be a better way (perhaps using macros or even a pure regex way) but I usually find this works when I'm in a hurry. Of course, if you have lines starting with XYZZYPARA, you may want to adjust the string :-)

It's good enough to turn:

    This is a new paragraph
spanning       two lines.
    And    so    is   this but on one line.

into:

    This is a new paragraph
spanning two lines. 
    And so is this but on one line.


Aside: If you're wondering why I use :g instead of :s, that's just habit mostly. :g can do everything :s can and so much more. It's actually a way to execute an arbitrary command on selected lines. The command to execute happens to be s in this case so there's no real difference but, if you want to become a vi power user, you should look into :g at some point.

这篇关于vim regex 仅用一个空格替换多个连续空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆