Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leavesofgrass/23cf0f61e0092e36dbbaa3f33e4dd060 to your computer and use it in GitHub Desktop.
Save leavesofgrass/23cf0f61e0092e36dbbaa3f33e4dd060 to your computer and use it in GitHub Desktop.
;; pretty print json files when they're opened and minify them when they are saved
(add-to-list 'auto-mode-alist
'("\\.json\\'" . (lambda ()
(json-mode)
(json-pretty-print (point-min) (point-max))
(goto-char (point-min))
(set-buffer-modified-p nil))))
(defun minify-json-buffer-contents()
"Minifies the buffer contents by removing whitespaces."
(interactive)
(delete-whitespace-rectangle (point-min) (point-max))
(mark-whole-buffer)
(goto-char (point-min))
(while (search-forward "\n" nil t) (replace-match "" nil t)))
(add-hook 'json-mode-hook
(lambda ()
(add-hook 'before-save-hook 'minify-json-buffer-contents)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment