Curly bracket substitution in sed, nvi, Vim


What is it they say about regular expressions? I got caught out making a basic mistake substituting curling brackets at the start of lines in a text file, so I thought I’d share it.

Here was my first attempt in FreeBSD nvi, escaping the bracket:

==> RE error: repetition-operator operand invalid.

Hmm, weird. The same happened with nvi installed from MacPorts on my work laptop. So I tried Vim:

==> E866: (NFA regexp) Misplaced {
==> E64: \{ follows nothing
==> E476: Invalid command
==> Press ENTER or type command to continue

This should be simple, what was I doing wrong? I tried sed to mimic what I was doing above:

$ sed -i '' -e 's/^\{//' $_FILE
==> sed: 1: "s/^\{\"id\"\:\"//
==> ": RE error: invalid repetition count(s)

Can you see the problem?

Not to get all Malcolm Gladwell on you, but turns out I shouldn’t have been escaping the curly bracket. So doing this was sufficient:


I’m going to dub this premature escaping. Or maybe Ruben shouldn’t do early morning regex without coffee and/or sufficient sleep.

Author bio and support


Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.