0

currently I'm working a lot with json files. They tend to be a little bigger. I'm a little fed up with always marking the whole file and then typing M-x json-reformat-region.

Is there a way to do this directly on opening the file?

Regards

1 Answer 1

0

I use this hook to achieve an effect like that:

;; pretty print json files when they're opened (add-to-list 'auto-mode-alist '("\\.json\\'" . (lambda () (javascript-mode) (json-pretty-print (point-min) (point-max)) (goto-char (point-min)) (set-buffer-modified-p nil))))

5
  • Hi I tried it but is not working. I even replaced json-pretty-print by json-reformat-region
    – daeda
    Jul 14, 2017 at 19:50
  • Are you getting any errors?
    – user12563
    Jul 14, 2017 at 23:23
  • I don't get any errors, at least no obvious ones that I can see i the minibuffer. Is this for a different mode? I have json-mode and json-snatcher installed. I open the file and nothings happens...
    – daeda
    Jul 16, 2017 at 14:49
  • In that case you might want to add a hook to json-mode that pretty prints the buffer.
    – user12563
    Jul 16, 2017 at 15:19
  • So I finally was able to do this. The problem was that I didn't know to which mode the command json-reformat-region belongs. So here is my adapted version. ;; pretty print json files when they're opened (add-to-list 'auto-mode-alist '("\\.json\\'" . (lambda () (json-mode) (json-reformat-region (point-min) (point-max)) (goto-char (point-min)) (set-buffer-modified-p nil))))
    – daeda
    Jul 18, 2017 at 20:43

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.