ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

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

R
Ray
C-u M-! date

仅出于完整性考虑:M-! 是函数 shell-command 的键盘快捷键。因此 M-! date 将调用 shell 命令 date,并将其显示在输出区域(迷你缓冲区,因为输出足够短以适应)。 C-u 是一个前缀参数,它使 M-! 将其输出放在当前缓冲区中。
如果你使用 Windows,你会得到令人惊讶的结果,“date”是一个更改系统日期的命令。用 elisp 处理这个问题的答案不依赖于 Unix、类 Unix 甚至 Linux 操作系统。
C
Christian C. Salvadó

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

Reference


我使用与此类似的东西,但我将其从 Cc Ct 更改为 Cx Ct,因为 Cc Ct 妨碍了 org-mode 的“Mark TODO item”绑定。不幸的是,这对我来说是硬连线到肌肉记忆中的。 :)
"\C-c\C-d" 我什么也没发生。实际上,它会在输入 C-d 时删除当前行。但我认为这就是为什么 C-c 不起作用。
M
Michael Paulukonis

我使用了这些简短的片段:

(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


谢谢。非常好。对于完全是 emacs 新手的任何人:通过将其添加到 .emacs. 文件并保存它来使用它,然后将光标(点)移动到每个函数的末尾括号,然后为每个函数移动 M-x M-e。现在您可以使用 M-x nowM-x today 在任何您想要的地方插入。
S
Sridhar Ratnakumar

对于插入日期:

M-x org-time-stamp

对于插入日期时间:

C-u M-x org-time-stamp

您可以为此命令绑定全局键。

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


有没有办法将其格式化与 org-mode 日期格式不同?
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)))
M
Marcel Levy

您可以安装 yasnippet,它可以让您输入“时间”和 Tab 键,此外还有很多其他功能。它只是在后台调用 current-time-string,因此您可以使用 format-time-string 控制格式。


与内置模板和 hippie-expand 相比,yasnippet 是一种资源消耗。
R
Ryan McGeary

这是我不久前写的一个包,它可以满足您的要求。

http://github.com/rmm5t/insert-time.el/tree/master/insert-time.el

(require 'insert-time)
(define-key global-map [(control c)(d)] 'insert-date-time)
(define-key global-map [(control c)(control v)(d)] 'insert-personal-time-stamp)

佚名

M-1 M-!日期

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


D
Dino Dini

最简单的方法可能是:

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


b
bjkeefe

谢谢,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 编程的全部内容。

PS 对我的 n00b 技术的评论非常受欢迎。


M
Misho M. Petkovic

要插入日期:

C-c . RET 

选择日期左/右/上/下 - RET(回车)


C-c . 在 org 模式下绑定 - 但在文本模式下默认不绑定。
K
Kaushal Modi

这是我的看法。

(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