在Emacs中编写“Hello World”?

时间:2010-01-31 04:10:19

标签: emacs elisp

我想在Emacs Lisp中编写一些Unix脚本。但是,似乎没有一种干净的方式来写入STDOUT,因此我可以将结果重定向到文件或将输出传递给另一个命令。 打印函数在输出字符串周围放置双引号,因此我得到“Hello world!”而不是 Hello world!

这是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

2 个答案:

答案 0 :(得分:23)

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

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

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

答案 1 :(得分:6)

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

此外,message支持从printf改编的格式控制字符串(类似于C中的format)。所以,你最终可能想要做一些像

这样的事情
(princ (format "Hello, %s!\n" "World"))

作为一些功能加上演示:

(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"))
相关问题