78

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.

Henke
  • 4,445
  • 3
  • 31
  • 44
Thong Yi Xuen
  • 1,001
  • 1
  • 9
  • 10
  • [https://stackoverflow.com/a/39532890/45966](https://stackoverflow.com/a/39532890/45966) Has a solution that worked for me. [![enter image description here](https://i.stack.imgur.com/wKrA6.png)](https://i.stack.imgur.com/wKrA6.png) – Hellonearthis Jun 25 '18 at 17:09
  • 1
    Does this answer your question? [How can I make all line endings (EOLs) in all files in Visual Studio Code, Unix-like?](https://stackoverflow.com/questions/48692741/how-can-i-make-all-line-endings-eols-in-all-files-in-visual-studio-code-unix) – Gino Mempin Nov 17 '21 at 03:07

4 Answers4

171

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

git config core.autocrlf false 
git rm --cached -r . 
git reset --hard
kris
  • 11,868
  • 9
  • 88
  • 110
Brandon Truong
  • 1,859
  • 1
  • 8
  • 3
  • 12
    in my opinion this is not the solution, if someone who uses unix enviroment will have the same end of line problems – Gaspar Oct 09 '19 at 02:35
  • It helps though if you make changes and the files are cached. – konsalex Sep 06 '22 at 14:13
  • The best explanation I've found of what the three commands do is [this answer on how to force Git to use LF instead of CR+LF](https://stackoverflow.com/a/33424884). ~ * ~ An even older reference is [this suggested solution on how to let Git deal with line endings](https://www.marten-online.com/source-versioning/git-dealing-with-line-endings-solution-2.html). – Henke Oct 26 '22 at 10:12
  • Between lines 2 and 3 it might be necessary to write `git stash`. – Eduardo Barrera Nov 02 '22 at 20:23
  • Should have been add warning to stash or commit your current change. – moanrisy Jun 12 '23 at 02:19
32

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Woody
  • 321
  • 3
  • 3
28

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.

Musilix
  • 330
  • 2
  • 14
cmlima
  • 381
  • 3
  • 3
  • 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 07 '22 at 09:05
  • 2
    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. – harshrathod50 Jan 05 '23 at 06:17
  • `prettier --end-of-line lf --write .` if you're lazy like me and want to do all project files blindly – ThaJay Apr 06 '23 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 '23 at 23:25
2

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.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31