Sublime Forum

CRLF isn't found when using Regex

#1

Sublime Text can’t find CRLF in the regex search of \r\n however Atom and Notepad++ have no problems finding them. This is the case with at least the last several dev builds of ST3, and probably earlier ones.

2 Likes

#2

I believe this is because views never have crlf. Sublime normalizes line endings. So in a view, you only have new lines, and if you have the Windows line ending enabled for that file, on save, newlines have the crlf added the the file on disk.

0 Likes

#3

It’s not just in a view, if I do a global search through directories and files (almost every file had the CRLF characters), ST3 still wouldn’t find them. Is there no way for ST3 to find them or replace them?

0 Likes

#4

It is likely it applies the same logic when searching directories as well for consistent search and replace behavior. I imagine it loads files into a hidden view like buffer much like a real view in order to normalize Unicode and such as Sublime doesn’t search binary files as I understand. When Sublime replaces globally in files, you will notice it opens the file and then replaces it in the view (then you can save). But we already established that Sublime doesn’t render \r in views, so I suspect global search and replace is the same as local search and replace.

I often use an external tool to do global search and replace in projects, so I don’t use Sublime’s global search and replace.

0 Likes

#5

That makes sense. Atom could see the CRLF, but couldn’t replace it properly, so I used Notepad++. I just didn’t expect to use something other than ST3 for my editing. Is this a feature that can be added?

0 Likes

#6

I hope this never changes, I for one really like how one can rest easy knowing there are only LFs and no CRLFs to worry about when working in ST

1 Like

#7

I guess the point is there is no easy way to mass convert CRLF across a project via Sublime. It isn’t against how Sublime handles CRLF in a view as much as STUser wants to fix an existing project’s mix in one shot.

At this point, an external find and replace tool, or in your case Notepad++, may be your best bet to do your one time fix. I guess a Sublime plugin could be written to comb your project and do this and the raw file as well.

0 Likes

#8

I agree with the LF only, but in my case, I had to override the ST default of “system” (Windows) to use “unix” going forward, but wanted the ability to replace all CRLF.

0 Likes

#9

That’s fair. I still wish if it could find and replace every other character combination in regex, that it ought to be able to do \r\n as well.

0 Likes

#10

Opening a file directly using the python api could be used to normalize file endings.

One would need to write a plugin, which walks through the whole directory tree of a project, opens each file replaces \r\n and writes the content back to that file.

0 Likes

#11

I wrote a plugin a long time ago that allows you to open a a file showing line endings: https://github.com/facelessuser/RawLineEdit. You can also edit them, but it does not comb your projects for them. I mainly wrote it to sometimes debug a file that isn’t supposed to have CLRF in them, or were CLRF mean something specific, but these days I rarely run into such issues.

1 Like

#12

Haven’t need something like that at all up to now. Was just a quick idea. I am happy not to need to bother with line endings at all. ST normalizes them - the rest is normalized by git. :wink:

0 Likes

#13

I wrote a small utility script that utilizes find, file and dos2unix.

0 Likes