For *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write
Answer from Tiago Bértolo on Stack OverflowVideos
For *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write
To exclude files from formatting, create a .prettierignore file at the root of your project.
And to format only the *.ts files you should ignore everything but the *.ts files.
Example
# Ignore everything recursively
*
# But not the .ts files
!*.ts
# Check subdirectories too
!*/
In the code above, the * means to ignore everything including the subfolders, and the next line !*.ts tells the prettier to reverse the previous ignoring of the .ts files. The last line !*/ means to check the subdirectories too, but with the previous rule, it's only looking for the .ts files.
Check the prettier and gitignore docs for more information.
» npm install @prettier/cli
» npm install prettier-check
I've been trying to use as neovim as vanilla possible to expand my horizon and one of the coolest and unknown features out here must be the filter (:!) command. Basically, you can (optionally) redirect part of your buffer as stdin to a shell command which writes it back into your buffer!
As an example, this one way how you could format your code using prettier:
:%!npx prettier --stdin-filepath %
Explanation:
The
%before the!is the range, ie, which part of your buffer do you want to redirect to the command?%means everything, but you could also say for example:.!npx prettier ...where the.would be just the current line, or make a visual selection and write:'<,'>!npx prettier ....The
!is called filter by the vim docs, it calls a shell commandNext comes the command itself, the first part is pretty self explanatory. I don't have prettier installed globally so I use
npx. You have to pass the--stdin-filepathoption, otherwise it won't read your buffer, but the file content (which can differ from your buffer if, for example, you haven't saved your buffer yet). The second%means, in the context of the shell command, "the current file name". (And another cool trick: you can expand it using <c-a>)
That's it, you can use this with any shell command! The beauty of the unix philosophy...
Of course you could also map this to some keybinding.
If you know other cool and hidden tricks of vim, please share them in the comments!
» npm install prettier