Reformat JavaScript

2 Points

Romain Lafourcade Romain Lafourcade

9 years ago

This command formats the whole buffer (default), the visual selection or an arbitrary range, using js-beautify (https://www.npmjs.org/package/js-beautify).

It is generic enough to be adapted to other languages easily.

Place the snippet in ~/.vim/after/ftplugin/javascript.vim.

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