5

I have a legacy system that only accepts Windows \r\n and I want to edit a file in VS Code that has just \n to have \r\n.

I'm trying to use a Regex replace:

regex replace in VS Code

But this puts literal \r in instead of the whitespace char.

I've tried putting a newline in the replacement using SHIFT+ENTER:

SHIFT+ENTER replace in VS Code

But this just puts in \n.

How do I change the line feed chars used and save the file in VS Code?

Keith
  • 150,284
  • 78
  • 298
  • 434
  • I think you must turn off regex, because it detects new line in your case. – Atahan Atay Jun 04 '20 at 11:20
  • 3
    use command `Change End of Line Sequence` – rioV8 Jun 04 '20 at 11:53
  • Your approach doesn't work because line feeds in the search and replace dialogue seem to be an abstraction, so you don't need to craft different expressions for different EOL styles. In any case, this program seems to support up to three different regexp engines, depending on settings and context, and I suspect they've been slightly customised. ([What flavor of Regex does Visual Studio Code use?](https://stackoverflow.com/questions/42179046/what-flavor-of-regex-does-visual-studio-code-use?)). – Álvaro González Jun 07 '20 at 10:03

4 Answers4

6

There's the text "LF" in the bottom bar on the right, click on it and select "CRLF". Or press Ctrl+Shift+P and enter Change End of Line Sequence.

No idea why your approach doesn't work. Nor does \x0D or \15 get recognized. I'd call it a bug.


For multiple files, on Linux, I'd do it outside of the editor, e.g., with

find somedir -name '*.someext' -exec perl -pi -e 's/\n/\r\n/' {} +
maaartinus
  • 44,714
  • 32
  • 161
  • 320
1

You can try this one In the local searchbox (Ctrl + F) you can insert newlines by pressing Ctrl + Enter. https://www.graef.io/find-and-replace-with-a-newline-in-visual-studio-code/

Unyil
  • 61
  • 1
  • 6
0

Just press Ctrl+H and select regex replace. Then start input:

Find what: ^\n

Replace with: \r\n

Trancer
  • 765
  • 2
  • 8
  • 23
  • 1
    Did you read the question? There's a screenshot of me doing that. This is over a year old so maybe it's been fixed since then. – Keith Nov 03 '21 at 21:47
  • @Keith, Not, you're not right at all. On your screenshot you did a little bit other thing. In my case my option helped me against your method that on screenshot – Trancer Nov 05 '21 at 09:37
  • The only difference I can see is that you added `^` before `\n` to avoid replacing it unless it's at the start/line-start (depending on `m` flag), but the regex `\n` _finds_ it just fine - your answer is a tweak to the find regex but the problem is the replace treated `\r\n` as a literal string. – Keith Nov 05 '21 at 16:34
0

Visual Studio Code provides an option to Select End of Line Sequence in its taskbar on bottom right:

Select End of Line Sequence in VSC taskbar

When clicked, it'll provide an option to choose between CRLF (Carriage return and Line feed: Windows default) and LF (Line feed alone: Linux supported):

Select End of Line Sequence in VSC Selector

Make sure to save the file once the EOL sequence is changed.

tdy
  • 36,675
  • 19
  • 86
  • 83