Home > other >  how to make text editing in the following way in vscode
how to make text editing in the following way in vscode

Time:06-24

I have a text file in the following format

some_text_1_1 | some_text_2_1 | some_text_3_1
some_text_1_2 | some_text_2_2 | some_text_3_2
some_text_1_3 | some_text_2_3 | some_text_3_3
...

And I want to transform it into the following

[
    {"a": "some_text_1_1", "b":"some_text_2_1", "c":"some_text_3_1"},
    {"a": "some_text_1_2", "b":"some_text_2_2", "c":"some_text_3_2"},
    {"a": "some_text_1_3", "b":"some_text_2_3", "c":"some_text_3_3"},
   ...
]

Is there a convenient way (such as through multi-cursor or block editing editing etc., NOT through regex if possible) to achieve that in vscode?

CodePudding user response:

The extension Select By could help.

I would suggest start with the c column.

  • Place cursors at the start of the lines
  • with MoveBy: Move cursors based on regex move twice to the next |
  • with Shift End select the c-text
  • Ctrl X
  • type , "c":"Ctrl V"}, Ctrl V is the key to past text
  • type Home to go to start of line
  • with MoveBy: Move cursors based on regex move to the next |
  • with SelectBy: Mark positions of cursors
  • with MoveBy: Move cursors based on regex move to the next |
  • with SelectBy: Mark positions of cursors
  • Ctrl X
  • type , "b":"Ctrl V" Ctrl V is the key to past text
  • type Home to go to start of line
  • with SelectBy: Mark positions of cursors
  • with MoveBy: Move cursors based on regex move to the next |
  • with SelectBy: Mark positions of cursors
  • Ctrl X
  • type { "a":"Ctrl V" Ctrl V is the key to past text

The regex to search for is \| because | is a special char in regex

CodePudding user response:

I would just use the multi-cursor option. Put a cursor on every one of those lines (method may vary depending on bound keystrokes but I use Ctrl-Alt-DownArrow to extend the cursor down).

Then press Home and start typing:

{"a": "<right-arrow to before' |'><del x 2>", "b": ... and so on.

Then you just need to wrap it in [ ].

Note that right-arrow method is if all your items are same size as per the question. If they are different sizes, you'll need to use different cursor movement commands such as Ctrl-RightArrow.

  • Related