web-dev-qa-db-ja.com

Emacsを使用して現在の日付と時刻をファイルに挿入するにはどうすればよいですか?

Emacsのどのコマンドを使用して、ファイルのテキストバッファーに現在の日付と時刻を挿入できますか?

(たとえば、メモ帳の同等の機能は、メモ帳の唯一の便利な機能であるF5キーを押すだけです!)

68
Ray Vega
C-u M-! date
127
eclecticx

.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)

参照

37
CMS

これらの短いスニペットを使用しました:

(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 から来ました

29

挿入日:

M-x org-time-stamp

日付時刻を挿入する場合:

C-u M-x org-time-stamp

このコマンドのグローバルキーをバインドできます。

org-modeの方法は非常にユーザーフレンドリーで、カレンダーから任意の日付を選択できます。

16
tangxinfa

yasnippet をインストールできます。これにより、「時間」とタブキーを入力でき、さらに多くのことができます。舞台裏でcurrent-time-stringを呼び出すだけなので、format-time-stringを使用してフォーマットを制御できます。

15
Marcel Levy

これは私が先ほど書いたパッケージで、あなたが求めていることをします。

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)
5
Ryan McGeary

M-1 M-!日付

これにより、実行するシェルコマンドが、新しいバッファーではなく、現在編集中のバッファーに挿入されます。

2
user284244

ありがとう、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テクニックに関する批判は大歓迎です。

1
bjkeefe

すでに投稿されているものと同様の回答、およびファイルを自動的に開き、ファイルの最後に現在の日付を挿入する(ジャーナルなど)などの拡張機能とともに、チェックアウト Paul Fordのemacsユーティリティの説明

1
Brendan Foote

これが私の見解です。

(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にバインドすることを好みます。

0
Kaushal Modi