如何在Emacs中插入当前日期和时间到文件中?

97

在Emacs中,我可以使用哪些命令将当前日期和时间插入到文件的文本缓冲区中?

(例如,在记事本中相当于只需按F5,这几乎是唯一有用的功能!)


2
在记事本中按 Ctrl+G 可以打开“转到行”对话框,这也很有用! - cfeduke
12个回答

149
C-u M-! date

67
仅为完整起见:M-! 是键盘快捷方式,用于函数 shell-command。 因此,M-! date 将调用 shell 命令 date 并在输出区域中显示它(由于输出足够短,可以适应迷你缓冲区)。 C-u 是前缀参数,使 M-! 将其输出放入当前缓冲区。 - ShreevatsaR
13
如果你使用Windows,使用"date"命令来改变系统日期会得到意想不到的结果。使用Emacs Lisp来处理这个问题的答案并不依赖于Unix、类Unix或者Linux操作系统。 - Richard Hoskins

49

在你的 .emacs 文件中添加如下内容:

;; ====================
;; insert date and time

(defvar current-date-time-format "%a %b %d %H:%M:%S %Z %Y"
  "Format of date to insert with `insert-current-date-time' func
See help of `format-time-string' for possible replacements")

(defvar current-time-format "%a %H:%M:%S"
  "Format of date to insert with `insert-current-time' func.
Note the weekly scope of the command's precision.")

(defun insert-current-date-time ()
  "insert the current date and time into current buffer.
Uses `current-date-time-format' for the formatting the date/time."
       (interactive)
       (insert "==========\n")
;       (insert (let () (comment-start)))
       (insert (format-time-string current-date-time-format (current-time)))
       (insert "\n")
       )

(defun insert-current-time ()
  "insert the current time (1-week scope) into the current buffer."
       (interactive)
       (insert (format-time-string current-time-format (current-time)))
       (insert "\n")
       )

(global-set-key "\C-c\C-d" 'insert-current-date-time)
(global-set-key "\C-c\C-t" 'insert-current-time)

参考资料


3
我使用类似的东西,但将其从"C-c C-t"更改为"C-x C-t",因为"C-c C-t"会妨碍org-mode的“标记TODO项目”的绑定。对我来说,这非常不幸,因为这已经深入到了肌肉记忆中。 :) - AssembledGhost
“\C-c\C-d” 对我没有任何反应。实际上,当键入“C-d”时,它会删除当前行。但我认为这就是为什么“C-c”无法工作的原因。 - Jack
Emacs Lisp手册推荐使用C-c letter来定义用户键绑定。因此,将"\C-c\C-d"更改为(kbd "C-c d")应该避免与主要模式的冲突。 - Student

36

我使用了以下这些简短的代码片段:

(defun now ()
  "Insert string for the current time formatted like '2:34 PM'."
  (interactive)                 ; permit invocation in minibuffer
  (insert (format-time-string "%D %-I:%M %p")))

(defun today ()
  "Insert string for today's date nicely formatted in American style,
e.g. Sunday, September 17, 2000."
  (interactive)                 ; permit invocation in minibuffer
  (insert (format-time-string "%A, %B %e, %Y")))

它们最初来自journal.el


2
谢谢。非常好。对于完全不懂emacs的人:将此添加到您的.emacs文件中并保存,然后将光标(point)移动到每个函数的结束括号,然后为每个函数键入“M-x M-e”。现在,您可以在任何地方使用“M-x now”或“M-x today”进行插入。 - celwell

34

插入日期:

M-x org-time-stamp

插入日期时间:

C-u M-x org-time-stamp

你可以为该命令绑定一个全局键。

org-mode的方法非常用户友好,你可以从日历中选择任何日期。


有没有一种方法可以将其格式化为不同于org-mode日期格式的方式? - Asalle
1
org-time-stamp 插入的日期时间格式由 org-time-stamp-formats 指定。;; 插入自定义格式的日期。 (let ((org-time-stamp-formats '("%Y-%m-%d" . "%Y-%m-%d %H:%M:%S"))) (org-time-stamp nil)) ;; 插入自定义格式的日期。 (let ((org-time-stamp-formats '("%Y-%m-%d" . "%Y-%m-%d %H:%M:%S"))) (org-time-stamp '(4))) - tangxinfa

15
您可以安装yasnippet,这将允许您键入“time”和tab键,还有更多功能。它在幕后只调用current-time-string,因此您可以使用format-time-string来控制格式。

2
与内置模板和 hippie-expand 相比,yasnippet 是一个资源占用较高的工具。 - Richard Hoskins

5

3
最简单的方式而不用调用 "date" 命令可能是:

(插入 (current-time-string))


3

M-1 M-! 日期

这会导致您运行的 shell 命令插入到当前正在编辑的缓冲区中,而不是新的缓冲区中。


3

插入日期:

C-c . RET 

要选择日期,向左/右/上/下移动 - 按回车键 (Enter)


2
在org模式下,C-c .被绑定了 - 但在文本模式下默认情况下没有被绑定。 - dat

2

谢谢,CMS!我的变化,值得一提——让我很开心:

(defvar bjk-timestamp-format "%Y-%m-%d %H:%M"
  "Format of date to insert with `bjk-timestamp' function
%Y-%m-%d %H:%M will produce something of the form YYYY-MM-DD HH:MM
Do C-h f on `format-time-string' for more info")


(defun bjk-timestamp ()
  "Insert a timestamp at the current point.
Note no attempt to go to beginning of line and no added carriage return.
Uses `bjk-timestamp-format' for formatting the date/time."
       (interactive)
       (insert (format-time-string bjk-timestamp-format (current-time)))
       )

我将这段代码放入一个文件中,并使用以下方式在我的 .emacs 文件中引用它:

(load "c:/bjk/elisp/bjk-timestamp.el")

这样做不仅可以更轻松地修改我的.emacs文件,而且还可以为我提供一个简单的入门点,以便将来也许能够真正学习Emacs Lisp编程的内容。

顺便说一句,欢迎对我的n00b技术提出批评意见。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接