Emacs is equivalent to VIM `%`?

In VIM, you can use it %to specify the current file name when invoking a shell command. Can someone point Me in the direction of the documentation showing that the equivalent is in emacs?

+5
source share
3 answers

No. But this is Emacs! So here:

(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)
+4
source

You can use this when the minibuffer expects you to type something (caveat: does not work with ido, but obviously you can always exit it, for example, Cx Cf). You can also use it in regular buffers.

(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)
+1
source

, emacs 24 gui homebrew.... 3- () -mode-bar, -.

, , ido-mode, C-x C-f .

0

All Articles