在Emacs中编写"Hello World"?

21 emacs elisp

我想在Emacs Lisp中编写一些Unix脚本.但是,似乎没有一种干净的方式来写入STDOUT,因此我可以将结果重定向到文件或将输出传递给另一个命令.在打印功能则以双引号输出字符串,所以我得到的"Hello world!" 而不是Hello世界!.

这是emacs脚本.

#!/usr/bin/emacs --script
;;
;; Run me from a Unix shell: ./hello.el > x.txt
;;
(message "Hello world!  I'm writing to STDERR.")
(print "Hello world!  I'm writing to STDOUT but I'm in quotes")
(insert "Hello world!  I'm writing to an Emacs buffer")
(write-file "y.txt")

以下是我想称之为的方式.

hello.el > x.txt
hello.el | wc

Dav*_*ian 23

好像你想要princ而不是print.所以,基本上:

(princ "Hello world! I'm writing to STDOUT but I'm not in quotes!")

但是,有一点需要注意的是,princ不会自动终止输出\n.


Chr*_*sen 6

正如David Antaramian所说,你可能想要princ.

此外,message支持printf改编自的格式控制字符串(类似于C)format.所以,你最终可能想要做类似的事情

(princ (format "Hello, %s!\n" "World"))
Run Code Online (Sandbox Code Playgroud)

作为一些功能加上演示:

(defun fmt-stdout (&rest args)
  (princ (apply 'format args)))
(defun fmtln-stdout (&rest args)
  (princ (apply 'format
                (if (and args (stringp (car args)))
                    (cons (concat (car args) "\n") (cdr args))
                  args))))

(defun test-fmt ()
  (message "Hello, %s!" "message to stderr")
  (fmt-stdout "Hello, %s!\n" "fmt-stdout, explict newline")
  (fmtln-stdout "Hello, %s!" "fmtln-stdout, implicit newline"))
Run Code Online (Sandbox Code Playgroud)