我越来越多地使用emacs shell-mode,并且我希望可以改进一些东西:更改目录时的完成.我很乐意使用ido
或projectile-find-dir
为此.
我的工作流程
截至今天,我尽力在 emacs的shell之外,尽可能地使用emacs的强大功能(使用ido访问文件,在项目中查找文件projectile
,探索dired中的树,......).
我不经常这样做.当我在一个不同的项目中工作时,我打开另一个shell缓冲区.但是当我不得不这样做的时候,我真的很想念ido或fasd shell实用程序(它的工作原理,但没有它的完成界面,这对zsh来说很棒,而且没有像使用ido那样强大,可能是https:// github. com/clvv/fasd).
如何在elisp中连接?
我知道我们可以列出一份清单ido-completing-read
;
在shell中,键入会cd ../<TAB>
打开一个新的*Completions*缓冲区.它使用comint-dynamic-completion
,但是如何在elisp列表中获取该列表,而不是在缓冲区中?
谢谢 !
编辑:这是另一种很好的方式来cd到最近访问过的目录,使用fasd实用程序和ido完成:https://gitlab.com/emacs-stuff/fasd-shell/blob/master/README.org
看另一个SO问题.
ps:eshell与某些shell脚本不兼容,我想保持shell模式.
小智 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))))
Run Code Online (Sandbox Code Playgroud)
<tab>
如果当前输入的命令是 ,则在 shell 中点击将提示使用 ido 可用的目录,cd
否则它将回退到默认完成,退出按C-RET
归档时间: |
|
查看次数: |
991 次 |
最近记录: |