Home > Net >  VSCode - Is there a way to apply snippets to each line of a selection?
VSCode - Is there a way to apply snippets to each line of a selection?

Time:12-07

I have a snippet. I want to select multiple lines - a set of relative paths to images that I have pasted in - and have the snippet be applied to each line separately. The snippet menu doesn't seem to have the same capabilities as Emmet (* treats each line separately)

CodePudding user response:

Yes, it is possible to apply a snippet to each line of a selection in Visual Studio Code. To do this, you can use the Shift Alt I shortcut to "wrap" the selected text with the snippet. This will insert the snippet at the beginning and end of the selected text, and will apply the snippet to each line of the selection individually.

For example, if you have the following selection:

image1.png
image2.png
image3.png

And you want to wrap it with a snippet that inserts an img tag around each line, you can use the Shift Alt I shortcut to insert the snippet at the beginning and end of the selection, like this:

<img src="image1.png">
<img src="image2.png">
<img src="image3.png">

Alternatively, you can also use the Edit > Wrap Selection menu item to accomplish the same thing.

I hope this helps! Let me know if you have any other questions.

CodePudding user response:

  • add multi cursors at each line: use Ctrl Alt Down or Ctrl Alt Up
  • or any other method for multi cursor if you have few Alt Click or a lot Shift Alt Click
  • select the text of each line
  • type the snippet prefix and select the snippet

this works if the snippet uses the variable ${TM_SELECTED_TEXT}

  • Related