Vim-prettier: Toggle autoformat feature and/or documentation

Created on 4 Feb 2019  路  3Comments  路  Source: prettier/vim-prettier

I currently use this bit of vimscript I wrote in my vimrc file to toggle vim-prettier autoformat on/off:

""" Toggle vim-prettier auto formatting with <Leader>pr
nnoremap <leader>pr :call TogglePrettier()<cr>

" enable autoformatting by default
let g:prettier#autoformat=0 
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html PrettierAsync

function! TogglePrettier()
    if g:prettier#autoformat
        let g:prettier#autoformat=0
        autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html PrettierAsync
    else
        let g:prettier#autoformat=1
        autocmd! BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html call clearmatches()
    endif
endfunction

because I like to keep autoformat on by default but once in a while I like to turn it off temporarily, e.g., if i don't want a json string to be multiple lines, etc. This allows you to still type <Leader>p, by making a new map called <Leader>pr but i don't care if you choose another mapping or way of doing this all together.

I can see it being difficult to get all the variations of autoformating and different file extensions working so maybe just a documentation of how to do it would be enough. Love the plugin, thanks!

enhancement

Most helpful comment

I believe this functionality should be part of release/1.x as it make sense to be able to disable autoformat and enable it.

All 3 comments

I believe this functionality should be part of release/1.x as it make sense to be able to disable autoformat and enable it.

For a quick one-off bypassing of Prettier you could do :noautocmd w to save the buffer.

release/1.x is already merged to master

Was this page helpful?
0 / 5 - 0 ratings

Related issues

n-xlkt picture n-xlkt  路  5Comments

larister picture larister  路  4Comments

blacksails picture blacksails  路  5Comments

jrock2004 picture jrock2004  路  5Comments

dedoyle picture dedoyle  路  11Comments