Thong Yi Xuen
Thong Yi Xuen

Reputation: 1281

How to change the EOL for all files from CRLF to LF

In Visual Studio Code, I have changed the default EOL (end-of-line token) from CRLF to LF, but this only applies to new files. I would like to know how to change the EOL for all files at once as I have more than a hundred files and it will be hard to do it manually.

Upvotes: 106

Views: 148462

Answers (7)

Priyanka
Priyanka

Reputation: 581

Assuming that your files are under version control with git, my recommendation would be to add a .gitattributes file to your repo, and explicitly list file formats that are text, like this:

*.csv  text
*.json text
*.py   text
*.txt  text

If you want to let git autodetect text files, add this line:

* text=auto

On the contrary, if you want to turn off any autodetection, add this line:

* -text

Then, once that .gitattributes file is committed, run this git command:

git add --renormalize .

Then, review and commit all changed files.

Above procedure is also described here:

Upvotes: 0

GyeongHoKim
GyeongHoKim

Reputation: 21

For anyone still looking,

I made a NPM package in github registry for changing All Files in the project

https://github.com/GyeongHoKim/lfify

you can follow the instructions or just copy and paste index.cjs in somewhere in your file and just node pasted-file.cjs

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 165

For anyone still looking, it is pretty simple if you just want it for a specific file. Saw it in this blog entry

There is a setting at the bottom bar of VSCode towards the right side which says "CRLF" or "LF", you can switch the line ending there.

Upvotes: 5

cmlima
cmlima

Reputation: 539

If you have a Node.js development environment and prettier installed, one way of replacing all CRLF for LF is by running prettier --end-of-line lf --write . in the command line. Where the dot represents the entirety of your current working directory.

Another way is to set the endOfLine option to lf in the .prettierrc configuration file and place a script in your package.json like so:

...
  "scripts": {
    ...
    "format": "prettier --write ."
    ...
  }
...

Then, you just need to execute npm run format in your terminal and all files targeted by prettier in your project will be automatically changed and saved.

Upvotes: 43

Isaac Hurtado
Isaac Hurtado

Reputation: 51

If you have PowerShell or git bash or another bash-type terminal you could use:

$ for i in *js; do vi -c "set fileformat=unix | wq" "${i}"; done

As mentioned in https://unix.stackexchange.com/questions/22604/how-to-bulk-convert-all-the-file-in-a-file-system-branch-between-unix-and-window/626954#626954.

Upvotes: 5

Woody
Woody

Reputation: 471

To solve the problem in my project I used a Visual Studio Code extension called "Change All End Of Line Sequence", follow the process of the extension and then save all your files.

And that's it, hope it helps somebody still looking for a quick fix.

Upvotes: 47

Brandon Truong
Brandon Truong

Reputation: 2297

Run these. It works for me. Customize it with your requirements

git config core.autocrlf false 
git rm --cached -r . 
git reset --hard

Upvotes: 214

Related Questions