Using a text editor, without writing JavaScript functions (e.g. can’t use this great answer: Replacing the nth instance of a regex match in Javascript)
How do I take something like this (a proprietary code structure I have no control over)
map(key1, value1, key2, value2, key3, value3, key4, value4 ...)
And convert it to this
map(
key1, value1,
key2, value2,
key3, value3,
key4, value4
...
)
One option I found was using this regex to find every 2nd comma (([^,]*,){2})
Then replace with \1\n\t
But I would like to improve it:
1. It doen’t handle the first and last lines very well
map(key1, value1,
key2, value2,
key3, value3,
key4, value4 ...)
2. Only works on a flat structure
e.g. I can’t think of a way to transform this
map(key1, value1, key2, map(key3, value3, key4, value4 ...), key5, value5)
To this
map(
key1,value1,
key2,map(
key3,value3,
key4,value4
),
key5,value5
)
Using regex, or is there a way?
Eran, oh you finally tag the question with
vim, nice! ^_^vim can format it a bit, I wrote a small function:
Note that I used
\ras linebreak, if it doesn’t work for you, change into\nYou can put it in your
.vimrcfile, if you use that very often. Also you could just give it a try by typing:so %You can create a map for that function call by:
in this way, if you want to reformat your
mapline, just move cursor to that line, and inNormalmode type<leader>r(default is \)This function will change
into
Here I show how it works:
k#,v#Now, when are you gonna uninstall your notepad++? ^_^