0

I am aware about surrounding selection in quotes or braces

PhpStorm wrap/surround selection?

I need similar but surround selection into array Assume I have text separated with newline or text separated with space

a

b

c

d

e

a b c d e

I need after selection get [a,b,c,d,e]

Please advice any IDE or method how to achieve this

IamMashed
  • 1,811
  • 2
  • 21
  • 32
  • The following question is the same as yours in vim context only: https://stackoverflow.com/questions/62069376/creating-a-list-or-tuple-out-of-one-word-per-line-in-vim/62071578#62071578 – Luc Hermitte Jun 03 '20 at 14:13

3 Answers3

1

A no-brainer solution

Make sure the following option is enabled:

enter image description here

For text separated with spaces:

  • Select the 'array-to-be', hit Ctrl+R and R again (to enter 'In selection' mode)
  • Type a space in the first field, , in the second
  • Click 'Replace all', hit Esc
  • With the text still selected, press [: the closing bracket will be added automatically

For multi-line text:

  • Select the 'array-to-be', hit Ctrl+R, enable 'Regex'
  • Type \n + in the first field, , in the second
  • Click 'Replace all', hit Esc
  • With the text still selected, press [: the closing bracket will be added automatically

The sequence of actions can be wrapped into a macro and assigned a single shortcut.

Koyasha
  • 5,275
  • 2
  • 8
  • 17
1

Vim + Surround plugin. Enter visual mode, select what you need and press S].

Dmitry
  • 436
  • 2
  • 7
0

The first group could be created by:

:%s/\v(\w)(\n)*/\1,/g | exec "norm! I[\<Esc>A]"

It takes care of:

a

b
...

\v ........... very magic regex
() ........... regex group
(\w) ......... letters
(\n)* ........ zero or more line breaks
\1 ........... repeat content of the first regex group
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40