Emacs对应于VIM的`%`是什么?

5
在VIM中,当调用一个shell命令时,可以使用%来表示当前文件名。请问有人能指导我在emacs中该如何做,有哪些相应的文档可供参考?

1
在Emacs中没有相应的功能。buffer-file-name是一个Elisp变量,不能在shell命令提示符下使用。 - event_jr
1
使用将Emacs变量传递到minibuffer shell命令进行交叉链接(这基本上是一个重复的问题,只是两个问题来自明显不同的角度)。 - phils
3个回答

5
没有一个标准答案。但是这里是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

每当 minibuffer 要求您输入内容时,您都可以使用此功能(注意:不适用于 ido,但您可以通过例如 C-x C-f 来退出)。您也可以在常规缓冲区中使用它。

(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
在我的情况下,我使用从Homebrew安装的Emacs 24 GUI版本...我可以在(次要)模式栏的左下角第3个项目中看到文件名,就在迷你缓冲区上方。

要查看我的位置,我只需使用ido-mode执行C-x C-f。不需要进行任何配置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接