11

I want to sort the paragraphs in my text according to their initials with the following global command:

g/_p/,/^$/mo$

This works alone. But when I use several global commands at once, then an error occurs:

g/_p/,/^$/mo$|g/_w/,/^$/mo$

This gives the following error:

Cannot do :global recursive

How can I run these commands sequentially at once?

toraritte
  • 6,300
  • 3
  • 46
  • 67
Mert Nuhoglu
  • 9,695
  • 16
  • 79
  • 117
  • ([Here](https://superuser.com/a/366354/21887) are a few lines of vimscript that sort all paragraphs. But they recognize the paragraphs by indentation, not empty lines. But surely one could adapt it to distinguish paragraphs by empty lines.) – Aaron Thoma Dec 18 '19 at 05:21

1 Answers1

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

To append more global commands, just keep wrapping them in 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

The reason for the error is in :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.

This also answers why the below chain would work without any issues:

%s/htm/html/c | %s/JPEG/jpg/c | %s/GIF/gif/c
toraritte
  • 6,300
  • 3
  • 46
  • 67
Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34
  • 2
    In this case it is not necessary to wrap both `:global` commands in `:execute`, it is enough to wrap only the first one: `:exe'g/_p/,/^$/mo$'|g/_w/,/^$/mo$`. – ib. Mar 18 '12 at 01:04
  • @ib.: I incorporated your suggestion. Nice one, thanks! I especially like the fact, that you even saved the space between `:exe` and its parameter `'…`. xD – Aaron Thoma Mar 18 '12 at 21:23