Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Vim plugin for Prettier

License

NotificationsYou must be signed in to change notification settings

prettier/vim-prettier

Repository files navigation

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


NOTE: If you want to fallback to older version of prettier/vim-prettier please add this to your.vimrc:

Plug'prettier/vim-prettier', {\'do':'yarn install --frozen-lockfile --production',\'branch':'release/0.x'\}

By default it will auto formatjavascript,typescript,less,scss,css,json,graphql andmarkdown files if theyhave/support the "@format" pragma annotation in the header of the file.

vim-prettier

INSTALL

To install with vim 8+ plugins, simply clone to your~/.vim/pack/plugins/start directory, and addpackloadall to your.vimrc (if you haven't already).

mkdir -p~/.vim/pack/plugins/startgit clone https://github.com/prettier/vim-prettier~/.vim/pack/plugins/start/vim-prettier

.vimrc:

packloadall

Install withvim-plug, assumes node andyarn|npm installed globally.

" post install (yarn install | npm install) then load plugin only for editing supported filesPlug'prettier/vim-prettier', {\'do':'yarn install --frozen-lockfile --production',\'for': ['javascript','typescript','css','less','scss','json','graphql','markdown','vue','svelte','yaml','html'] }

or simply enable for all formats by:

" post install (yarn install | npm install) then load plugin only for editing supported filesPlug'prettier/vim-prettier', {'do':'yarn install --frozen-lockfile --production' }

For those usingvim-pathogen, you can run the following in a terminal:

cd ~/.vim/bundlegit clone https://github.com/prettier/vim-prettier

If usingdein, add the following to your dein config:

calldein#add('prettier/vim-prettier', {'build':'npm install'})

If using other vim plugin managers or doing manual setup make sure to haveprettier installed globally or go to your vim-prettier directory and either donpm install oryarn install --frozen-lockfile

Prettier Executable resolution

When installed via vim-plug, a default prettier executable is installed insidevim-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 insidenode_modules
  3. Look for a global prettier installation
  4. Use locally installed vim-prettier prettier executable

Prettier Stylelint

To use an alternative command, likeprettier-stylelint, setthis at the buffer level, e.g.:

auFileTypecss,scssletb:prettier_exec_cmd="prettier-stylelint"

vim-prettier will look for the executable in the same places it looks forprettier, and will fall back toprettier if it can't findb:prettier_exec_cmd

USAGE

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

<Leader>p

or

:Prettier

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

:PrettierAsync

You can send to prettier your entire buffer but ensure that it formats only your selection.

note: differs from:PrettierFragment by sending the entire buffer to prettier, allowing identation level to be preserved, but it requires the whole file to be valid.

:PrettierPartial

You can send to prettier your current selection as a fragment of same type as the file being edited.

note: differs from:PrettierPartial by sending only the current selection to prettier, this allows for faster formatting but wont preserve indentation.

:PrettierFragment

You can check what is thevim-prettier plugin version by:

:PrettierVersion

You can send commands to the resolvedprettier cli by:

:PrettierCli <q-args>

You can check what is the resolvedprettier cli path by:

:PrettierCliPath

You can check what is the resolvedprettier cli version by:

:PrettierCliVersion

Configuration

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

nmap<Leader>py<Plug>(Prettier)

Enable auto formatting of files that have "@format" or "@prettier" tag

letg:prettier#autoformat=1

Allow auto formatting for files without "@format" or "@prettier" tag

letg:prettier#autoformat_require_pragma=0

NOTE The previous two options can be used together for autoformatting files on save without@format or@prettier tags

letg:prettier#autoformat=1letg:prettier#autoformat_require_pragma=0

Toggle theg:prettier#autoformat setting based on whether a config file can be found in the current directory or any parent directory. Note that this will override theg:prettier#autoformat setting!

letg:prettier#autoformat_config_present=1

A list containing all config file names to search for when using theg:prettier#autoformat_config_present option.

letg:prettier#autoformat_config_files= [...]

Set the prettier CLI executable path

letg:prettier#exec_cmd_path="~/path/to/cli/prettier"

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

letg:prettier#exec_cmd_async=1

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

letg:prettier#quickfix_enabled=0

By default selection formatting will be running:PrettierFragment but we can set:PrettierPartial as the default selection formatting by:

letg:prettier#partial_format=1

By default we auto focus on the quickfix when there are errors but can also be disabled

letg:prettier#quickfix_auto_focus=0

To run vim-prettier not only before saving, but also after changing text or leaving insert mode:

" when running at every change you may want to disable quickfixletg:prettier#quickfix_enabled=0autocmdTextChanged,InsertLeave*.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.svelte,*.yaml,*.html PrettierAsync

Overwrite default prettier configuration

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

" Max line length that prettier will wrap on: a number or 'auto' (use" textwidth)." default: 'auto'letg:prettier#config#print_width='auto'" number of spaces per indentation level: a number or 'auto' (use" softtabstop)" default: 'auto'letg:prettier#config#tab_width='auto'" use tabs instead of spaces: true, false, or auto (use the expandtab setting)." default: 'auto'letg:prettier#config#use_tabs='auto'" flow|babylon|typescript|css|less|scss|json|graphql|markdown or empty string" (let prettier choose)." default: ''letg:prettier#config#parser=''" cli-override|file-override|prefer-file" default: 'file-override'letg:prettier#config#config_precedence='file-override'" always|never|preserve" default: 'preserve'letg:prettier#config#prose_wrap='preserve'" css|strict|ignore" default: 'css'letg:prettier#config#html_whitespace_sensitivity='css'" false|true" default: 'false'letg:prettier#config#require_pragma='false'" Define the flavor of line endings" lf|crlf|cr|all" defaut: 'lf'letg:prettier#config#end_of_line=get(g:,'prettier#config#end_of_line','lf')

REQUIREMENT(S)

If theprettier executable can't be found by Vim, no code formatting will happen


[8]ページ先頭

©2009-2025 Movatter.jp