如何使用Emacs将当前日期和时间插入文件?


74

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

(例如,记事本中的等效项只是按F5键,这是记事本唯一有用的功能!)


2
记事本中的Ctrl + G会打开“转到行”对话框,这也很有用!
cfeduke

Answers:


136
C-u M-! date

58
仅出于完整性考虑:M-!是该功能的键盘快捷键shell-command。因此M-! date将调用shell命令date,并在输出区域中显示它(最小缓冲区,因为输出足够短以适合显示)。该C-u是一个前缀参数,使得M-!把它的输出电流缓冲液代替。
ShreevatsaR 2011年

11
如果使用Windows,将获得令人惊讶的结果,“ date”是更改系统日期的命令。用elisp处理这个问题的答案不依赖于Unix,类Unix甚至Linux操作系统。
理查德·霍斯金斯

40

放入您的.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)

参考


1
我对此使用类似的方法,但是我将其从Cc Ct更改为Cx Ct,因为Cc Ct妨碍了组织模式的“标记待办事项”绑定。不幸的是,这对我的肌肉记忆具有硬性约束。:)
AssembledGhost 2012年

"\C-c\C-d"我什么也没有发生。实际上,C-d键入时会删除当前行。但是我认为为什么C-c不起作用。
杰克

30

我使用了以下简短摘要:

(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.文件中并保存,然后将光标(点)移至每个函数的末尾括号,然后M-x M-e对每个函数使用它来使用。现在,您可以在任何位置插入M-x now或使用M-x today
Celwell 2014年

21

对于插入日期:

M-x org-time-stamp

对于插入日期时间:

C-u M-x org-time-stamp

您可以为此命令绑定一个全局密钥。

org-mode的方法非常人性化,您可以从日历中选择任何日期。


有没有一种格式格式可以不同于组织模式日期格式?
Asalle

1
日期时间格式insert by由org-time-stamp指定org-time-stamp-formats。;; 以自定义格式插入日期。(let(((org-time-stamp-formats'(“%Y-%m-%d”。“%Y-%m-%d%H:%M:%S”)))(org-time-盖章));; 以自定义格式插入日期。(let(((org-time-stamp-formats'(“%Y-%m-%d”。“%Y-%m-%d%H:%M:%S”)))(org-time-戳'(4)))
tangxinfa



2

M-1 M-!日期

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


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技术的PS评论非常受欢迎。


1

没有炮轰“日期”的最简单方法可能是:

(插入(当前时间字符串))


0

这是我的看法。

(defun modi/insert-time-stamp (option)
  "Insert date, time, user name - DWIM.

If the point is NOT in a comment/string, the time stamp is inserted prefixed
with `comment-start' characters.

If the point is IN a comment/string, the time stamp is inserted without the
`comment-start' characters. If the time stamp is not being inserted immediately
after the `comment-start' characters (followed by optional space),
the time stamp is inserted with “--” prefix.

If the buffer is in a major mode where `comment-start' var is nil, no prefix is
added regardless.

Additional control:

        C-u -> Only `comment-start'/`--' prefixes are NOT inserted
    C-u C-u -> Only user name is NOT inserted
C-u C-u C-u -> Both prefix and user name are not inserted."
  (interactive "P")
  (let ((current-date-time-format "%a %b %d %H:%M:%S %Z %Y"))
    ;; Insert a space if there is no space to the left of the current point
    ;; and it's not at the beginning of a line
    (when (and (not (looking-back "^ *"))
               (not (looking-back " ")))
      (insert " "))
    ;; Insert prefix only if `comment-start' is defined for the major mode
    (when (stringp comment-start)
      (if (or (nth 3 (syntax-ppss)) ; string
              (nth 4 (syntax-ppss))) ; comment
          ;; If the point is already in a comment/string
          (progn
            ;; If the point is not immediately after `comment-start' chars
            ;; (followed by optional space)
            (when (and (not (or (equal option '(4)) ; C-u or C-u C-u C-u
                                (equal option '(64))))
                       (not (looking-back (concat comment-start " *")))
                       (not (looking-back "^ *")))
              (insert "--")))
        ;; If the point is NOT in a comment
        (progn
          (when (not (or (equal option '(4)) ; C-u or C-u C-u C-u
                         (equal option '(64))))
            (insert comment-start)))))
    ;; Insert a space if there is no space to the left of the current point
    ;; and it's not at the beginning of a line
    (when (and (not (looking-back "^ *"))
               (not (looking-back " ")))
      (insert " "))
    (insert (format-time-string current-date-time-format (current-time)))
    (when (not (equal option '(16))) ; C-u C-u
      (insert (concat " - " (getenv "USER"))))
    ;; Insert a space after the time stamp if not at the end of the line
    (when (not (looking-at " *$"))
      (insert " "))))

我更喜欢将此绑定到C-c d

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.