How does vim use prettier-eslint to format the code according to eslint configuration

  question, vim

I want to format the code with prettier on vim, but I found that the vim-prettier plug-in cannot format the code according to the eslintrc of different projects, so I want to format it directly through vim.

according toprettier-eslint-cliAccording to the document, it seems that it can be used in this way.

Vim users can add the following to their .vimrc:

autocmd FileType javascript set formatprg=prettier-eslint\ --stdin

This makes prettier-eslint-cli power the gq command for automatic formatting without any plugins. You can also add the following to your .vimrc to run prettier-eslint-cli when .js files are saved:

autocmd BufWritePre *.js :normal gggqG

I don’t want to format the file when saving it, so I haven’t tried the second one. I used the first method and found that I had to select the code before entering the command.gqOnly the selected file is formatted, so there is a problem-it is not a specific file that is passed to prettier-eslint-cli. This will cause prettier-eslint-cli to be unable to index the eslitrc configuration file according to the file path, thus failing to meet the requirement to format the code according to the eslitrc configuration file.

I can post the debugging information after vim is running.

clipboard.png

You can see from the first line in the figure that Gettingeslintconfig for fileat’ undefined’, and if you enter the command line directly in the console to format the fileprettier-eslint index.js -l debug, it is not undefined

clipboard.png

Therefore, who is the great god of vim, according to the functions that can be implemented for me, is there any way to let prettier-eslint know the information of the current file, so that it can find eslintrc configuration?

I found that it can be realized in the following ways

:silent !  prettier-eslint % --write

It is always annoying to pop up a console window in this way. Can you run the command directly in the background without popping up the console window?

clipboard.png