在 Emacs 中,如何在 shell-command 中插入文件名? [英] In Emacs, how to insert file name in shell-command?

查看:21
本文介绍了在 Emacs 中,如何在 shell-command 中插入文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用 shell-command(默认绑定到 M-!),但我经常对当前正在编辑的缓冲区做一些事情.与其输入缓冲区名称(而且在 shell 命令中没有文件名补全,唉!),最好有一个快捷键,比如 f3,每当我按下这个键时,就为我插入该名称.

问题是我不想全局绑定密钥(我在其他上下文中将 f3 用于其他内容),仅在 shell-command 提示时在 minibuffer 中绑定.编写 lisp 函数来插入当前缓冲区名称很容易,但是我应该修改什么模式的键映射以将键绑定到该函数?

或者,是否有任何 lisp 代码/包在 shell-command 中提供文件名补全,类似于 bash 的做法?我知道普通的 M-x shell 可以完成,但是在 minibuffer 中输入命令的便利性很难放弃;)

这是我想要的,取自 huaiyuan 的回答以及一些受 http://osdir.com/ml/emacs.sources/2002-04/msg00022.html

(define-key minibuffer-local-map[f3] (lambda() (互动)(插入(缓冲区名称(当前缓冲区非迷你)))))(defun current-buffer-not-mini ()"如果当前缓冲区不是 *mini-buffer*,则返回当前缓冲区否则在激活 minibuf 之前返回缓冲区."(if (not (window-minibuffer-p)) (current-buffer)(if (eq (get-lru-window) (next-window))(window-buffer (previous-window)) (window-buffer (next-window)))))

解决方案

(define-key minibuffer-local-map[f3] (lambda() (interactive) (insert (buffer-name))))

正如评论部分和其他地方所指出的,上面的代码片段不起作用.(抱歉,我应该在发布之前测试它.:) 已经发布了一些修复;这是另一个:

(define-key minibuffer-local-map [f3](lambda() (交互式)(插入(缓冲区名称(窗口缓冲区(minibuffer-selected-window))))​​))

关于在发出 shell-command 时完成文件名,也许这个组合会起作用(它在 Emacs 23.0.60 上对我有用):

(需要'shell)(define-key minibuffer-local-map (kbd "C-i") 'comint-dynamic-complete)

I use shell-command a lots (default binded to M-!), but often I'm doing stuff to the buffer I'm currently editing. Instead of typing the buffer name (and no filename completion is available in shell-command, alas!), it'd be nice to have a shortcut key, say f3, to insert that name for me whenever I press the key.

The problem is I don't want to bind the key globally (I use f3 for other things in other context), only in minibuffer when shell-command is prompting. It's easy to write a lisp function to insert the current buffer name, but what mode's keymap should I modify to bind a key to that function?

Alternatively, is there any lisp code/package that provide filename completion in shell-command, similar to how bash does it? I know the normal M-x shell does completion, but the convenience of entering a command in minibuffer is hard to give up ;)

Edit:

here is what I wanted, taken from huaiyuan's answer with some fixes inspired by / stolen from http://osdir.com/ml/emacs.sources/2002-04/msg00022.html

(define-key minibuffer-local-map
  [f3] (lambda () (interactive) 
       (insert (buffer-name (current-buffer-not-mini)))))

(defun current-buffer-not-mini ()
  "Return current-buffer if current buffer is not the *mini-buffer*
  else return buffer before minibuf is activated."
  (if (not (window-minibuffer-p)) (current-buffer)
      (if (eq (get-lru-window) (next-window))
          (window-buffer (previous-window)) (window-buffer (next-window)))))

解决方案

(define-key minibuffer-local-map
  [f3] (lambda () (interactive) (insert (buffer-name))))

Edit:

As pointed out in the comment section and elsewhere, the above code snippet doesn't work. (Sorry, I should have tested it before posting. :) Some fixes have been posted; here is another:

(define-key minibuffer-local-map [f3]
  (lambda () (interactive) 
     (insert (buffer-name (window-buffer (minibuffer-selected-window))))))

Regarding filename completion while issuing shell-command, perhaps this kludge would work (it works for me on Emacs 23.0.60):

(require 'shell)
(define-key minibuffer-local-map (kbd "C-i") 'comint-dynamic-complete)

这篇关于在 Emacs 中,如何在 shell-command 中插入文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