quickly beautify your javascript in emacs

This is for the javascript version of js-beautify and could easily apply to their python version as well. You’ll need to have node installed with npm and run the following:

$ sudo npm install -g js-beautify

Next, open up your emacs init file (~/.emacs.d/init.el) and add the following lisp code:

(defun jstidy ()
  'Run js-beautify on the current region or buffer.'
  (interactive)
  (save-excursion
    (unless mark-active (mark-defun))
    (shell-command-on-region (point) (mark) 'js-beautify -f -'; nil t)))
(global-set-key "\C-cg" 'jstidy)

Once you’ve re-evaluated or restarted emacs the (CTRL+c then g) will be bound to the jstidy function.

Finally, load up any javascript file and either mark a region or not and press the key commands to execute jstidy and you should see your code re-formatted nicely.

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

You may also like...