Emacs Lisp: Run Current File

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows a emacs lisp command that execute the script you are writing of current buffer.


You are coding ruby. You want to be able to press a button, and have the current file executed or compiled. The file may be a Perl, Python, Ruby, PHP, Java, Clojure, golang, TeX/LaTeX, Bash, and more.

This way is even better than using a interactive command line (aka REPL), because you can write and edit normally with full emacs power. And you can run it by just pressing a key, such as F8.


(defun xah-run-current-file ()
  "Execute the current file.
For example, if the current buffer is the file x.py, then it'll call 「python x.py」 in a shell.
The file can be Emacs Lisp, PHP, Perl, Python, Ruby, JavaScript, Bash, Ocaml, Visual Basic, TeX, Java, Clojure.
File suffix is used to determine what program to run.

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

URL `http://ergoemacs.org/emacs/elisp_run_current_file.html'
version 2016-01-28"
  (let (
          ;; (‹extension› . ‹shell program name›)
            ("php" . "php")
            ("pl" . "perl")
            ("py" . "python")
            ("py3" . ,(if (string-equal system-type "windows-nt") "c:/Python32/python.exe" "python3"))
            ("rb" . "ruby")
            ("go" . "go run")
            ("js" . "node") ; node.js
            ("sh" . "bash")
            ("clj" . "java -cp /home/xah/apps/clojure-1.6.0/clojure-1.6.0.jar clojure.main")
            ("rkt" . "racket")
            ("ml" . "ocaml")
            ("vbs" . "cscript")
            ("tex" . "pdflatex")
            ("latex" . "pdflatex")
            ("java" . "javac")
            ;; ("pov" . "/usr/local/bin/povray +R2 +A0.1 +J1.2 +Am2 +Q9 +H480 +W640")


    (when (null (buffer-file-name)) (save-buffer))
    (when (buffer-modified-p) (save-buffer))

    (setq ξfname (buffer-file-name))
    (setq ξfSuffix (file-name-extension ξfname))
    (setq ξprog-name (cdr (assoc ξfSuffix ξsuffix-map)))
    (setq ξcmd-str (concat ξprog-name " \""   ξfname "\""))

     ((string-equal ξfSuffix "el") (load ξfname))
     ((string-equal ξfSuffix "java")
        (shell-command ξcmd-str "*xah-run-current-file output*" )
         (format "java %s" (file-name-sans-extension (file-name-nondirectory ξfname))))))
     (t (if ξprog-name
              (message "Running…")
              (shell-command ξcmd-str "*xah-run-current-file output*" ))
          (message "No recognized program file suffix for this file."))))))

The code is very easy to understand.

(info "(elisp) Association Lists")

(info "(elisp) Buffer File Name")

(info "(elisp) File Name Components")

Now, we can define a keyboard shortcut for this:

(global-set-key (kbd "<f8>") 'xah-run-current-file)

〔➤ Emacs: How to Define Keys

So now, doesn't matter we are writing in Python, ruby, golang, …, we can just press a button and have the file executed or compiled.

Determine Language by Major Mode

If you don't have file extension, you might use the current major mode to determine what language it is.

The major mode's name is stored in the variable major-mode.

You also need to redefine your associative list to store major mode names corresponding to program paths.

(thanks to Matic Bojan for suggesting major mode for determining language.)

This package, emacs-quickrun, supports more languages and features. https://github.com/syohex/emacs-quickrun/

Like it? Buy Xah Emacs Tutorial.