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!
3
  • 1
    Possible duplicate of What is the most efficient way to invert two words?
    – Tumbler41
    Sep 21, 2016 at 18:11
  • Unrelated to your question: the syntax of your second s/.../.../ command is wrong. I presume it's inspired by Perl regexps, but it would be wrong even in Perl. Either way, it has nothing to do with Vim. Read :h regexp and :h :substitute to learn about Vim syntax. Sep 22, 2016 at 4:47
  • Thanks for the info. Can you please show where it's wrong? both are working for me. I'm using VRapper on Eclipse, not Vim itself. Sep 22, 2016 at 12:32

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.