2009-04-29 63 views
66

每天我啓動emacs並打開前一天打開的完全相同的文件。有什麼我可以添加到init.el文件,所以它會重新打開我上次退出emacs時使用的所有緩衝區?Emacs:在啓動時從上次會話重新打開緩衝區?

+0

似乎在Emacs 21和22+中桌面處理有所不同。該主題在此頁面上進行了描述:http://www.emacswiki.org/emacs/DeskTop。 – hji 2009-06-08 12:53:30

回答

92

可以使用Emacs Desktop library

您可以保存在桌面與 手動命令M-X桌面保存。您可以 也使 桌面自動保存,當你退出Emacs,並 最後 保存的桌面自動恢復Emacs啓動時:使用 的定製緩衝液(見易 定製)設置 桌面節省模式來t代表未來 會議,或在您的 〜/ .emacs文件中加入這一行:

(desktop-save-mode 1) 
+3

很好的答案。但是,我注意到Emacs在會話之間重新排列了緩衝區順序。有沒有辦法總是有相同的緩衝區順序? – axel22 2011-01-18 19:44:38

+1

Emacs ..我喜歡它。 :-) – Jack 2012-11-14 03:03:51

2

您可以使用下面的函數在你的.emacs文件中打開文件:

(find-file「/ home/me/path-to-file」)

+1

根據你的解決方案,提問者應該每次都在她的dotemacs中輸入最近打開的文件名。不是很有希望。 – 2009-04-30 08:29:45

10

雖然我懷疑這個問題是在尋找emacs的「桌面」功能(參見上面的答案),但Lewap的方法可能是有用的,一套使用的文件確實是完全相同的文件集。事實上,我們可以走得更遠了一步,定義「配置文件」如果一個人有不同的組經常使用的文件...晨間例如:

(let ((profile 
     (read-from-minibuffer "Choose a profile (acad,dist,lisp,comp,rpg): ") 
     )) 
    (cond 
    ((string-match "acad" profile) 
    (dired "/home/thomp/acad") 
    (dired "/home/thomp/acad/papers") 
    ) 
    ((string-match "lisp" profile) 
    (setup-slime) 
    (lisp-miscellany) 
    (open-lisp-dirs) 
    ) 
    ((string-match "rpg" profile) 
    (find-file "/home/thomp/comp/lisp/rp-geneval/README") 
    (dired "/home/thomp/comp/lisp/rp-geneval/rp-geneval") 
... etc. 

如果你發現你經常來回切換不同套之間在您工作時經常使用的文件,請考慮使用perspectives並使用所需的一組常規使用的文件填充每個透視圖。

2

您可以對基本桌面功能進行有用的增強。特別方便的(IMO)是在會話期間自動保存桌面的方法,否則,如果系統崩潰,您將與您開始會話的桌面文件卡住 - 如果您傾向於保持Emacs運行多個每天一次。

http://www.emacswiki.org/emacs/DeskTop

維基也有對一般會話之間持久化數據的有用信息:

http://www.emacswiki.org/emacs/SessionManagement

對於臺式機而言,我認爲Desktop Recover看起來特別有前途的,但是我還沒試過了。

0

(find-file-noselect "/my/file")會默默打開它,即不提高緩衝區。只是說。

編輯這條命令是不是 interactive;爲了測試它,你必須評估表達式,例如通過將光標定位在最後一個括號之後並擊中C-x C-e

下調這個是不是很酷;這個命令絕對有效,並且在這個問題的範圍之內。

5

對於存儲/恢​​復緩衝器/標籤(特別是elscreen選項卡):我用elscreen和我管理存儲的方式/恢復桌面會話和elscreen卡配置是在我的.emacs文件中的以下代碼(所使用的名稱是不言自明的,如果每次啓動emacs時都不應執行存儲/恢復功能,只需用「(push#'elscreen-store kill-emacs-hook)」和「(elscreen-restore)」註釋行):

(defvar emacs-configuration-directory 
    "~/.emacs.d/" 
    "The directory where the emacs configuration files are stored.") 

(defvar elscreen-tab-configuration-store-filename 
    (concat emacs-configuration-directory ".elscreen") 
    "The file where the elscreen tab configuration is stored.") 

(defun elscreen-store() 
    "Store the elscreen tab configuration." 
    (interactive) 
    (if (desktop-save emacs-configuration-directory) 
     (with-temp-file elscreen-tab-configuration-store-filename 
      (insert (prin1-to-string (elscreen-get-screen-to-name-alist)))))) 
(push #'elscreen-store kill-emacs-hook) 
(defun elscreen-restore() 
    "Restore the elscreen tab configuration." 
    (interactive) 
    (if (desktop-read) 
     (let ((screens (reverse 
         (read 
         (with-temp-buffer 
          (insert-file-contents elscreen-tab-configuration-store-filename) 
          (buffer-string)))))) 
      (while screens 
       (setq screen (car (car screens))) 
       (setq buffers (split-string (cdr (car screens)) ":")) 
       (if (eq screen 0) 
        (switch-to-buffer (car buffers)) 
        (elscreen-find-and-goto-by-buffer (car buffers) t t)) 
       (while (cdr buffers) 
        (switch-to-buffer-other-window (car (cdr buffers))) 
        (setq buffers (cdr buffers))) 
       (setq screens (cdr screens)))))) 
(elscreen-restore)