在Emacs中,我可以使用哪些命令将当前日期和时间插入到文件的文本缓冲区中?
(例如,在记事本中相当于只需按F5,这几乎是唯一有用的功能!)
在Emacs中,我可以使用哪些命令将当前日期和时间插入到文件的文本缓冲区中?
(例如,在记事本中相当于只需按F5,这几乎是唯一有用的功能!)
C-u M-! date
M-!
是键盘快捷方式,用于函数 shell-command
。 因此,M-! date
将调用 shell 命令 date
并在输出区域中显示它(由于输出足够短,可以适应迷你缓冲区)。 C-u
是前缀参数,使 M-!
将其输出放入当前缓冲区。 - ShreevatsaR在你的 .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)
我使用了以下这些简短的代码片段:
(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
文件中并保存,然后将光标(point)移动到每个函数的结束括号,然后为每个函数键入“M-x M-e”。现在,您可以在任何地方使用“M-x now”或“M-x today”进行插入。 - celwell插入日期:
M-x org-time-stamp
插入日期时间:
C-u M-x org-time-stamp
你可以为该命令绑定一个全局键。
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-stamp nil))
;; 插入自定义格式的日期。
(let ((org-time-stamp-formats '("%Y-%m-%d" . "%Y-%m-%d %H:%M:%S")))
(org-time-stamp '(4)))
- tangxinfa这是我一段时间前写的一个软件包,可以完成你所要求的功能。
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)
(插入 (current-time-string))
M-1 M-! 日期
这会导致您运行的 shell 命令插入到当前正在编辑的缓冲区中,而不是新的缓冲区中。
插入日期:
C-c . RET
要选择日期,向左/右/上/下移动 - 按回车键 (Enter)
C-c .
被绑定了 - 但在文本模式下默认情况下没有被绑定。 - dat谢谢,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技术提出批评意见。