Skip to main content

Vim 安装程序

Vim 用户可以安装 Prettier 特定的 vim-prettier,或者 NeoformatALE,它们是支持 Prettier 的通用 lint/格式引擎。

¥Vim users can install either vim-prettier, which is Prettier specific, or Neoformat or ALE which are generalized lint/format engines with support for Prettier.

vim-prettier

有关安装和使用说明,请参阅 vim-prettier 自述文件。

¥See the vim-prettier readme for installation and usage instructions.

Neoformat

安装 Neoformat 的最佳方法是使用你最喜欢的 Vim 插件管理器,例如 vim-plug

¥The best way to install Neoformat is with your favorite plugin manager for Vim, such as vim-plug:

Plug 'sbdchd/neoformat'

为了让 Neoformat 使用项目本地版本的 Prettier(即使用 node_modules/.bin/prettier 而不是在 $PATH 上寻找 prettier),你必须设置 neoformat_try_node_exe 选项:

¥In order for Neoformat to use a project-local version of Prettier (i.e. to use node_modules/.bin/prettier instead of looking for prettier on $PATH), you must set the neoformat_try_node_exe option:

let g:neoformat_try_node_exe = 1

在支持的文件中运行 :Neoformat:Neoformat prettier 以运行 Prettier。

¥Run :Neoformat or :Neoformat prettier in a supported file to run Prettier.

让 Neoformat 在保存时运行 Prettier:

¥To have Neoformat run Prettier on save:

autocmd BufWritePre *.js Neoformat

你也可以通过为其他事件设置 autocmd 来让 Vim 更频繁地格式化你的代码。这里有几个有用的:

¥You can also make Vim format your code more frequently, by setting an autocmd for other events. Here are a couple of useful ones:

  • TextChanged:在普通模式下对文本进行更改后

    ¥TextChanged: after a change was made to the text in Normal mode

  • InsertLeave:离开插入模式时

    ¥InsertLeave: when leaving Insert mode

例如,你可以将上述两个事件与 BufWritePre 一起格式化,如下所示:

¥For example, you can format on both of the above events together with BufWritePre like this:

autocmd BufWritePre,TextChanged,InsertLeave *.js Neoformat

有关详细信息,请参阅 Vim 中的 :help autocmd-events

¥See :help autocmd-events in Vim for details.

建议使用 配置文件,但你也可以在 .vimrc 中添加选项:

¥It’s recommended to use a config file, but you can also add options in your .vimrc:

autocmd FileType javascript setlocal formatprg=prettier\ --single-quote\ --trailing-comma\ es5
" Use formatprg when available
let g:neoformat_try_formatprg = 1

Prettier 选项中的每个空格都应使用 \ 进行转义。

¥Each space in Prettier options should be escaped with \.

ALE

ALE 需要 Vim 8 或 Neovim,因为 ALE 使用 Vim 8 和 Neovim 提供的异步功能。

¥ALE requires either Vim 8 or Neovim as ALE makes use of the asynchronous abilities that both Vim 8 and Neovim provide.

安装 ALE 的最佳方法是使用你最喜欢的 Vim 插件管理器,例如 vim-plug

¥The best way to install ALE is with your favorite plugin manager for Vim, such as vim-plug:

Plug 'dense-analysis/ale'

你可以在 ALE 存储库 上找到更多说明。

¥You can find further instructions on the ALE repository.

在寻找全局安装之前,ALE 将尝试使用本地安装的 Prettier。

¥ALE will try to use Prettier installed locally before looking for a global installation.

为你使用的语言启用 Prettier 修复程序:

¥Enable the Prettier fixer for the languages you use:

let g:ale_fixers = {
\ 'javascript': ['prettier'],
\ 'css': ['prettier'],
\}

ALE 同时支持 linters 和 fixers。如果你不指定要运行的 linter,则将运行所有受支持语言的所有可用工具,并且你可能会得到一个带有一堆 lint 错误的格式正确的文件。要禁用此行为,你可以告诉 ALE 仅运行你明确配置的 linters(更多信息在 常见问题 中):

¥ALE supports both linters and fixers. If you don’t specify which linters to run, all available tools for all supported languages will be run, and you might get a correctly formatted file with a bunch of lint errors. To disable this behavior you can tell ALE to run only linters you've explicitly configured (more info in the FAQ):

let g:ale_linters_explicit = 1

然后,你可以在 JavaScript 或 CSS 文件中运行 :ALEFix 以运行 Prettier。

¥You can then run :ALEFix in a JavaScript or CSS file to run Prettier.

要让 ALE 在保存时运行 Prettier:

¥To have ALE run Prettier on save:

let g:ale_fix_on_save = 1

建议使用 配置文件,但你也可以在 .vimrc 中添加选项:

¥It’s recommended to use a config file, but you can also add options in your .vimrc:

let g:ale_javascript_prettier_options = '--single-quote --trailing-comma all'

coc-prettier

coc.nvim 的 Prettier 扩展需要 neovim 或 vim8.1。用你喜欢的插件管理器安装 coc.nvim,比如 vim-plug

¥Prettier extension for coc.nvim which requires neovim or vim8.1. Install coc.nvim with your favorite plugin manager, such as vim-plug:

Plug 'neoclide/coc.nvim', {'branch': 'release'}

并通过命令安装 coc-prettier:

¥And install coc-prettier by command:

CocInstall coc-prettier

init.vim.vimrc 中设置 Prettier 命令

¥Setup Prettier command in your init.vim or .vimrc

command! -nargs=0 Prettier :call CocAction('runCommand', 'prettier.formatFile')

更新你的 coc-settings.json 以获取你希望在保存时设置格式的语言。

¥Update your coc-settings.json for languages that you want format on save.

{
"coc.preferences.formatOnSaveFiletypes": ["css", "markdown"]
}

coc-prettierprettier-vscode 的配置相同,:CocConfig 打开 coc-settings.json 以获得自动补全支持。

¥coc-prettier have same configurations of prettier-vscode, open coc-settings.json by :CocConfig to get autocompletion support.

手动运行

¥Running manually

如果你想要一些真正简单的东西,你可以创建一个自定义键绑定。在此示例中,gp(助记符:"变得漂亮")用于在当前活动缓冲区中运行 Prettier(带有选项):

¥If you want something really bare-bones, you can create a custom key binding. In this example, gp (mnemonic: "get pretty") is used to run prettier (with options) in the currently active buffer:

nnoremap gp :silent %!prettier --stdin-filepath %<CR>

请注意,如果你的代码中存在语法错误,整个缓冲区将被替换为一条错误消息。你需要按 u 才能取回密码。

¥Note that if there’s a syntax error in your code, the whole buffer will be replaced with an error message. You’ll need to press u to get your code back.

这种方法的另一个缺点是不会保留光标位置。

¥Another disadvantage of this approach is that the cursor position won’t be preserved.