gpt4 book ai didi

vim - 如何在Vim中顺序执行多个全局命令? (错误: "Cannot do :global recursive")

转载 作者:行者123 更新时间:2023-12-04 03:59:47 24 4
gpt4 key购买 nike

我想使用以下全局命令根据其首字母对段落进行排序:

g/_p/,/^$/mo$

这单独起作用。但是,当我一次使用多个全局命令时,就会发生错误:
g/_p/,/^$/mo$|g/_w/,/^$/mo$

这给出了以下错误:
Cannot do :global recursive

如何一次依次运行这些命令?

最佳答案

:exe 'g/_p/,/^$/mo$' | g/_w/,/^$/mo$

要追加更多 global命令,只需将它们包装在 execute中:
:execute 'g/aaa/s//bbb/g ' | execute 'g/ccc/s/ddd//g' | execute 'g/eee/s/fff/ggg/g' | g/^cake/s/$/ is a lie/g

错误的原因在 :help :bar 中:

*:bar* *:\bar* | can be used to separate commands, so you can give multiple commands in one line. If you want to use | in an argument, precede it with \.

These commands see the | as their argument, and can therefore not be followed by another Vim command:

  • (.. list of commands ..)
  • :global
  • (.. list of commands ..)

Note that this is confusing (inherited from Vi): With :g the | is included in the command, with :s it is not.

To be able to use another command anyway, use the :execute command.



这也回答了为什么以下链可以正常工作的原因:
%s/htm/html/c | %s/JPEG/jpg/c | %s/GIF/gif/c

关于vim - 如何在Vim中顺序执行多个全局命令? (错误: "Cannot do :global recursive"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com