86

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.

2

4 Answers 4

184

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

git config core.autocrlf false 
git rm --cached -r . 
git reset --hard
6
  • 13
    in my opinion this is not the solution, if someone who uses unix enviroment will have the same end of line problems
    – Gaspar
    Oct 9, 2019 at 2:35
  • It helps though if you make changes and the files are cached.
    – konsalex
    Sep 6, 2022 at 14:13
  • 3
    Between lines 2 and 3 it might be necessary to write git stash. Nov 2, 2022 at 20:23
  • 3
    Should have been add warning to stash or commit your current change.
    – moanrisy
    Jun 12 at 2:19
  • 1
    This is the answer that worked for me. In my case the project was configured to use CRLF line endings but I had installed Git for Windows with autocrlf false. The first line changes line endings from now on, the second line removes existing line endings in local files and the third line recreates line endings in existing files using the new autocrlf value (and will delete all local changes). Nov 2 at 12:18
37

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.

2
  • 1
    Not sure why but it's not working for me. The other thing is "Save All" (Ctrl+k) is greyed out, I cannot use it. Sep 1, 2022 at 12:01
  • thankyou so sososoooo much for this extension. this entire proccess was one of the dumbest thing I ever did as a programmer Oct 20 at 21:05
30

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.

4
  • Thanks for this, it got me going in the right direction but I found I had to modify the format script as I was getting a usage error about not supplying any files. I'm using express with node so my working script is "format": "prettier app.js **/*.js --write",
    – DazBaldwin
    Sep 7, 2022 at 9:05
  • 3
    Noice! 😎 In my case, I had to convert all my Spring Boot project's Java files' EOL's from LF to CRLF. I had NodeJS already installed. So I installed prettier globally: npm install --global prettier and then executed Prettier in root directory: npx prettier --end-of-line crlf --write . (👈 this dot is mandatory) and that solved my issue. Jan 5 at 6:17
  • prettier --end-of-line lf --write . if you're lazy like me and want to do all project files blindly
    – ThaJay
    Apr 6 at 13:55
  • OMG do not do this. Now my branch says every file has changed and not just the files for my feature :(
    – JesseBoyd
    Jul 10 at 23:25
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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