试试这一段代码。 我改了一点 @子龙山人 配置的一段代码。 不止 ruby 可以用哦。
(defun zilongshanren/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.
File suffix is used to determine what program to run.
If the file is modified, ask if you want to save first.
URL `http://ergoemacs.org/emacs/elisp_run_current_file.html'
version 2015-08-21"
(interactive)
(let* (
(ξsuffix-map
;; (‹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")
("js" . "node") ; node.js
("sh" . "bash")
;; ("clj" . "java -cp /home/xah/apps/clojure-1.6.0/clojure-1.6.0.jar clojure.main")
("ml" . "ocaml")
("vbs" . "cscript")
("tex" . "pdflatex")
("lua" . "lua")
("cpp" . "make && ./runcpp") ; compile for c++. and you need Makefile in dir by noclyt 2016.7.8
;; ("pov" . "/usr/local/bin/povray +R2 +A0.1 +J1.2 +Am2 +Q9 +H480 +W640")
))
(ξfname (buffer-file-name))
(ξfSuffix (file-name-extension ξfname))
(ξprog-name (cdr (assoc ξfSuffix ξsuffix-map)))
(ξcmd-str (if (string-equal ξfSuffix "cpp")
(concat ξprog-name " ")
(concat ξprog-name " \"" ξfname "\""))))
(when (buffer-modified-p)
(when (y-or-n-p "Buffer modified. Do you want to save first?")
(save-buffer)))
(if (string-equal ξfSuffix "el") ; special case for emacs lisp
(load ξfname)
(if ξprog-name
(progn
(message "Running…")
(async-shell-command ξcmd-str "*zilongshanren/run-current-file output*"))
(message "No recognized program file suffix for this file.")))))
对这个函数,自己做个 key-binding
(global-set-key (kbd "<f8>") 'zilongshanren/run-current-file)