我如何使用Emacs将当前date和时间插入到文件中?

Emacs中的哪些命令可以用来将当前date和时间插入到文件的文本缓冲区中?

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

Cu M-! date 

放入您的.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 "\Cc\Cd" 'insert-current-date-time) (global-set-key "\Cc\Ct" '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, eg Sunday, September 17, 2000." (interactive) ; permit invocation in minibuffer (insert (format-time-string "%A, %B %e, %Y"))) 

他们最初来自journal.el

你可以安装yasnippet ,它可以让你input“time”和tab键,而且还可以做更多的事情。 它只是在幕后调用current-time-string ,因此您可以使用format-time-string来控制格式。

对于插入date:Mx组织时间戳记对于插入date时间:Cu Mx组织时间戳记

也许你可以为这个命令绑定一个全局密钥。

组织模式的方法非常用户友好,您可以select日历中的任何date。

这是我刚才写的一个包,就是你要的。

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-! date

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

谢谢,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 Ch 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技术最受欢迎。

对于类似于已经发布的答案,以及解释和更多的扩展,比如自动打开一个文件并在其末尾插入当前date(如日记),请查看Paul Ford关于他的emacs实用程序的讨论 。

这是我的承诺。

 (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: Cu -> Only `comment-start'/`--' prefixes are NOT inserted Cu Cu -> Only user name is NOT inserted Cu Cu Cu -> 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)) ; Cu or Cu Cu Cu (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)) ; Cu or Cu Cu Cu (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))) ; Cu Cu (insert (concat " - " (getenv "USER")))) ;; Insert a space after the time stamp if not at the end of the line (when (not (looking-at " *$")) (insert " ")))) 

我更喜欢把这个绑定到Cc d