Search code examples
intellij-ideavimpycharmsublimetext3atom-editor

Surround selection into array


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


Solution

  • 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.