Emacs Shell:使用ido更改目录

7
我越来越多地使用emacs shell-mode,但有一些我希望能够改进的地方:在更改目录时完成自动补全。我想使用ido或projectile-find-dir实现这一点。
我的工作流程如下:尽可能在emacs shell之外执行所有操作(使用ido访问文件,在项目中查找文件使用projectile,浏览dired内部的树等)。
我不经常使用cd命令。当我在不同的项目中工作时,我会打开另一个shell buffer。但是,当我必须要使用cd时,我真的很想念ido或fasd shell实用程序(它可以正常工作,但没有zsh的完成接口似乎不那么强大),它不像ido的使用方式可以参考https://github.com/clvv/fasd
如何在elisp中实现这一点?我知道我们可以给ido-completing-read函数传递一个列表;
在shell中,输入“cd ../<TAB>”将打开一个新的Completions buffer。它使用comint-dynamic-completion,但如何将该列表获取到elisp列表中,而不是获取到buffer中?
以下是我的问题:
- 是否可以将该完成列表与ido、projectile、helm或其他内容连接起来? - 如果您能够链接到精确的文档,我会很感激(有很多,很难知道什么对我有用) - 是否已经存在解决方案?
谢谢!
编辑:这是另一种使用fasd实用程序和ido自动补全快速进入最近访问的目录的好方法:https://gitlab.com/emacs-stuff/fasd-shell/blob/master/README.org 请参见另一个SO问题
附注:eshell与某些shell脚本不兼容,因此我希望保留在shell-mode中。

关于您的PS,如果您没有像我这样的许多shell脚本,我会运行M-&进行异步shell命令。 - nymo
1个回答

4

试试这个,它是一个快速而简单的技巧,在某些情况下可能会失败,但通常应该有效。另外,请原谅我的elisp。

(require 'ido)
(require 'cl-lib)
(require 'shell)

(defvar my-dir-selected nil "Flag to indicate that user has selected the directory")

(defun my-filter-cd-input (current-input)
  "Takes current user input for `cd' the a list
    whose car is the 'maximum possible directory path'
    and cdr is remaining string.

    Examples:
    '~/.emacs.d/in => ('~./emacs.d/' 'in')
    '/home/gue' => ('/home/' 'gue')
    '~/../' => ('~/../' '')"
  (let* ((unquoted-input (shell-unquote-argument current-input))
     (components (split-string unquoted-input "/"))
         (directory-parts (butlast components))
         (possible-prefix (car (last components))))
    (list (if (string= possible-prefix "")
              unquoted-input
            (concat (mapconcat 'identity directory-parts "/")
                    (when directory-parts "/")))
          possible-prefix)))

(defun my-complete-directory-name (directory current-input)
  "Prompts user for directories in `directory', `current-input'
    is the string entered by the user till now"
  (let* ((filtered-input (my-filter-cd-input current-input))
         (directory-path (car filtered-input))
         (partial-input (cadr filtered-input))
         (directory-choices (mapcar 'file-name-nondirectory
                                    (condition-case nil
                                        (cl-remove-if-not 'file-directory-p
                                                          (directory-files (concat directory directory-path) t))
                                      ('file-error (list)))))
         (selected-name (ido-completing-read "Directory: "
                                             directory-choices
                                             nil nil partial-input)))
    (comint-delete-input)
    (insert (concat "cd " 
            (shell-quote-argument (concat directory-path selected-name "/"))))))

(defun my-prompt-for-dir-or-fallback ()
  "If current shell command is `cd' prompt for directory
    using ido otherwise fallback to normal completion"
  (interactive)
  (let* ((user-input (buffer-substring-no-properties (comint-line-beginning-position)
                                                     (point-max))))
    (if (and (>= (length user-input) 3)
             (string= (substring user-input 0 3) "cd "))
        (progn 
          (setq my-dir-selected nil)
          (while (not my-dir-selected)
            (my-complete-directory-name default-directory 
                    (buffer-substring-no-properties (+ (comint-line-beginning-position) 3) 
                                    (point-max))))
          (comint-send-input))
      (call-interactively 'completion-at-point))))

(define-key shell-mode-map (kbd "<tab>") 'my-prompt-for-dir-or-fallback)

(add-hook 'ido-setup-hook 'ido-my-keys)

(defun ido-my-keys ()
  "Add my keybindings for ido."
  (define-key ido-completion-map (kbd "<C-return>") (lambda ()
                                                        (interactive)
                                                        (setq my-dir-selected t)
                                                        (ido-exit-minibuffer))))

在Shell中按下<tab>键,如果当前输入的命令是cd,则会提示可用的目录,并使用ido自动完成;否则,它将返回默认完成。要退出,请按C-RET键。

1
嗨@Ehvince,抱歉耽误了,我做了一些更改,现在ido会在使用RET选择目录后持续询问子目录,要完成选择请按C-RET - user2053036
嗨,我能够毫无问题地使用 C-RET,我选择 C-RET 的原因之一是它不会与任何其他绑定冲突。你可以通过执行 C-h k C-RET 命令来检查 C-RET 正在运行的命令吗? - user2053036
太好了,我很高兴能帮助你! - user2053036
我在wikemacs中添加了一个条目:http://wikemacs.org/index.php/Shell#Change_directory_with_ido-mode - Ehvince
@Ehvince 噢,很好。 - user2053036
显示剩余9条评论

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