2012-06-29 11 views
5

En VIM, se puede usar % para indicar el nombre de archivo actual al invocar un comando de shell. ¿Puede alguien señalarme en la dirección de la documentación que muestra cuál es el equivalente en emacs?Emacs equivalente al `%` de VIM?

+2

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

+1

no hay equivalente en Emacs. 'buffer-file-name' es una variable de Elisp y no se puede utilizar en el símbolo del sistema del shell. –

+1

Enlace cruzado con [Pasando variables de Emacs a comandos de shell de minibuffer] (http://stackoverflow.com/questions/10121944/passing-emacs-variables-to-minibuffer-shell-commands) (que es básicamente un duplicado, excepto que las dos preguntas provienen de ángulos notablemente diferentes). – phils

Respuesta

4

No hay ninguna. ¡Pero este es Emacs! Así que aquí:

(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

Se puede usar esta cada vez que el minibuffer espera que escriba algo (advertencia: no funciona con IDO, pero obviamente siempre se puede salir de eso con, por ejemplo, C-x C-f). También puedes usarlo en buffers regulares.

(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

en mi caso, el uso de los emacs 24 versión GUI de homebrew .... veo el nombre de archivo como el 3 artículo disponible desde la esquina inferior izquierda de la (menor) -mode-bar, justo por encima del mini-buffer.

Para ver dónde estoy, con ido-mode solo hago C-x C-f Sin configuración necesaria allí.

Cuestiones relacionadas