Here is a slight workaround, which is interesting to learn new functionalities and can solve your problem:
If you consider having a ,
after each element in your array (which is valid, given the 2nd expression), you can use a substitution to do the job:
:s/\({\|,\)/\1\r/g
Let's break it down:
:s/ create a substitution command
\( create a matching group for your regex
{\|, match a { OR a ,
\) end the matching group
/ substitute the matched regex with
\1 the element matched in the group: a { or a ,
\r a end of line character
/g apply for all matching of the line
So you'll get:
const array = {
a: 1,
b: 2,
c: 3,
}
With your cursor on the last {
, and from there <<
will de-indent the closing curly brace.
Now if you want to do the other way around, select your array in linewise V
isual mode and simply press J
.
This will J
oin the lines into one.
Or (as VanLaser) suggested, on the first or last line you can do:
V%J
V
will enter linewise visual mode
%
will go to the matching curly brace
J
will join the lines
Some reading:
:h \|
:h \(
:h \1
:h v_J
:h %
d: 4,
in the second array is an error? Or do you want Vim to add this? Also, which language is this? For some languages there are specialised plugins or tools that can help.