Comment puis-je insérer la date et l’heure actuelles dans un fichier à l’aide d’Emacs?

Quelles commandes dans Emacs puis-je utiliser pour insérer dans le tampon de texte d’un fichier la date et l’heure actuelles?

(Par exemple, l’équivalent dans le Bloc-notes consiste simplement à appuyer sur F5, ce qui est à peu près la seule fonctionnalité utile pour le Bloc-notes!)

Cu M-! date 

Mettez dans votre fichier .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-ssortingng' 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-ssortingng 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-ssortingng current-time-format (current-time))) (insert "\n") ) (global-set-key "\Cc\Cd" 'insert-current-date-time) (global-set-key "\Cc\Ct" 'insert-current-time) 

Référence

J’ai utilisé ces petits extraits:

 (defun now () "Insert ssortingng for the current time formatted like '2:34 PM'." (interactive) ; permit invocation in minibuffer (insert (format-time-ssortingng "%D %-I:%M %p"))) (defun today () "Insert ssortingng for today's date nicely formatted in American style, eg Sunday, September 17, 2000." (interactive) ; permit invocation in minibuffer (insert (format-time-ssortingng "%A, %B %e, %Y"))) 

Ils sont venus à l’origine de journal.el

Vous pouvez installer yasnippet , ce qui vous permettra de taper “time” et la touche de tabulation, et fait beaucoup plus que cela. Il appelle simplement current-time-ssortingng arrière-plan, vous pouvez donc contrôler le formatage en utilisant format-time-ssortingng .

Pour la date d’insertion: Mx org-time-stamp Pour la date et l’heure d’insertion: Cu-Mx

Peut-être que vous pouvez lier une clé globale pour cette commande.

La méthode du mode org est très conviviale, vous pouvez sélectionner n’importe quelle date dans le calendrier.

Voici un paquet que j’ai écrit il y a quelque temps qui fait ce que vous demandez.

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-! rendez-vous amoureux

Cela provoque l’insertion de la commande shell que vous exécutez dans le tampon que vous êtes en train d’éditer plutôt qu’un nouveau tampon.

Merci, CMS! Ma variation, pour ce que ça vaut – me rend assez heureux:

 (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-ssortingng' 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-ssortingng bjk-timestamp-format (current-time))) ) 

Je mets ceci dans un fichier appelé par mes .emacs en utilisant:

 (load "c:/bjk/elisp/bjk-timestamp.el") 

ce qui rend la modification plus facile sans risquer de casser quelque chose d’autre dans mes .emacs, et m’a permis d’avoir un point d’entrée facile dans un jour peut-être apprendre ce qu’est la programmation Lisp d’Emacs.

Les critiques de PS concernant ma technique n00b sont les bienvenues.

Pour une réponse similaire à celles déjà affichées, avec des explications et d’autres extensions telles que l’ouverture automatique d’un fichier et l’insertion de la date actuelle à la fin (comme un journal), consultez la discussion de Paul Ford sur ses utilitaires emacs .

Voici ma version à ce sujet.

 (defun modi/insert-time-stamp (option) "Insert date, time, user name - DWIM. If the point is NOT in a comment/ssortingng, the time stamp is inserted prefixed with `comment-start' characters. If the point is IN a comment/ssortingng, 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 (ssortingngp comment-start) (if (or (nth 3 (syntax-ppss)) ; ssortingng (nth 4 (syntax-ppss))) ; comment ;; If the point is already in a comment/ssortingng (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-ssortingng 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 " ")))) 

Je préfère lier ceci à Cc d .