Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append to line in vim matching regex

Tags:

vim

vi

ex

I have a json file that I ran a find and replace in using vim, however I forgot a , at the end of the line.

...
"id":41483
"someName":"someValue",
...

Using vim, how can I append a , to every line matching \"id\"\:[0-9].*$?

like image 735
Matt Clark Avatar asked Dec 25 '22 06:12

Matt Clark


2 Answers

Try this. Match everything that has id followed by any character till the end. Replace it with the matching group (matched by parentheses) which in the replace segment is represented by \1.

%s/\(id".*\)$/\1,/g
like image 106
Nikhil Baliga Avatar answered Jan 13 '23 22:01

Nikhil Baliga


Another way to do this is with a global command and the normal command.

:g/"id":[0-9]/norm A,

The global command runs norm A, on every line that matches "id":[0-9]. norm A, runs A, in normal mode which is append a , at the end of the line.

Take a look at :help :global and :h :normal

like image 20
FDinoff Avatar answered Jan 13 '23 21:01

FDinoff