2011-07-27 23 views

回答

19

是的。在dired中使用!在文件上運行shell命令。

對於evince,使用&會更聰明,它會異步運行該命令,因此在您打開PDF時,emacs仍然可用。

3

您可以使用!來打開文件,然後指定一個命令。

+4

如果你想異步運行該命令,而'&'而不是'!'。 – Thomas

14

有更多的方法可以做到這一點。我建議OpenWith庫。設置爲你的情況可能看起來像:

(add-to-list 'load-path "/path/to/downloaded/openwith.el") 
(require 'openwith) 
(setq openwith-associations '(("\\.pdf\\'" "evince" (file)))) 
(openwith-mode t) 

它設置文件處理程序,將來自diredfind-file工作。

+3

至少在Mac OSX中,我用'open'替換'evince',然後使用默認軟件(在我的情況下爲'Skim')。 – Dror

+3

請注意,在這種情況下,您只需在dired中的文件名上點擊'RET',而不是使用'!'或'&'運行shell命令。通過在openwith-association上使用setq,您還會失去所有內置關聯。可能有更好的方法來改變只有pdf關聯,而離開協會爲.mp3,.jpg,...? –

+0

是的,有一種簡單的方法可以在不丟失其他文件的情況下改變pdf關聯:M-x customize-group RET openwith RET – Jorge

8

試試這個。

(defun dired-open-file() 
    "In dired, open the file named on this line." 
    (interactive) 
    (let* ((file (dired-get-filename nil t))) 
    (message "Opening %s..." file) 
    (call-process "gnome-open" nil 0 nil file) 
    (message "Opening %s done" file))) 
+3

這很好。如果在另一個GUI(例如KDE)中使用,我將'gnome-open'替換爲'xdg-open'。 –

3

注意,您可以保持工藝活用nohup [百科]退出的Emacs後,所以把點上的一個文件中dired

C-u ! nohup evince ? & 

它創建了一個Persistent Processes [EmacsWiki] 。

0

這是一個打開的任何外部應用程序從一個OSX框上的直接模式,使用鍵盤快捷鍵C-c o當光標與用戶想要用外部應用程序打開一個文件的行 - 一個第二個直接緩衝器打開到/Applications文件夾,然後回車鍵選擇外部應用程序。 [選擇多個文件超出了這個答案的範圍,但是,我已經包含了一個dired-get-marked-files的定義,以防萬一有人感興趣。我還包括一個選項來打開一個外部應用程序作爲使用類似Finder.app或Dock.app或Spotlight的替代方案 - 從/Applications文件夾中的dired中,RET鍵將爲用戶提供選擇,以打開應用外部或進入封裝的應用程序的文件夾]

的第二種方法是使用類似(equal (file-name-extension input-filename) ".pdf"))設置條件和使用start-process . . .以激活特定外部應用程序來打開所選擇的文件 - 例如,(start-process "pdf-with-skim" nil "open" "-a" "/Applications/Skim.app/Contents/MacOS/Skim" input-filename)。這可以通過子選項(例如,Adobe,Skim或Preview)製作得相當詳細,也可以使用string-match函數或各種文件擴展名。第二種方法更適合於基於安裝的特定程序的用戶個人偏好,而第一種方法(下文)可用於訪問任何已安裝的外部應用程序。

(require 'dired) 

(defun dired-read-file-name (&optional directory) 
    (if directory 
     (dired directory) 
    (dired nil)) 
    (let (output-filename) 
     (recursive-edit) 
     output-filename)) 

;; Open with external application. 
(defvar open-with-variable nil) 
(define-key dired-mode-map (kbd "C-c o") (lambda() (interactive) 
    (setq open-with-variable t) 
    (let* (
     (lawlist-filename (dired-get-file-for-visit)) 
     (application (dired-read-file-name "/Applications"))) 
    (start-process "external" nil "open" "-a" application lawlist-filename) 
    (setq open-with-variable nil)))) 

;; select file or directory. 
(define-key dired-mode-map (kbd "<return>") (lambda() (interactive) 
    (let* (
    (input-filename 
     (if (or (re-search-backward "^*" nil t) 
      (re-search-forward "^*" nil t)) 
     (dired-get-marked-files) 
     (dired-get-file-for-visit))) 
    (dired-buffer-name (buffer-name))) 
    (cond 
    ;; open file 
    ((and (not (file-directory-p input-filename)) 
     (file-exists-p input-filename) 
     (not (equal input-filename (concat (file-name-directory input-filename) "."))) 
     (not open-with-variable)) 
     (kill-buffer dired-buffer-name) 
     (find-file input-filename)) 
    ;; open with external application 
    ((and (file-directory-p input-filename) 
     (not (equal input-filename (concat (file-name-directory input-filename) "."))) 
     open-with-variable 
     (equal (file-name-extension input-filename) "app")) 
     (setq output-filename input-filename) 
     (kill-buffer dired-buffer-name) 
     (throw 'exit nil)) 
    ;; Enter the directory; or, open an application 
    ((and (file-directory-p input-filename) 
     (not (equal input-filename (concat (file-name-directory input-filename) ".")))) 
     (if (equal (file-name-extension input-filename) "app") 
     (progn 
      (message "[o]pen %s" (file-name-nondirectory input-filename)) 
      (let* ((open-or-enter (read-char-exclusive))) 
      (cond 
       ((eq open-or-enter ?o) 
       (start-process "application" nil "open" "-a" input-filename) 
       (kill-buffer dired-buffer-name)) 
       (t 
       (dired-find-file) 
       (goto-char (point-min)) 
       (re-search-forward " \\.\\.$" nil t) 
       (kill-buffer dired-buffer-name))))) 
     (dired-find-file) 
     (goto-char (point-min)) 
     (re-search-forward " \\.\\.$" nil t) 
     (kill-buffer dired-buffer-name))))))) 
1

在Windows中,我使用了!並命令「資源管理器」打開PDF/Word/Excel ...

+0

我認爲「開始」應該工作得太短。 –

相關問題