3

Giving these 2 substitution commands in vim:

%s/}$/\r}\r/g
%s/^\s*([a-zA-Z\#\.\:\s\(\)-\[\]'=]*)\s*\{/$1 \{/g

I have 2 questions:

  • How can I join 2 substitution commands in a one line command ?
  • Is there a better regex command to make this a oneliner ?

I know I could make a function with both commands, but I'm in Eclipse VRapper not in Vim itself. AFAIU I can't have functions in it.

Edit

That question suggested doesn't solve the problem, because these substitutions should be made one after the other. They convert a compiled sccs from nested to expanded output style. It's not just two occurences that can be substituted in text.

What I would like in the first question is something like this PHP code:

$w = "Hi Planet!";
echo str_replace("Hi", "Hello", str_replace("Planet", "World", $w));
// result: Hello World!
Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
Nelson Teixeira
  • 319
  • 2
  • 14

1 Answers1

7

The usual way to concatenate two commands in one line in Vim is to use a pipe |. Unlike shells, | in Vim is for sequential, conditional execution - it's more like a && in shells.

So, in this case:

%s/}$/\r}\r/g | %s/^\s*([a-zA-Z\#\.\:\s\(\)-\[\]'=]*)\s*\{/$1 \{/g

Additional notes from :h :|:

You can also use <NL> to separate commands in the same way as with '|'.  To
insert a <NL> use CTRL-V CTRL-J.  "^@" will be shown.  Using '|' is the
preferred method.  But for external commands a <NL> must be used, because a
'|' is included in the external command.  To avoid the special meaning of <NL>
it must be preceded with a backslash.  Example: 
        :r !date<NL>-join
This reads the current date into the file and joins it with the previous line.

Note that when the command before the '|' generates an error, the following
commands will not be executed.
muru
  • 24,838
  • 8
  • 82
  • 143