将 Emacs 变量传递给 minibuffer shell 命令 [英] Passing Emacs variables to minibuffer shell commands
问题描述
我可以通过点击 M-! 快速运行 shell 命令.我想做的一件事是对当前文件执行 shell 快速操作.一个例子是通过 perforce 检出文件:
I can run a shell command quickly by hitting M-!. One thing I'd like to do is perform shell quick operations on the current file. An example would be checking the file out through perforce:
M-! p4 编辑缓冲区文件名
RET
(是的,有 perforce 集成,但我对 minishell/变量问题更感兴趣,而不是特定的工作流程)
(Yes there are perforce integrations, but I'm more interested in the minishell/variable problem rather than a specific workflow)
当然,buffer-file-name
变量在命令发送到 shell 之前不会被评估.
Of course, the buffer-file-name
variable is not evaluated before the command is sent to the shell.
有没有一种简单的即时方法来做到这一点?还是我必须推出自定义的 elisp 功能?
Is there an easy on-the-fly way to do this? Or will I have to roll a custom elisp function?
推荐答案
确实使用 C-u M-: 几乎是正确的.我不太确定在 eval-to-shell-argument
中使用 shell-quote-argument
,因为它只适用于字符串,因此无法使用 eval-to-shell-argument
插入数字或符号.您可以尝试以下操作:
Indeed using C-u M-: is almost right. I'm not so sure about using shell-quote-argument
in eval-to-shell-argument
since it only works on strings making it impossible to use eval-to-shell-argument
to insert a number or a symbol. You could try something like:
(defun sm-minibuffer-insert-val (exp)
(interactive
(list (let ((enable-recursive-minibuffers t))
(read-from-minibuffer "Insert: "
nil read-expression-map t
'read-expression-history))))
(let ((val (with-selected-window (minibuffer-selected-window)
(eval exp)))
(standard-output (current-buffer)))
(prin1 val)))
然后使用 (define-key minibuffer-local-map [?M-:] 'sm-minibuffer-insert-val)
将此函数绑定到您的迷你缓冲区中.当然,如果您只想插入缓冲区文件名,那么您的 execute-shell-command-on-buffer
会更简单.
and then bind this function in your minibuffer with (define-key minibuffer-local-map [?M-:] 'sm-minibuffer-insert-val)
.
Of course, if the only thing you ever want to insert is the buffer-file-name, then your execute-shell-command-on-buffer
is simpler.
这篇关于将 Emacs 变量传递给 minibuffer shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!