sponsor Vim development Vim logo Vim Book Ad

vim-prettier : A vim plugin wrapper for prettier

 script karma  Rating 404/128, Downloaded by 9801  Comments, bugs, improvements  Vim wiki

created by
mtermayer reis
 
script type
utility
 
description
## vim-prettier

A vim plugin wrapper for prettier, pre-configured with custom default prettier
settings.

---

By default it will auto format **javascript**, **typescript**, **less**,
**scss**, **css**, **json**, **graphql** and **markdown** files if they
have/support the "@format" pragma annotation in the header of the file.

### Prettier Executable resolution

When installed via vim-plug, a default prettier executable is installed inside
vim-prettier.

vim-prettier executable resolution:

1. Look for user defined prettier cli path from vim configuration file
2. Traverse parents and search for Prettier installation inside `node_modules`
3. Look for a global prettier installation
4. Use locally installed vim-prettier prettier executable

### USAGE

Prettier by default will run on auto save but can also be manually triggered by:

```vim
<Leader>p
```

or

```vim
:Prettier
```

If your are on vim 8+ you can also trigger async formatting by:

```vim
:PrettierAsync
```

You can check what is the `vim-prettier` plugin version by:

```vim
:PrettierVersion
```

You can send commands to the resolved `prettier` cli by:

```
:PrettierCli <q-args>
```

You can check what is the resolved `prettier` cli path by:

```vim
:PrettierCliPath
```

You can check what is the resolved `prettier` cli version by:

```vim
:PrettierCliVersion
```

### Configuration

Change the mapping to run from the default of `<Leader>p`

```vim
nmap <Leader>py <Plug>(Prettier)
```

Disable auto formatting of files that have "@format" tag

```vim
let g:prettier#autoformat = 0
```

Set the prettier CLI executable path

```vim
let g:prettier#exec_cmd_path = "~/path/to/cli/prettier"
```

The command `:Prettier` by default is synchronous but can also be forced async

```vim
let g:prettier#exec_cmd_async = 1
```

By default parsing errors will open the quickfix but can also be disabled

```vim
let g:prettier#quickfix_enabled = 0
```

To enable vim-prettier to run in files without requiring the "@format" doc tag.
First disable the default autoformat, then update to your own custom behaviour

Running before saving sync:

```vim
let g:prettier#autoformat = 0
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md Prettier
```

Running before saving async (vim 8+):

```vim
let g:prettier#autoformat = 0
autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md 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,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md PrettierAsync
```

### Overwrite default prettier configuration

**Note:** vim-prettier default settings differ from prettier intentionally.
However they can be configured by:

```vim
" max line length that prettier will wrap on
let g:prettier#config#print_width = 80

" number of spaces per indentation level
let g:prettier#config#tab_width = 2

" use tabs over spaces
let g:prettier#config#use_tabs = 'false'

" print semicolons
let g:prettier#config#semi = 'true'

" single quotes over double quotes
let g:prettier#config#single_quote = 'true'

" print spaces between brackets
let g:prettier#config#bracket_spacing = 'false'

" put > on the last line instead of new line
let g:prettier#config#jsx_bracket_same_line = 'true'

" none|es5|all
let g:prettier#config#trailing_comma = 'all'

" flow|babylon|typescript|css|less|scss|json|graphql|markdown
let g:prettier#config#parser = 'flow'

" cli-override|file-override|prefer-file
let g:prettier#config#config_precedence = 'prefer-file'
```

### REQUIREMENT(S)

If prettier installation can't be found no code formatting will happen
 
install details
### INSTALL

