5

Is there a way to set Negate in the substitution command? In other words how to substitute any character except | to - and all | to +. Example:

| some text | other text |

to

+-----------+------------+

Is there a shortest way than :s/[^|]/-/g | s/|/+/g ?

7

You can use the replace special substitution:

:s/./\= submatch(0) == '|' ? '+' : '-'/g

Here \= says to use the following expression to use as the result string.

And it's checking for submatch(0) (the char matched by the /./ pattern) and do a condition on that.

Have a look at:

  • :h sub-replace-special
  • :h submatch()
3

As an alternative you can use the system's tr program:

:.!tr '| -z' '+-'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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