19

Given the following text

Node1_L1
  Node1_L2
  Node2_L2
Node2_L1
  Node3_L2
    Node1_L3
    Node2_L3
  Node4_L2
    Node3_L3
    Node4_L3
Node3_L1
  Node5_L2
  Node6_L2  

I can use vscode's built-in folding feature to fold it to look like so

+ Node1_L1
  Node2_L1
+   Node3_L2
+   Node4_L2
  Node3_L1
    Node5_L2
    Node6_L2

but when I now select the folded text and copy & paste it then it actually grabbed all text - also the hidden one. The result of copy & paste of the first 4 lines of the folded text above would therefore be

Node1_L1
  Node1_L2
  Node2_L2
Node2_L1
  Node3_L2
    Node1_L3
    Node2_L3
  Node4_L2

whereas I would like to have

Node1_L1
Node2_L1
  Node3_L2
  Node4_L2  

Hope that makes sense and someone knows a way to do it. Thanks!

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
NoaHammer
  • 322
  • 2
  • 9
  • I don't use vs code so I can't give a good answer but can you instead do a regex search and then highlight all found lines? In your case, you can search for `^\s{0,2}[^\s]+$` (lines with at most 2 spaces). I don't know if vscode let's use select all found lines though. – Cave Johnson Aug 26 '16 at 18:58
  • Sorry for the late reply and thanks for your suggestion, however my text in the question was just a sample and could be folded in very different ways so it may be very complicated or not possible at all to come up with a matching regex. However, DAXaholic's answer works great for me so no problems left :) – NoaHammer Sep 23 '16 at 17:05

2 Answers2

11

Maybe there is another way of doing it but a workaround seems to be using block selection with multiple cursors - see the GIF

Block selection to copy only top level folding text

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • 1
    What's the shortcut for "block selection"? – WeSam Abdallah Oct 29 '18 at 18:00
  • 2
    @WeSamAbdallah Should be Ctrl+Alt+Down - see the [docs](https://code.visualstudio.com/docs/getstarted/keybindings#_basic-editing) and search for 'Insert Cursor Below' – DAXaholic Oct 30 '18 at 12:01
  • It seems that it doesn't work anymore as of today : the block selection will expand folded code as your cursor goes down – Fogux May 16 '23 at 16:37
  • The working solution I found is Toolbar -> Selection -> Column Selection Mode – Fogux May 16 '23 at 16:47
1

If the selection doesn't include the new line and carriage return, folded content will not be copied.

The selection must go to the start of the next line to select the folded text ( hidden text )

enter image description here

https://github.com/Microsoft/vscode/issues/41922#issuecomment-359368290

The op actually wants to select 'unfolded' text ignoring the folded text so they need to use a multi-line select where each selection will span a single line

ta32
  • 131
  • 1
  • 8