Konstantin Glukhov
Konstantin Glukhov

Reputation: 2186

In Vim, how to insert a word after regex match at the begining of next line

A word needs to be injected at the beginning of each line 1. I tried the following, but obviously it does not work :g/^=/+1i/myword/

File structure:

===============
line 1
line 2
...
===============
line 1
line 2
...

Upvotes: 2

Views: 1514

Answers (2)

Wray Zheng
Wray Zheng

Reputation: 997

You can also do it by :s command:

:%s/=\n\zs/myword

\zs denotes that preceding pattern is zero-width, which is just used as a matching condition, and won't be captured.

Upvotes: 4

romainl
romainl

Reputation: 196546

You are almost there:

:g/^=/+1normal Imyword

Note that +1 could be shortened to + and :normal to :norm:

:g/^=/+norm Imyword

See :help :insert for why your method couldn't work and :help :normal.

Upvotes: 6

Related Questions