My research hints that there is no way to have a JS file properly formatted without going through installations.

I wonder whether there is a minimal effort way of doing it in vim. What are your opinions on that one? What do you recommend? Can't this be achieved by an appropriate configuration of a .vimrc file?

1 Answer 1


I recommend filtering your buffer/file through a node module called js-beautify (also available as a python module):

:%!js-beautify -f -        " whole buffer
:'<,'>!js-beautify -f -    " selected lines

Here is the custom command I have in after/ftplugin/javascript.vim:

command! -buffer -range=% Format let b:winview = winsaveview() |
    \ execute <line1> . "," . <line2> . "!js-beautify -f - -j -t -s " . &shiftwidth |
    \ call winrestview(b:winview)

That command works on the whole buffer by default or on any range, while keeping the cursor position and, more or less, respecting indentation settings.

Read $ js-beautify --help for the available options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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