Install with [vim-plug](https://github.com/junegunn/vim-plug), assumes node and
yarn|npm installed globally.

```vim
" post install (yarn install | npm install) then load plugin only for editing supported files
Plug 'prettier/vim-prettier', {
  \ 'do': 'yarn install',
  \ 'for': ['javascript', 'typescript', 'css', 'less', 'scss', 'json', 'graphql', 'markdown'] }
```

or simply enable for all formats by:

```vim
" post install (yarn install | npm install) then load plugin only for editing supported files
Plug 'prettier/vim-prettier', { 'do': 'yarn install' }
```

If using other vim plugin managers or doing manual setup make sure to have
`prettier` installed globally or go to your vim-prettier directory and either do
`npm install` or `yarn install`
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
vim-prettier-0.2.6.zip 0.2.6 2017-12-08 7.4 mtermayer reis feature - adding support for prettier 1.9.1
vim-prettier-0.2.5.zip 0.2.5 2017-11-08 7.4 mtermayer reis feature: Adding support for prettier 1.8
feature: Adding support for formatting markdown files
vim-prettier-0.2.4.zip 0.2.4 2017-10-20 7.4 mtermayer reis bugfix: fixing bug where vim-prettier would incorrectly resolve the prettier configuration file
vim-prettier-0.2.3.zip 0.2.3 2017-10-08 7.4 mtermayer reis feature: adding support for vim 8.0-0015+
vim-prettier-0.2.2.zip 0.2.2 2017-10-07 7.4 mtermayer reis feature: supporting formatting hidden buffers that already had scheduled jobs running
vim-prettier-0.2.1.zip 0.2.1 2017-10-07 7.4 mtermayer reis bugfix: fixing race conditions
vim-prettier-0.2.0.zip 0.2.0 2017-10-04 7.4 mtermayer reis bugfix: Non javascript parsers were broken due to parser flag been ignored by prettier when using configPrecedence, this make sure all non javascript parsers can leverage from custom config
vim-prettier-0.1.3.zip 0.1.3 2017-09-30 7.4 mtermayer reis bugfix: Non javascript parsers were broken due to parser flag been ignored by prettier when using configPrecedence
vim-prettier-0.1.2.zip 0.1.2 2017-09-26 7.4 mtermayer reis feature: Adding support to overwrite prettier cli path via vim configuration
vim-prettier-0.1.1.zip 0.1.1 2017-09-18 7.4 mtermayer reis feature: Adding new commands

- `:PrettierVersion` - prints the `vim-prettier` plugin version
- `:PrettierCli <q-args>` - send commands to the resolved `prettier` cli
- `:PrettierCliPath` - check what is the resolved `prettier` cli path
- `:PrettierCliVersion` - check what is the resolved `prettier` cli version

vim-prettier-0.1.0.zip 0.1.0 2017-09-17 7.4 mtermayer reis Breaking Change: requires prettier 1.7+
feature: adding support for prettier 1.7
feature: adds support for prettier custom configuration '.prettierrc' - refer to prettier website
vim-prettier-0.0.15.zip 0.0.15 2017-08-30 7.4 mtermayer reis bugfix: after formatting making sure cursor jump history does not take cursor to first line of file
vim-prettier-0.0.14.zip 0.0.14 2017-08-24 7.4 mtermayer reis bugfix: fixing support for :PrettierAsync for windows users
vim-prettier-0.0.13.zip 0.0.13 2017-08-13 7.4 mtermayer reis bugfix: making sure quickfix errors are only cleared if opened by vim-prettier itself
vim-prettier-0.0.12.zip 0.0.12 2017-08-06 7.4 mtermayer reis feature: Bumping internal prettier version to 1.5.3
vim-prettier-0.0.11.zip 0.0.11 2017-07-12 7.4 mtermayer reis feature: Adding support for vim 7.4
vim-prettier-0.0.10.zip 0.0.10 2017-07-02 8.0 mtermayer reis bugfix: fixing bug where json parser could create invalid json by adding extra comma to the end
vim-prettier-0.0.9.zip 0.0.9 2017-07-01 8.0 mtermayer reis feature: file type detection support
vim-prettier-0.0.8.zip 0.0.8 2017-07-01 8.0 mtermayer reis feature: Adding support for JSON and GraphQL formatting
vim-prettier-0.0.7.zip 0.0.7 2017-06-20 8.0 mtermayer reis feature: enabling partial buffer conversion when using visual selection
vim-prettier-0.0.6.zip 0.0.6 2017-06-07 8.0 mtermayer reis feature: Adding configuration toggle for controlling error quick fix parsing
feature: Adding PrettierAsync command
bugfix: Fixing configuration bug where boolean values where not correctly  parsed
bugfix: Making sure that async jobs do not execute if already in progress
vim-prettier-0.0.5.zip 0.0.5 2017-06-06 8.0 mtermayer reis feature: Bumping support to latest prettier ^1.4.X
feature:  Support for formatting css, scss, less, typescript
feature:  Allowing user configuration setting to enable async as the default for the `:Prettier` command
vim-prettier-0.0.4.zip 0.0.4 2017-05-31 8.0 mtermayer reis feature: Adding support for error parsing on quickfix
vim-prettier-0.0.3.zip 0.0.3 2017-05-28 8.0 mtermayer reis feature: Adding support for Async formatting  and making it default for auto save
bugfix: Fixing params sent to prettier CLI
bugfix: Fix bug where default prettier plugin installation path was never used
vim-prettier-0.0.2.zip 0.0.2 2017-05-26 8.0 mtermayer reis bugfix: Should not overwrite buffer when prettier fails to parse file
vim-prettier-0.0.1.zip 0.0.1 2017-05-25 8.0 mtermayer reis Initial upload
ip used for rating: 44.201.59.20

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github