【问题标题】:In Vim how do I split a long string into multiple lines by a character?在 Vim 中,如何将一个长字符串按一个字符分成多行?
【发布时间】:2013-05-26 21:16:13
【问题描述】:

我有这么长的正则表达式字符串

(\.#.+|__init__\.py.*|\.wav|\.mp3|\.mo|\.DS_Store|\.\.svn|\.png|\.PNG|\.jpe?g|\.gif|\.elc|\.rbc|\.pyc|\.swp|\.psd|\.ai|\.pdf|\.mov|\.aep|\.dmg|\.zip|\.gz|\.so|\.shx|\.shp|\.wmf|\.JPG|\.jpg.mno|\.bmp|\.ico|\.exe|\.avi|\.docx?|\.xlsx?|\.pptx?|\.upart)$

我想将其拆分为| 并将每个组件放在一个新行上。

所以最终的形式是这样的

(\.#.+|
__init__\.py.*|
\.wav|
\.mp3|
\.mo|
\.DS_Store|
... etc

我知道我可能可以将其作为宏来执行,但我认为更聪明的人可以找到更快/更简单的方法。

感谢任何提示和帮助。谢谢!

【问题讨论】:

标签: vim


【解决方案1】:

用它自己和换行符 (\r) 替换 | 的每个实例:

:s/|/|\r/g

(在执行之前确保您的光标在相关行上)

【讨论】:

  • 与原问题或答案无关,但为什么是\r而不是\n
  • @Aperçu 我没有研究为什么,但是在 VIM 中你必须使用\r作为替换字符,它会为底层操作系统做正确的事情。如果您使用\n 作为替代品,结果将不会是人们所期望的。
  • @Apalala 这是对您问题的回答,而不是问题;)
  • @Aperçu 不错。我认为您的 8.5k 允许您编辑答案;-)
【解决方案2】:

试试这个:

:s/|/|\r/g

以上将适用于当前行。

要对整个文件执行替换,请在 s 之前添加 %

:%s/|/|\r/g

细分:

:    - enter command-line mode
%    - operate on entire file
s    - substitute
/    - separator used for substitute commands (doesn't have to be a /)
|    - the pattern you want to replace
/    - another separator (has to be the same as the first one)
|\r  - what we want to replace the substitution pattern with
/    - another separator
g    - perform the substitution multiple times per line

【讨论】:

  • 我的也是;没有解释...`:`
  • @AndrewMarshall - 也许我们的回答有效...我已经为你投票了 :-)
【解决方案3】:

实际上你不必在模式之前添加|,试试这个s/,/,\r/g它将在换行符后用逗号替换逗号。

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2018-10-11
    • 2019-03-14
    • 1970-01-01
    • 2011-09-01
    • 2013-12-25
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多