5

在 VIM 中,%调用 shell 命令时可以使用 One 来指示当前文件名。任何人都可以指出我在文档的方向上显示 emacs 中的等价物吗?

4

3 回答 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)
于 2012-06-29T17:57:19.900 回答
1

每当 minibuffer 要求您输入某些内容时,您都可以使用它(警告:不适用于 ido,但显然您总是可以使用例如 Cx Cf 摆脱这种情况)。您也可以在常规缓冲区中使用它。

(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)
于 2012-06-29T21:12:02.493 回答
0

在我的例子中,使用来自 homebrew 的 emacs 24 gui 版本......我看到文件名是(次要)模式栏左下角的第三个项目,就在迷你缓冲区的上方。

要查看我在哪里,使用 ido-mode 我只是在C-x C-f那里不需要配置。

于 2013-08-15T18:35:59.297 回答