Skip to content

Toggle autoformat feature and/or documentation #170

@rkulla

Description

@rkulla

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions