Vim, reformat text to initializers

I have a large file with lines that look like

2 No route to the specified transit network

3 No route to destination

i.e. number at the beginning of the line followed by a description.

And I would like to convert this to use as a structure initializer

{2, "There is no route to the specified transit network"},

{3, "No route to destination"},

How can I do it?

+3
source share
1 answer

Try

:%s/^\(\d\+\)\s\(.*\)$/{\1, "\2"},/

This uses search and replace and searches for a line starting with a digit, followed by a space, followed by arbitrary text to the end of the line. This is replaced by the template you specified.

, " " ( Al ):

:%s/\v^(\d+)\s(.*)$/{\1, "\2"},/
+8

Source: https://habr.com/ru/post/1716669/


All Articles