Emacs: Run Current File 🚀

By Xah Lee. Date: . Last updated: .

Here's a emacs command that execute the current file. (the content can be JavaScript code, or python, ruby, bash, golang, etc.)

put this in your Emacs Init File:

(defvar xah-run-current-file-dispatch nil
"A dispatch table used by `xah-run-current-file' to call a dedicated elisp function to do the job, if any.
Value is a association list.
Each item is (EXT . FUNCTION).
EXT is file suffix (without the dot prefix) (type string),
FUNCTION is a elisp function name to call (type symbol).
If file extension is found, call the associated function pass current buffer's filepath as arg, else `xah-run-current-file' continues.
You can customize this variable." )


   ("el" . load)
   ("elc" . load)
   ("go" . xah-go-run-current-file)
   ("m" . xah-wolfram-run-script)
   ("wl" . xah-wolfram-run-script)
   ("wls" . xah-wolfram-run-script)


(defvar xah-run-current-file-map
  "A association list that maps file extension to program name, used by `xah-run-current-file'.
Each item is (EXT . PROGRAM), both strings.
EXT is file suffix (without the dot prefix), PROGRAM is program name or path, with possibly command options.
You can customize this alist.")

 '(("clj" . "clj")
   ("fs" . "dotnet fsi")
   ("fsx" . "dotnet fsi")
   ("go" . "go run")
   ("hs" . "runhaskell")
   ("js" . "deno run")
   ("latex" . "pdflatex")
   ("m" . "wolframscript -file")
   ("mjs" . "node --experimental-modules ")
   ("ml" . "ocaml")
   ("php" . "php")
   ("pl" . "perl")
   ("ps1" . "pwsh")
   ("py" . "python")
   ("py2" . "python2")
   ("py3" . "python3")
   ("rb" . "ruby")
   ("rkt" . "racket")
   ("sh" . "bash")
   ("tex" . "pdflatex")
   ("ts" . "deno run")
   ("tsx" . "tsc")
   ("vbs" . "cscript")
   ("wl" . "wolframscript -file")
   ("wls" . "wolframscript -file")
   ("pov" . "povray +R2 +A0.1 +J1.2 +Am2 +Q9 +H480 +W640")))

(defun xah-run-current-file (Filename)
  "Execute the current file.
Output is printed to buffer *xah-run output*.

File suffix is used to determine what external command to run, in the variable `xah-run-current-file-map'.

If the file is modified or not saved, save it automatically before run.

The variable `xah-run-current-file-dispatch' allows you to customize this command to call other function to run the current file.

URL `http://xahlee.info/emacs/emacs/elisp_run_current_file.html'
Version: 2020-09-24 2024-03-17 2024-03-21"
   (if buffer-file-name
         (when (buffer-modified-p) (save-buffer))
         (list buffer-file-name))
     (user-error "Buffer is not file. Save it first.")))
  ;; (setenv "NO_COLOR" "1") ; 2022-09-10 for deno. default color has yellow parts, hard to see
  (let ((xoutBuffer (get-buffer-create "*xah-run output*" t))
        (xext (file-name-extension Filename))
    (setq xdispatch (assoc xext xah-run-current-file-dispatch))
    (if (and xdispatch (fboundp (cdr xdispatch)))
          (message "calling %s" (cdr xdispatch))
          (funcall (cdr xdispatch) Filename))
      (let ((xappCmdStr (cdr (assoc xext xah-run-current-file-map))))
        (when (and xdispatch (not (fboundp (cdr xdispatch))))
          (warn "`xah-run-current-file' found function %s in xah-run-current-file-dispatch but it is unbound. Normal run continues using `xah-run-current-file-map'." xdispatch))
        (when (not xappCmdStr) (error "%s: Unknown file extension: %s. check `xah-run-current-file-map'" real-this-command xext))
        (let ((xcmdStr (format "%s %s &" xappCmdStr (shell-quote-argument Filename))))
          (message "Running 「%s」" xcmdStr)
          ;; note, not using make-process or start-process. problem is, they are too complex to create, and have issues or variation on different operating system.
          (shell-command xcmdStr xoutBuffer)))))
  ;; (setenv "NO_COLOR")

Get Latest Version

Emacs, Run Current File