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 |



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

2 Answers 2


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()

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 and acknowledge you have read our privacy policy.

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