30 lines
		
	
	
		
			No EOL
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			No EOL
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
<p>A quick note that I thought might be interesting to document.</p>
 | 
						||
<p>As befits a shell written in elisp, eshell is extremely easy to modify to your use.  There are a few packages floating around that add autojump/fasd/z type functionality to eshell. These let you jump quickly to any previously visited directory. As it turns out it’s very easy to implement a more powerful version of this by piggybacking off of <a href="https://github.com/karthink/consult-dir">consult-dir</a>.</p>
 | 
						||
<p>Here’s a <strong>z</strong> command that lets you jump to any previously visited directory, as well as any bookmark, project or emacs-wide recent directory:</p>
 | 
						||
<video controls="controls" loop="loop" width="700">
 | 
						||
<source src="https://karthinks.com/img/eshell-z.mp4" type="video/mp4" />
 | 
						||
Your browser does not support the video tag.
 | 
						||
</video>
 | 
						||
<p>It’s a single function:</p>
 | 
						||
<div class="highlight"><pre><code class="language-emacs-lisp">(<span style="color: #a2f;">defun</span> <span style="color: #b8860b;">eshell/z</span> (<span style="color: #a2f;">&optional</span> <span style="color: #b8860b;">regexp</span>)
 | 
						||
    <span style="color: #b44;">"Navigate to a previously visited directory in eshell, or to
 | 
						||
</span><span style="color: #b44;">any directory proferred by </span><span style="color: #b8860b;">`consult-dir'</span><span style="color: #b44;">."</span>
 | 
						||
    (<span style="color: #a2f;">let</span> ((<span style="color: #b8860b;">eshell-dirs</span> (<span style="color: #b8860b;">delete-dups</span>
 | 
						||
                        (<span style="color: #00a000;">mapcar</span> <span style="color: #b8860b;">'abbreviate-file-name</span>
 | 
						||
                                (<span style="color: #b8860b;">ring-elements</span> <span style="color: #b8860b;">eshell-last-dir-ring</span>)))))
 | 
						||
      (<span style="color: #a2f;">cond</span>
 | 
						||
       ((<span style="color: #a2f;">and</span> (<span style="color: #b8860b;">not</span> <span style="color: #b8860b;">regexp</span>) (<span style="color: #a2f;">featurep</span> <span style="color: #b8860b;">'consult-dir</span>))
 | 
						||
        (<span style="color: #a2f;">let*</span> ((<span style="color: #b8860b;">consult-dir--source-eshell</span> <span style="color: #666;">`</span>(<span style="color: #a2f;">:name</span> <span style="color: #b44;">"Eshell"</span>
 | 
						||
                                             <span style="color: #a2f;">:narrow</span> <span style="color: #b44;">?e</span>
 | 
						||
                                             <span style="color: #a2f;">:category</span> <span style="color: #b8860b;">file</span>
 | 
						||
                                             <span style="color: #a2f;">:face</span> <span style="color: #b8860b;">consult-file</span>
 | 
						||
                                             <span style="color: #a2f;">:items</span> <span style="color: #666;">,</span><span style="color: #b8860b;">eshell-dirs</span>))
 | 
						||
               (<span style="color: #b8860b;">consult-dir-sources</span> (<span style="color: #00a000;">cons</span> <span style="color: #b8860b;">consult-dir--source-eshell</span>
 | 
						||
                                          <span style="color: #b8860b;">consult-dir-sources</span>)))
 | 
						||
          (<span style="color: #b8860b;">eshell/cd</span> (<span style="color: #00a000;">substring-no-properties</span>
 | 
						||
                      (<span style="color: #b8860b;">consult-dir--pick</span> <span style="color: #b44;">"Switch directory: "</span>)))))
 | 
						||
       (<span style="color: #800;">t</span> (<span style="color: #b8860b;">eshell/cd</span> (<span style="color: #a2f;">if</span> <span style="color: #b8860b;">regexp</span> (<span style="color: #b8860b;">eshell-find-previous-directory</span> <span style="color: #b8860b;">regexp</span>)
 | 
						||
                            (<span style="color: #00a000;">completing-read</span> <span style="color: #b44;">"cd: "</span> <span style="color: #b8860b;">eshell-dirs</span>)))))))
 | 
						||
</code></pre></div><p>Plus <code>consult-dir</code>, of course.</p>
 | 
						||
<p>Note that if you provide input to the command instead, <em>e.g.</em> <code>z doc</code>, it will jump directly to the last matching directory you visited in eshell, <em>i.e.</em> <code>~/Documents/</code> or something like it.</p>
 | 
						||
<p>The idea is simple, and it should be trivial to write something similar if you’re a Helm or Ivy user, as well as to write one for the comint mode <code>M-x shell</code>.</p> |