vim search for ) replace with text then newline then )

Problem

I have a huge sql query with 574 lines and I needed to replace a closing bracket ) with text and then a new line with a closing bracket

Before:


) AS

After:


GROUP BY v.visit_id
) AS

Solution

Using vim’s search and replace feature


:%s/^)/GROUP BY v.visit_id\r)/gc

The above searches for the lines beginning with the closing bracket ) and then replaces it with text and then new line (\r) followed by a closing bracket )

References

How to replace a character by a newline in Vim

Add a newline after given patterns