【问题标题】:Emacs: Open file with date in title, insert textEmacs:打开标题中带有日期的文件,插入文本
【发布时间】:2015-02-08 01:28:02
【问题描述】:

我在this question中有一个脚本的修改版本,即

(defun now ()
  (interactive)
  (let ((daily-name (format-time-string "%y%m%d_%H%M%S")))
    (find-file (format "/path/%s.org" daily-name))))

在我的.emacs 中,但我还想在打开文件时在光标前插入一个字符串。我对 Emacs Lisp 完全不熟悉,所以我不知道该怎么做。我该怎么做?

谢谢!

【问题讨论】:

  • 喜欢这个? (defun now () (interactive) (let ((daily-name (format-time-string "%y%m%d_%H%M%S"))) (find-file (format "/path/%s.org" daily-name)) (insert "hello-world")))
  • 这太完美了,谢谢!

标签: emacs


【解决方案1】:

EmacsWiki 上的页面Inserting Today's Date 提供了多种方式来执行您的请求,从使用shell 到使用Emacs 日历和使用format-time-string

【讨论】:

    【解决方案2】:

    我认为非常有用的 emacs 的一个很少使用的功能是“节奏”。它已经存在很多年了,还有各种其他的包,比如 ysn-ps,它们可以做类似的事情。

    本质上,tempo 允许您定义可以插入到文件中的模板。这些模板可以具有静态和动态内容。我使用速度模板在我的文件顶部插入一个标题,其中包含各种信息,包括创建日期和最后修改日期。通过使用其他 emacs 功能,每次保存文件时都会更新上次修改日期。这是我使用的。请注意,我绑定了 tempo 函数以将模板插入 f5,因此插入模板所需要做的就是在创建新文件时按 f5。您也可以在打开特定类型的新文件时自动插入模板。

    (require 'tempo)
    
    
    (tempo-define-template "generic-header"
                           '((format "%s%s" comment-start comment-start)
                             "      Filename: "
                             (file-name-nondirectory (buffer-file-name)) 'n
                             (format "%s%s" comment-start comment-start)
                             " Creation Date: "
                             (format-time-string "%A, %d %B %Y %I:%M %p %Z") 'n
                             (format "%s%s" comment-start comment-start)
                             " Last Modified: "
                             (format-time-string "%A, %d %B %Y %I:%M %p %Z") 'n
                             (format "%s%s" comment-start comment-start)
                             "        Author: Tim Cross <theophilusx AT gmail.com>"
                             'n
                             (format "%s%s" comment-start comment-start)
                             "   Description:" 'n
                             (format "%s%s" comment-start comment-start) 'n
                             'n))
    
    ;;; Lets setup some key bindings.
    
    (global-set-key [(f5)] 'tempo-template-generic-header)
    
    (provide 'tx-template)
    

    这是更新时间戳的位

    (add-hook 'write-file-hooks 'time-stamp)
    
    (setq time-stamp-active t)
    (setq time-stamp-format "%:a, %02d %:b %:y %02I:%02M %#P %Z")
    (setq time-stamp-start "\\(Time-stamp:[         ]+\\\\?[\"<]+\\|Last Modified:[ 
            ]\\)")
    (setq time-stamp-end "\\\\?[\">]\\|$")
    (setq time-stamp-line-limit 10)
    
    (provide 'tx-timestamp)
    

    【讨论】:

      【解决方案3】:

      header2.el 允许您(可选)在打开新文件时自动插入文件头。您可以自定义标题中的内容。您可以选择的预定义字段(您也可以轻松定义自己的字段)包括带有时间戳的字段(您可以为其定义所需的时间/日期格式)。

      除了在新文件中插入标头外,该库还可以自动更新您选择的标头字段 - 特别是上次修改时间/日期字段。

      header2.el 可在Emacs WikiMELPA 获得。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-23
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        相关资源
        最近更新 更多