2008-10-30 71 views

回答

110
C-u M-! date 
+17

令人難以置信的直覺:) – msandiford 2010-09-24 04:16:25

+50

只是爲了完整:`M-!`是函數`shell-command`的鍵盤快捷鍵。所以`M-! date`將調用shell命令`date`,並將其顯示在輸出區域(minibuffer,因爲輸出足夠短以適合)。 `C-u`是一個前綴參數,它使`M-!`將其輸出放入當前緩衝區。 – ShreevatsaR 2011-09-26 05:09:03

32

把你的.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) 

Reference

+1

我使用類似的東西,但是我將它從C-C C-t改爲C-X C-t,因爲C-c C-t採用了org-mode的「標記TODO項目」綁定方式。對我來說,這是不幸的硬連線肌肉記憶。 :) – AssembledGhost 2012-04-10 08:55:21

+0

`「\ C-c \ C-d」`沒有任何事情發生在我身上。實際上,當輸入'C-d'時它會刪除當前行。但我認爲爲什麼`C-c`不工作。 – Jack 2013-02-15 03:32:22

13

您可以安裝yasnippet,這將讓你輸入「時間」和TAB鍵,並做了很多更多之外。它只是在幕後調用current-time-string,因此您可以使用format-time-string控制格式。

+1

與內置模板和hippie-expand相比,yasnippet是一種資源管理器。 – 2014-07-28 01:56:30

25

我已經使用了這些簡短片段:

(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

1

M-1 M-來了!日期

這會導致您運行的shell命令插入到當前正在編輯的緩衝區中,而不是新的緩衝區。

1

謝謝,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編程的全部內容。

P.S.對我的n00b技術的批評最受歡迎。

10

對於插入日期: M-X ORG-時間戳 對於插入日期時間: C-U m的-X ORG-時間戳

可能是你可以綁定這個命令全局鍵。

組織模式的方法非常用戶友好,您可以選擇日曆中的任何日期。

0

這是我的承擔。

(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

相關問題