Luke Murray

Make Prettier Ignore Ranges

March 24, 2021

I used prettier-vscode to keep my code consistently formatted. I also have VSCode automatically format my code on save. This is super nice because when I make changes I don't need to worry about creating weird diffs due to small formatting changes. I simply save and get something which looks good enough.

"editor.formatOnSave": true,

Problem

Sometimes I have code which I don't want prettier to change. For example in my post Fuzzy search Json Files I have some exmaple json which is purposefully minified. Prettier tries to be super helpful and unminmize the json for me whenever I save. Sometimes I work around this issue by executing the vscode command Save without Formatting (cmd + k s) but that can get tedious if you're working in a file that is being changed quite a bit.

Solution

Prettier supports disabling formatting for ranges using html comments!

<!-- prettier-ignore-start -->
```json
{"my": "minimized", "json": "object"}
```
<!-- prettier-ignore-end -->