2012-06-29 45 views
5

在VIM中,可以使用%來指示調用shell命令時的當前文件名。任何人都可以指示我在文檔的方向上顯示emacs中的等效內容嗎?Emacs相當於VIM的'%`?

+2

[buffer-file-name](http://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer-File-Name.html) – jfs

+1

Emacs中沒有等價物。 'buffer-file-name'是一個Elisp變量,不能在shell命令提示符下使用。 –

+1

[將Emacs變量傳遞給minibuffer shell命令]交叉鏈接(http://stackoverflow.com/questions/10121944/passing-emacs-variables-to-minibuffer-shell-commands)(這基本上是重複的,除了這兩個問題來自明顯不同的角度)。 – phils

回答

4

沒有一個。但是這是Emacs!所以在這裏:(:不IDO工作,但很明顯,你可以隨時離開,與例如C-X C-F警告)

(defun my-shell-command-on-current-file (command &optional output-buffer error-buffer) 
    "Run a shell command on the current file (or marked dired files). 
In the shell command, the file(s) will be substituted wherever a '%' is." 
    (interactive (list (read-from-minibuffer "Shell command: " 
              nil nil nil 'shell-command-history) 
        current-prefix-arg 
        shell-command-default-error-buffer)) 
    (cond ((buffer-file-name) 
     (setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t))) 
     ((and (equal major-mode 'dired-mode) (save-excursion (dired-move-to-filename))) 
     (setq command (replace-regexp-in-string "%" (mapconcat 'identity (dired-get-marked-files) " ") command nil t)))) 
    (shell-command command output-buffer error-buffer)) 

(global-set-key (kbd "M-!") 'my-shell-command-on-current-file) 
1

您可以隨時迷你緩衝區希望你輸入的東西用這個。您也可以在常規緩衝區中使用它。

(defun insert-filename-or-buffername (&optional arg) 
    "If the buffer has a file, insert the base name of that file. 
    Otherwise insert the buffer name. With prefix argument, insert the full file name." 
    (interactive "P") 
    (let* ((buffer (window-buffer (minibuffer-selected-window))) 
     (file-path-maybe (buffer-file-name buffer))) 
    (insert (if file-path-maybe 
       (if arg 
        file-path-maybe 
        (file-name-nondirectory file-path-maybe)) 
       (buffer-name buffer))))) 

(define-key minibuffer-local-map (kbd "C-c f") 'insert-filename-or-buffername) 
0
在我的情況

,使用Emacs從自制24 GUI版本....我看到文件名是從(小)型杆的左下角的第三個項目,正上方小緩衝區。

要看我在哪裏,使用ido模式我只是做C-x C-f沒有配置需要那裏。