Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a carriage return in VS Code replace string [duplicate]

I am trying out the new Microsoft Visual Studio Code editor in Linux Fedora environment. I would like to know how to replace new line (\n) in place of some other text.

For example, I have html text like this

<tag><tag> 

which I would like to replace as

<tag>
<tag>

In sublime I would use regex pattern and find "><" and replace with ">\n<" How do I accomplish this in Visual Studio Code?

like image 643
Sriram Avatar asked May 20 '15 13:05

Sriram


3 Answers

In the local searchbox (ctrl + f) you can insert newlines by pressing ctrl + enter.

Image of multiline search in local search

If you use the global search (ctrl + shift + f) you can insert newlines by pressing shift + enter.

Image of multiline search in global search

If you want to search for multilines by the character literal, remember to check the rightmost regex icon.

Image of regex mode in search replace


In previous versions of Visual Studio code this was difficult or impossible. Older versions require you to use the regex mode, older versions yet did not support newline search whatsoever.

like image 146
Håkon Egset Harnes Avatar answered Oct 15 '22 12:10

Håkon Egset Harnes


With VS Code release 1.38 you can press CTRL + Enter in the editor find box to add a newline character.

enter image description here

With VS Code release 1.30 you can type Shift + Enter in the search box to add a newline character without needing to use regex mode.

enter image description here

Since VS Code release 1.3, the regex find has supported newline characters. To use this feature set the find window to regex mode and use \n as the newline character.

Multiline find in VS Code gif

like image 34
Chic Avatar answered Oct 15 '22 12:10

Chic


  • Control F for search, or Control Shift F for global search
  • Replace >< by >\n< with Regular Expressions enabled

enter image description here

like image 45
Casper Dijkstra Avatar answered Oct 15 '22 14:10

Casper Dijkstra