Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace with a newline in Visual Studio Code

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 924
Sriram Avatar asked May 20 '15 13:05

Sriram


People also ask

How do I insert a new line in Find and Replace?

Select the cells that you want to search. On the keyboard, press Ctrl + H to open the Find and Replace dialog box, with the Replace tab active. On the Replace tab, click in the Find What box. On the keyboard, press Ctrl + J to enter the line break character.

Can you find and replace in Visual Studio code?

Find and Replace# VS Code allows you to quickly find text and replace in the currently opened file. Press Ctrl+F to open the Find Widget in the editor, search results will be highlighted in the editor, overview ruler and minimap.

How do you insert a line break in VS Code?

For Windows, Pressing Alt+Z will break the line.

How do you see the new line character in Visual Studio code?

AFAIK there is no way to visually see line endings in the editor space, but in the bottom-right corner of the window there is an indicator that says "CLRF" or "LF" which will let you set the line endings for a particular file. Clicking on the text will allow you to change the line endings as well.


9 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 130
Håkon Egset Harnes Avatar answered Oct 17 '22 03: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 41
Chic Avatar answered Oct 17 '22 05:10

Chic


In version 1.1.1:

  • Ctrl+H
  • Check the regular exp icon .*
  • Search: ><
  • Replace: >\n<
like image 29
Eric Bole-Feysot Avatar answered Oct 17 '22 05:10

Eric Bole-Feysot


Also note, after hitting the regex icon, to actually replace \n text with a newline, I had to use \\n as search and \n as replace.

like image 34
Stiv Ostenberg Avatar answered Oct 17 '22 03:10

Stiv Ostenberg


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

enter image description here

like image 39
Casper Dijkstra Avatar answered Oct 17 '22 03:10

Casper Dijkstra


A possible workaround would be to use the multi-cursor. select the >< part of your example use Ctrl+Shift+L or select all occurrences. Then use the arrow keys to move all the cursors between the tags and press enter to insert a newline everywhere.

This won't work in all situations.

You can also use Ctrl+D for select next match, which adds the next match to the selection and adds a cursor. And use Ctrl+K Ctrl+D to skip a selection.

like image 36
Bastiaan Deknudt Avatar answered Oct 17 '22 03:10

Bastiaan Deknudt


On my mac version of VS Code, I select the section, then the shortcut is Ctrl+j to remove line breaks.

like image 37
tripleonard Avatar answered Oct 17 '22 05:10

tripleonard


CTRL + H, then select regex (*) and write \n

Shorter version: CTRL+H ALT+R \n

like image 33
TomoMiha Avatar answered Oct 17 '22 05:10

TomoMiha


with v1.31.1 in RegEx mode the Replace All functionality is broken. clicking that button replaces only one instance

like image 37
ekkis Avatar answered Oct 17 '22 05:10

ekkis