1

I have the following line of code:

  autocmd FileType javascript execute "setlocal formatprg=".g:prettier_eslint_path2."\\ --eslint-config-path" . g:eslintrc_full_path ."\\"

I can see this when I start vim in the directory which has .eslintrc:

ESLINTRC PATH
.eslintrc
Prettier ESLINT PATH
/Users/localuser/lendi/lendi-app/node_modules/.bin/prettier-eslint^@
/Users/localuser/lendi/lendi-app/node_modules/.bin/prettier-eslint
Has  ESLINTRC
/Users/localuser/lendi/lendi-app/.eslintrc
--- Auto-Commands ---
Press ENTER or type command to continue

I have the g:eslintrc_full_path logged out correctly, how do I pass it from the variable to formatprg correctly.

If I remove the args, I get an empty output as the result of the file.

If I remove the \\ it informs that it is valid to do so.

I dont understand how to pass arguments to the cli script when using a variable. I did not find any examples of string interpolation when reading through http://learnvimscriptthehardway.stevelosh.com/chapters/26.html

Also, the following code also causes a problem:

  autocmd FileType javascript execute "setlocal formatprg=".g:prettier_eslint_path2."\\ --eslint-config-path\\ ".g:eslintrc_full_path

and deletes the entire file. I have tried:

  autocmd FileType javascript execute "setlocal formatprg=".g:prettier_eslint_path2."\\ --eslint-config-path\\ ".g:eslintrc_full_path

but it deletes the contents of the file as well.

I have tried it with only --stdin like this but it fails and converts everything to double quotes:

  autocmd FileType javascript execute "setlocal formatprg=".g:prettier_eslint_path2."\\ --stdin"

I have tried adding in the single quote as an argument here but it still converts everything to double-quotes.

autocmd FileType javascript let &l:formatprg= g:prettier_eslint_path2."\\ --single-quote\\ --eslint-config-path\\ ". g:eslintrc_full_path

Also, logging &l:formatprg does not work:

autocmd FileType javascript let &l:formatprg = g:prettier_eslint_path2.' --single-quote --eslint-config-path '. g:eslintrc_full_path
echom &l:formatprg

I tried checking the messages:

and it shows:

/Users/localuser/lendi/lendi-app/node_modules/.bin/prettier-eslint --single-quote --write --eslint-config-path /Users/localuser/lendi/lendi-app/.eslintrc

which should work. How does neoformat pass the file to the cli script.

How do I solve this problem?

I have also created a issue on neoformat for the same:

https://github.com/sbdchd/neoformat/issues/112

  • 1
    please, do not open issues for basic usage questions in various repositories at the same time. – Christian Brabandt Aug 21 '17 at 12:33
  • You mention Neoformat in the title of your question, but the entire content appears to be about using Vim's builtin 'formatprg'. For help using Neoformat, I suggest reading :help neoformat.txt – Rich Jan 31 '18 at 11:28
1

Maybe this is an escaping problem, you should use:

autocmd FileType javascript setlocal formatprg=command\ --arg

Or if you need variables:

autocmd FileType javascript let &l:formatprg = 'command ' . l:arg

Update: As commented by the plugin owner, it would be easier to define a formatter via Neoformat:

let g:neoformat_javascript_eslint = {
            \ 'exe': 'eslint',
            \ 'args': [],
            \ }

let g:neoformat_enabled_javascript = ['eslint']

But if you want to keep using formatprg, you should get rid of the FileType auto command and put setlocal {option} or let &l:{option} directly in ftplugin/javascript.vim, see :help filetype-plugin.

  • I have tried autocmd FileType javascript let &l:formatprg= g:prettier_eslint_path2."\\ --single-quote\\ --eslint-config-path\\ ". g:eslintrc_full_path but it still wont work. – vamsiampolu Aug 21 '17 at 8:06
  • Try removing the backslashes, spaces are already escaped within quotes. The result of \\ is \ , not a space. – LEI Aug 21 '17 at 8:10
  • I removed the `\` and left the spaces in there and the result is that it deletes the contents of the file and saves the file. – vamsiampolu Aug 21 '17 at 8:11
  • Then it might indeed be related to the eslint command or the neoformat plugin. Does the same thing happen when you execute the formatting command without vim (from terminal)? – LEI Aug 21 '17 at 8:13
  • No, from terminal I execute ./node_modules/.bin/prettier-eslint --write ./app/routes.js --eslint-config-path ./.eslintrc and it works correctly – vamsiampolu Aug 21 '17 at 8:17
0

When using the formatprg option, Vim passes the file contents to the external program on stdin. (See :help 'formatprg')

You are not telling prettier-eslint-cli to expect to receive input in this way, and so the command fails.

The README for prettier-eslint-cli explains how to use it from Vim:

Vim users can add the following to their .vimrc:

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

(Emphasis mine)

You also want to specify the paths of the executable and rcfile, so you need to amend this to add these in, whilst continuing to pass the necessary --stdin option.

autocmd FileType javascript let &l:formatprg = g:prettier_eslint_path2.' --stdin --eslint-config-path '. g:eslintrc_full_path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.