4

I am trying to set up the Node.js version of js-beautify as an equalprg command when I am editing a JavaScript file, so that the Vim = indent commands reflow my JavaScript at the same time.

Initially, I tried this:

autocmd FileType javascript setlocal equalprg=js-beautify -f -

(the -f - is necessary because js-beautify otherwise doesn't read from stdin).

However, that fails with an error like this when I open a JavaScript file:

Error detected while processing FileType Auto commands for "javascript":
E518: Unknown option: -f

So then I tried adding surrounding quotes:

autocmd FileType javascript setlocal equalprg="js-beautify -f -"

That seems to work (JavaScript files open without error), but then 1G=G (or any other = command) seems to work in exactly the same way as the standard vim behaviour, and it appears it's not set up correctly - if I type :set equalprg, I see:

equalprg=

If I remove the -f -, the equalprg is set, but it doesn't work, because js-beautify tries to read from a file:

Must define at least one file.
Run `js-beautify -h` for help.

How can pass these command-line options into js-beautify as part of setting equalprg?

Note I am aware that I could use the js-beautify vim plugin, but I'm trying not to overwhelm my vim setup with plugins, and instead setup the standard vim = keymapping.

5

Close, but you have to use single quotes instead of double quotes, because Vim interprets " as a comment in this context:

autocmd FileType javascript setlocal equalprg='js-beautify -f -'

Or just escape the spaces:

autocmd FileType javascript setlocal equalprg=js-beautify\ -f\ -
  • That works perfectly, thank you! Is there any explanation as to why viml/vimscript expects `` here and not a quoted string instead? Seems unusual to me. – Andrew Ferrier Feb 8 '15 at 14:43
  • 2
    @AndrewFerrier It looks like Vim is interpreting " as a comment in this context, so the command you're really running is setlocal equalprg=. – Doorknob Feb 8 '15 at 14:45

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.