Emacs: Format JavaScript Code 📜
put this in your Emacs Init File:
(defun xah-js-format-buffer () "Format JavaScript/TypeScript code of current buffer. Buffer is saved first. This command requires external command deno. URL `http://xahlee.info/emacs/emacs/emacs_format_js_code.html' Created: 2020-09-23 Version: 2025-07-02" (interactive) (when (not buffer-file-name) (user-error "buffer %s is not a file." (buffer-name))) (when (buffer-modified-p) (save-buffer)) (let ((xbackupName (concat buffer-file-name "." (format-time-string "%Y%m%d%H%M%S") "~"))) (copy-file buffer-file-name xbackupName t) (message (concat "\nBackup saved at: " xbackupName))) (let ((xpos (point)) (xoutbuf (get-buffer-create "*xah-js-format output*")) xexitstatus) (setq xexitstatus (call-process "deno" nil xoutbuf nil "fmt" buffer-file-name "--quiet")) (if (eq 0 xexitstatus) (progn (revert-buffer t t t) (goto-char xpos)) (progn (display-buffer xoutbuf) (error "error xah-html-format-buffer")))))