719

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?

6
  • Possible duplicate of Use a carriage return in VS Code replace string Dec 15, 2016 at 10:21
  • 3
    @RobertMacLean this question was asked before the above tagged question
    – Sriram
    Dec 16, 2016 at 11:38
  • 2
    @RobertMacLean creating a duplicate cycle Jun 17, 2017 at 15:09
  • 1
    Actually not a duplicate: The other question asks about carriage return, this one about newline, and no the answer does not work for carriage return for some reason. Nov 12, 2019 at 22:38
  • 3
    Shift + Enter will do the job
    – Shayan
    Feb 15, 2020 at 12:52

11 Answers 11

1229

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.

14
  • 6
    Nor in version 0.10.3. But there is an extension that can do this: marketplace.visualstudio.com/items/jmallien.ReplaceWithNewline
    – rmac
    Dec 3, 2015 at 19:40
  • 15
    Note that while replacing with newline characters is supported, finding newline characters still does not work until github.com/Microsoft/vscode/issues/313 is fixed.
    – C Snover
    Jan 15, 2016 at 19:55
  • 6
    This doesn't work on the mac - it literally replaces \r or \n Jun 20, 2016 at 14:14
  • 47
    Note that regex mode must be on. If not, it won't use a newline.
    – neves
    Jul 6, 2016 at 19:48
  • 14
    regex search for \\n and replace with \n works for me, using 1.25.0
    – Gregor
    Jul 9, 2018 at 15:37
241

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

3
  • 2
    This gave me a little push into the right direction. I needed the empty line to replace HTML comments: <!--[\s\S\n]*?--> And I was missing the \n :)
    – Guntram
    Oct 13, 2016 at 10:54
  • 1
    I am loving this feature, works in the search and replace fields too!
    – SimplyInk
    Dec 21, 2016 at 8:24
  • 5
    Still doesn't work in the ctrl+shift+f search sidebar... -_-
    – Xunnamius
    Jun 18, 2018 at 18:37
82

In version 1.1.1:

  • Ctrl+H
  • Check the regular exp icon .*
  • Search: ><
  • Replace: >\n<
1
  • 18
    Thanks for mentioning that you have to press the regexp icon! Jan 26, 2017 at 17:00
46

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.

3
  • 1
    Hi @Stiv great tip but this really should have been a comment since it does not answer the question.
    – Chic
    Jun 23, 2017 at 16:21
  • Might not have looked in the comments... this was the answer I needed. Thanks @stiv!
    – Jack Ryan
    Jul 1, 2019 at 21:32
  • Might not answer the question, but this is exactly what I am looking for.
    – fsevenm
    Mar 25 at 2:11
28
  • Control F for search, or Control Shift F for global search
  • Replace >< by >\n< with Regular Expressions enabled

enter image description here

19

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.

1
  • Ctrl+D is great! You may need to press Ctrl+F first and after a few Ctrl+Ds or skipping with Ctrl+K Ctrl+D, you may press Esc to place the cursor in all selections!
    – CPHPython
    Aug 29, 2019 at 10:57
7

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

5

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

Shorter version: CTRL+H ALT+R \n

2

At least for me in VS Code Version 1.62.3 on Windows and VS Code Version 1.75 on MacOS (Ventura) it is working with regex, as shown below:

  1. Replace Menu

    Windows: CTRL+R

    Mac: command+option+F

  2. Activate "Use Regular Expression (ALT + R)"

    Windows: ALT+R

    Mac: command+option+R

  3. Use <> in the Search field and >\n< in the Replace field

  4. Press "Replace All (CRTL+ALT+Enter)" with an empty field

    Windows: CTRL+ALT+ENTER

    Mac: command+option+R

In case of the initial question, it would look like this:

enter image description here


Hint: Use [\n] or \n to find all newlines

enter image description here

In addition, to find newlines at the beginning of a line, you can also use ^[\n] or ^\n:

enter image description here

1

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

-2

In case if you want to remove extra new lines from code then use this in vs code find a section

for opening the find box use ctrl + f in vs code

then find all new lines from code type this in the find box

^\n 

also only choosing Use regular expression option means the last option of the find box. After selecting this you will see all new lines on the code highlighted. then on replace box leave it as it is and click replace all option.

Same as if you want to add a new line where ever you found space then on find, box provide one space than on replace box add

^\n and click return all 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.