2012-06-29 3 views
5

VIMでは、シェルコマンドを呼び出すときに現在のファイル名を示すのに%を使用できます。誰でもemacsに相当するものを示すドキュメンテーションの方向に私を指摘できますか?VIMの `%`に相当するEmacs?

+2

[バッファファイル名](http://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer-File-Name.html) – jfs

+1

Emacsには同等のものはありません。 'buffer-file-name'はElisp変数であり、シェルコマンドプロンプトでは使用できません。 –

+1

[Emacs変数をミニバッファシェルコマンドに渡す](http://stackoverflow.com/questions/10121944/passing-emacs-variables-to-minibuffer-shell-commands)(これは基本的には重複していますが、 2つの質問は著しく異なる角度から来ている)。 – phils

答えて

4

ありません。しかし、これはEmacsです!だからここに:

(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

ミニバッファはあなたが何か(警告:IDOでは動作しませんが、明らかにあなたは常に、例えばC-X C-Fとのことから抜け出すことができます)を入力することを期待するたびにあなたがこれを使用することができます。通常のバッファーでも使用できます。自作からのemacs 24 GUIバージョンを使用して、私の場合は

(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

、....私はちょうど上に、(マイナー)-mode-バーの左下隅からに第三項目としてファイル名を参照してくださいミニバッファ。私はここで、iは、ただそこに必要なC-x C-fん設定しないIDOモードを使用して、表示するには