在Emacs中使用z(跳转)查找目录 [英] Use z (jump around) in Emacs to find directories
问题描述
Z 是一种常用的shell工具,用于跳过常用的目录。它使用freecency作为衡量您打算跳转到哪个目录的指标关键字完成。所以如果我通常光盘到〜/ .ssh
,我可以 z ssh
跳到那里。
Z is a popular shell tool for jumping around commonly used directories. It uses "frecency" as a metric for determining which directory you intend to jump to based on keyword completions. So if I commonly cd to ~/.ssh
, I can z ssh
to jump there.
我的问题是如何获得与Emacs相同的功能?也就是说,我想要使用 ido-find-file
或类似的东西,但只需键入几个字符即可跳转到我想要的目录。希望解决方案可以包含 z
本身,因此它可以利用已经由 z
记录的频率度量。
My question is how can I get the same functionality to work inside Emacs? That is, I want to be able to use ido-find-file
or something similar but only have to type a few characters to jump to the directory I intended. Hopefully the solution can incorporate z
itself so it makes use of the frecency metric already recorded by z
.
推荐答案
我使用z一次,但是我发现 fasd ,它受到autojump,z或v的启发,我发现更强大,如果我记得很好,那是因为:
I used z once but then I found fasd, which is inspired by autojump, z or v, and which I found much more powerful, if I remember well it is because:
- 它不仅可以查找目录,还可以查找文件
- 它可以
cd
一个结果或使用mplayer
或您的编辑器或其他命令 - 完成更好,特别是对于zsh(再次,如果我记得很好)。事实是,我经常使用
d
别名来更改目录。
- it not only finds directories but also files
- it can
cd
a result or usemplayer
or your editor or another command - the completion is better, specially for zsh (again, if I remember well). The thing is, I constantly use the
d
alias to change directories.
无论如何,还有一个emacs包来查找文件: https://github.com/steckerhalter/emacs-fasd
这很酷,但它不像我想要的那样交互。
Anyway, there's an emacs package to find files with it: https://github.com/steckerhalter/emacs-fasd That's cool, but it isn't as interactive as I would like.
编辑:那么我不得不更新包,并且: / p>
edit: then I had to update the package and:
(setq fasd-enable-initial-prompt nil) ;; don't ask for first query but fire fuzzy completion straight away.
还有一个未填写的用例:
There's a still a use case that isn't filled:
我经常使用emacs的shell模式。当我使用我最喜欢的别名 d
,它的工作,但我根本没有完成。在这里,zsh的完成明显缺失。所以我想使用ido完成,例如。我写了一个很容易适应z的功能:
I often use emacs' shell-mode. When I use my favorite alias d
, it works, but I don't have completion at all. Here, zsh's completion is clearly missing. So I would like to use ido completion, for instance. I wrote a little function which you can easily adapt for z:
编辑:完成命令并添加了TAB触发的ido完成。现在键入 d
(d后跟一个空格)。如果它不断变化,如果我设法创建一个小模式,我将发布链接到我的gitlab repo。
edit: finished the command and added ido completion triggered by TAB. Now type d
(d followed by a space). If it keeps changing and if I manage to create a minor mode I'll post the link to my gitlab repo.
编辑:我创建此功能的一种模式: https://gitlab.com/emacs-stuff/fasd -shell / tree / master
edit: I created a mode for this feature: https://gitlab.com/emacs-stuff/fasd-shell/tree/master
;; Use the fasd command line utility to change recently visited directories and more.
(defun fasd-get-path-list (pattern)
"call fasd with pattern and return the list of possibilities"
(s-split "\n" (s-trim (shell-command-to-string (format "fasd -l -R %s" pattern))))
)
(defun fasd ()
"If current shell command is `d something' call fasd"
(interactive)
(let* ((user-input (buffer-substring-no-properties (comint-line-beginning-position)
(point-max))))
(if (and (string= (substring user-input 0 2) "d ")) ;; todo: mapping to use something else than d and change directory.
(progn
;; get what is after "d "
(setq fasd-pattern (buffer-substring-no-properties (+ (comint-line-beginning-position) 2) (point-max)))
(setq fasd-command (concat "cd " (ido-completing-read "cd to: " (fasd-get-path-list fasd-pattern))))
(comint-kill-input)
(insert fasd-command)
(comint-send-input)
))))
;; Use TAB as in normal shell. Now we have even better completion than in zsh !
(define-key shell-mode-map (kbd "<tab>") 'fasd) ;; works like a charm :)
作为一个附注,我不经常使用它,因为我使用 shell-here 和 shell-pop (一个下拉式终端,如gnome的guake)。
As a side note, I don't use it very often because I open shells in the directory of the current buffer with shell-here and shell-pop (a drop-down terminal like guake for gnome).
这篇关于在Emacs中使用z(跳转)查找目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!