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

Ray*_*ega 70 emacs datetime timestamp text-editor

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

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

小智 130

C-u M-! date
Run Code Online (Sandbox Code Playgroud)

  • 只是为了完整性:`M-!`是函数`shell-command`的键盘快捷键.所以`M-!date`将调用shell命令`date`,并在输出区域显示它(迷你缓冲区,因为输出足够短以适应).`Cu`是一个前缀参数,它使`M-!`将其输出放在当前缓冲区中. (55认同)
  • 多么直观:) (20认同)
  • 如果使用Windows,"date"是更改系统日期的命令,您将获得令人惊讶的结果.使用elisp处理这个问题的答案不依赖于Unix,类Unix甚至Linux操作系统. (11认同)
  • M-!实际上非常直观,给它"!" 是大多数(经典)交互式UNIX应用程序中的shell-escape.像vi ......:D (2认同)

CMS*_*CMS 37

放入.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)
Run Code Online (Sandbox Code Playgroud)

参考

  • 我使用与此类似的东西,但我将其从 Cc Ct 更改为 Cx Ct,因为 Cc Ct 妨碍了组织模式的“标记 TODO 项”绑定。不幸的是,这对我来说是根深蒂固的肌肉记忆。:) (3认同)

Mic*_*nis 29

我用过这些简短的片段:

(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")))
Run Code Online (Sandbox Code Playgroud)

他们最初来自journal.el

  • 谢谢。非常好。对于完全不了解emacs的用户:将其添加到`.emacs.`文件中并保存,然后将光标(点)移至每个函数的末尾括号,然后对每个函数使用“ Mx Me” 。现在,您可以在任意位置使用“立即Mx”或“今天Mx”进行插入。 (2认同)

tan*_*nfa 17

对于插入日期:

M-x org-time-stamp
Run Code Online (Sandbox Code Playgroud)

对于插入日期时间:

C-u M-x org-time-stamp
Run Code Online (Sandbox Code Playgroud)

您可以绑定此命令的全局键.

org-mode该方法非常用户友好,您可以从日历中选择任何日期.


Mar*_*evy 15

您可以安装yasnippet,它可以让您输入"time"和tab键,并且还可以执行更多操作.它只是current-time-string在幕后调用,因此您可以使用控制格式format-time-string.

  • 与内置模板和 hippie-expand 相比,yasnippet 是一个资源猪。 (2认同)

Rya*_*ary 5

这是我刚才写的一个包,它可以满足您的要求.

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)
Run Code Online (Sandbox Code Playgroud)