From 24f9cc3a71febe61c20b8bfb65c7f5e676c9505b Mon Sep 17 00:00:00 2001 From: Erick Romero Date: Mon, 20 Mar 2017 23:12:10 -0600 Subject: [PATCH] Vim readme (#1058) * Moved Vim info to its own editor folder, started drafting new README * Finished first draft, ready for PR * Testing Readme layout * Removed vim folder --- README.md | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 49274323..eccbee36 100644 --- a/README.md +++ b/README.md @@ -223,11 +223,15 @@ Emacs users should see [this folder](https://github.com/jlongster/prettier/tree/master/editors/emacs) for on-demand formatting. -### Vim +### Vim + +For Vim users there are two main approaches, one that leans on [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat), which has the advantage of leaving the cursor in the same position despite changes, or a vanilla approach which can only approximate the cursor location, but might be good enough for your needs. + +#### Vanilla approach Vim users can add the following to their `.vimrc`: -``` +```vim autocmd FileType javascript set formatprg=prettier\ --stdin ``` @@ -235,7 +239,7 @@ This makes Prettier power the [`gq` command](http://vimdoc.sourceforge.net/htmld for automatic formatting without any plugins. You can also add the following to your `.vimrc` to run prettier when `.js` files are saved: -``` +```vim autocmd BufWritePre *.js :normal gggqG ``` @@ -243,10 +247,39 @@ If you want to restore cursor position after formatting, try this (although it's not guaranteed that it will be restored to the same place in the code since it may have moved): -``` +```vim autocmd BufWritePre *.js exe "normal! gggqG\\" ``` +#### Neoformat approach + +Add [sbdchd](https://github.com/sbdchd)/[neoformat](https://github.com/sbdchd/neoformat) to your list based on the tool you use: + +```vim +Plug 'sbdchd/neoformat' +``` + +Then make Neoformat run on save: + +```vim +autocmd BufWritePre *.js Neoformat +``` + +#### Customizing prettier in Vim + +If your project requires settings other than the default prettier settings you can pass arguments to do so in your `.vimrc` or [vim project](http://vim.wikia.com/wiki/Project_specific_settings), you can do so: + +```vim +autocmd FileType javascript set formatprg=prettier\ --stdin\ --parser\ flow\ --single-quote\ --trailing-comma\ es5 +``` + +Each command needs to be escaped with `\`. If you are using Neoformat and you want it to recognize your formatprg settings you can also do that by adding the following to your `.vimrc`: + +```vim +" Use formatprg when available +let g:neoformat_try_formatprg = 1 +``` + ### Visual Studio Code Can be installed using the extension sidebar. Search for `Prettier - JavaScript formatter`