Vim-prettier: Autoformat "all" files by default.

Created on 14 Jul 2017  路  7Comments  路  Source: prettier/vim-prettier

Hi guys, first of all thank you for your time and effort, I really appreciate it :)

Is it possible to auto format files, on save, without the need to add the "@format" tag?

Most helpful comment

Hi @tugorez, thank you for your message. You can achieve your desired behavior by adding to your .vimrc one of the bellow strategies:


Running before saving sync:

let g:prettier#autoformat = 0
autocmd BufWritePre *.js,*.json,*.css,*.scss,*.less,*.graphql Prettier

Running before saving async (vim 8+):

let g:prettier#autoformat = 0
autocmd BufWritePre *.js,*.json,*.css,*.scss,*.less,*.graphql PrettierAsync

Running before saving, changing text or leaving insert mode:

```vim
" when running at every change you may want to disable quickfix
let g:prettier#quickfix_enabled = 0

let g:prettier#autoformat = 0
autocmd BufWritePre,TextChanged,InsertLeave .js,.json,.css,.scss,.less,.graphql PrettierAsync

All 7 comments

Hi @tugorez, thank you for your message. You can achieve your desired behavior by adding to your .vimrc one of the bellow strategies:


Running before saving sync:

let g:prettier#autoformat = 0
autocmd BufWritePre *.js,*.json,*.css,*.scss,*.less,*.graphql Prettier

Running before saving async (vim 8+):

let g:prettier#autoformat = 0
autocmd BufWritePre *.js,*.json,*.css,*.scss,*.less,*.graphql PrettierAsync

Running before saving, changing text or leaving insert mode:

```vim
" when running at every change you may want to disable quickfix
let g:prettier#quickfix_enabled = 0

let g:prettier#autoformat = 0
autocmd BufWritePre,TextChanged,InsertLeave .js,.json,.css,.scss,.less,.graphql PrettierAsync

Will close this issue, but feel free to comment on it if you have any further questions around it.

@mitermayer Thank you very much

This info should be documented. The doc says its default format on auto save, but only when I declared this config explicitly on my .vimrc it has worked.

@wellyal it will only autoformat if the pragma format exists on the file example:

/**
 * @format
 */
const b    = 2;

That will auto format upon saving, however if you try on a file with:

const b    = 2;

it won't auto format, notice its missing the pragma format


@wellyal does that clarifies your question ? I believe the above is described on the README, if you think its not clear enough feel free to submit a Pull Request with a more descriptive information and I will happily merge it.

Is it possible to implement a strategy similar to other editors' Prettier plugins, where formatting is done automatically without pragma if

  • The closest package.json defines prettier as a dependency and/or
  • There is a .prettierrc file in one of the parent directories

For me, that would remove the need for a pragma completely.

Hi, any idea why it's not working on .ts and .tsx files?

let g:prettier#autoformat = 0
autocmd BufWritePre *.ts,*.tsx,*.jsx,*.js,*.json,*.css,*.scss,*.less,*.graphql PrettierAsync

It can format Typescript file when I press <Leader>p but it doesn't want to auto format on save (only for .ts and .tsx, the others work well).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

n-xlkt picture n-xlkt  路  5Comments

larister picture larister  路  4Comments

blacksails picture blacksails  路  5Comments

hitochan777 picture hitochan777  路  14Comments

alex-shamshurin picture alex-shamshurin  路  10Comments