在VIM中,当调用一个shell命令时,可以使用
%
来表示当前文件名。请问有人能指导我在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)
每当 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)
要查看我的位置,我只需使用ido-mode执行C-x C-f
。不需要进行任何配置。
buffer-file-name
是一个Elisp变量,不能在shell命令提示符下使用。 - event_jr