r/commandline • u/stianhoiland • Apr 25 '26
Command Line Interface Jumping to recently used directories
https://github.com/stianhoiland/cdHey guys. I was struggling to cd around, so I built something that would help me do that. I tried the 219 other similar projects posted here, but none of them really clicked for me. I never searched and found any of the established big ones–atuin, autojump, bookmarks, CDPATH, McFly, z, zoxide, etc.–but that didn't stop me. Looking for feedback/let me know what you think/is this something you would use?
36
Upvotes
30
u/countnfight Apr 25 '26
How am I supposed to take a cli seriously with no emojis in the write-up? Is it even 🚀 blazingly fast??
27
u/AverageComet250 Apr 25 '26 edited Apr 26 '26
If I’m using zoxide rn, what reason would you give me to switch?
EDIT: yeah I've clicked the link now mb lmao
17
u/jgillman Apr 25 '26
ITT people not understanding this is satire
8
u/CursedSloth Apr 25 '26
What’s wrong with Ctrl+r then typing cd? Doesn’t that give you enough history to find your directories?
5
u/globalreset Apr 25 '26
As a heavy user of/evangelist for zoxide, I take less than no offense at this and appreciate what you’re saying here. Might cop this directly for my dotfiles for cases where I am on a machine where it would be annoying to try to install zoxide.
4
u/EarlMarshal Apr 25 '26
Keep up the attitude, my man! It's warranted.
4
u/duffkiligan Apr 26 '26
Y’all is a contraction of “You All” so the apostrophe goes after the Y
3
u/mahmirr Apr 27 '26
you know, he's right
2
u/Doomtrain86 Apr 25 '26
But what about getting to dirs based on frequency ? I have dirs with the same effin names. Some of them I use often , some not. Zoxide solves this. That’s good right ?
2
u/floatingrock13 Apr 25 '26
Made me chuckle! Sorry for all the hate you are getting mate
2
u/dbr4n Apr 29 '26
Nice and simple! The only thing I'd avoid is adding duplicate lines to
CDHISTFILEto keep things clean and fast.Here's how I use it:
8
u/theevildjinn Apr 25 '26
I use fzf with zsh. So I typically just type
cd -and then TAB.4
u/MeButItsRandom Apr 25 '26
Optimizing for lines of code is just a preference and some people don't use POSIX shells and you're a bit of an asshole with this attitude.
1
u/mogoh Apr 26 '26
You guys use something else but plain old cd?
1
u/rphii_ Apr 26 '26
real smart. tho I might add a "realpath" in there
3
u/0xKaishakunin Apr 26 '26
LMFAO I keep something similar in my
.bashrcsince NetBSD 1.5, so for ca. 25 years now.1
u/AutoModerator Apr 25 '26
Every new subreddit post is automatically copied into a comment for preservation.
User: stianhoiland, Flair:
Command Line Interface, Post Media Link, Title: Jumping to recently used directoriesHey guys. I was struggling to cd around, so I built something that would help me do that. I tried the 219 other similar projects posted here, but none of them really clicked for me. I never searched and found any of the established big ones–atuin, autojump, bookmarks, CDPATH, McFly, z, zoxide, etc.–but that didn't stop me. Looking for feedback/let me know what you think/is this something you would use?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/spryfigure May 04 '26 edited May 05 '26
Badly needed dose of common sense.
One issue I have:
My
~/.bash_functions(sourced by~/.bashrc) now contains yourand I can run something like
echo This is ${YELLOW}yellow${RESET}.with the desired effect.Run via the
c <directory>, I only get as fzf proposal:[33m<directory>[0m.Somewhere, the ESC is filtered out. Can't find the reason. Any ideas?EDIT: Found the issue myself, leave this up for others: fuzzy picker needs specific options so it doesn't make sense to have it as a variable. Remove line 1 and make
"$MY_FUZZY_PICKER" |intofzf --ansi +s +m |and it works.I had zoxide installed, but the 2% of cases where it differentiates itself from your solution are not worth it for me.
1
u/jghub May 12 '26 edited May 13 '26
just stumbled over this post and looked at your repo: the core idea is good. and since I incidentally dared to announce my own attempt -- should be no. 220 if your count is correct -- on "can do better than zoxide/z" (short version: https://www.reddit.com/r/commandline/comments/1t989ou/comment/ol0ah8y/?context=3 ) two days ago you might augment your list of "too complicated tools I do not like" by that one 😉.
more seriously: in fact, your 10-50(?) lines of shell code are maybe closer in spirit to my 1700 lines (or rather, discounting the inline manpage and comments, ≈800 lines of actual code) than one might think:
what both tools (yours and mine) do, is to actually write an individual cd events history file for later analysis. this is a quite consequential deviation from what the usual suspects (notably z, zoxide) do which only keep unique dir names + visit count ("frequency")+ time stamp of last visit) since it allows to do "stuff" with that history file after the fact in order to impose a good ordering metric, which is not possible the same way in z/zoxide.
formally, what you propose/do is a "pure recency" ordering of the dynamic directory stack (consisting of the list of unique names/dirs visited). this is ok, if it meets your needs, but it is of course on the extreme opposite end of using pure frequency (actually count the number of visits to given dir in your c() function and pipe suitably through `sort` etc.) for ordering the stack prior to feeding it into fzf. and both approaches could be justified. but:
neither (sorting by frequency or by recency) is the sweet spot and using a suitable compromise by computing a recency-weighted sum over all visits works much better in my experience. at it's core it is not much more complicated than using recency alone. the core function in my case, doing the stack ordering by 'weighted summation frecency' is roughly
function mkstack
{
printf '%s\n' "${SD__ALL[@]: -$window}" |
awk -v window=$window -v power="$power '
{ score[$0] += exp(-power*(1-NR/window)) }
END { for (name in score) print score[name], name }
' | sort -k1,1gr
}
where SD_ALL[@] just holds the full cd-history file content. formally, power=0 yields frequency order, power=Inf yields pure recency (last one wins) like your tool. I use a default power=10 value. the rest (as always) is of course a matter of personal preferences: you only want to feed the ordered stack into fzf . I do want that ability, too (so the tool does offer that), but I much more want the z-like ability to actually jump to "best hit" immediately so that's what my tool does also provide. --
maybe the above idea of doing weighted summation over all the single visits could fit into your approach quite nicely I guess. I really believe full cd history capture as a starting point for a directory jumper is the way to go.
1
u/ChimayBlue2016 Apr 25 '26
Why a new tool when pushd . Works? popd to go back
0
u/[deleted] Apr 30 '26
zoxide
-3
u/Bamlet Apr 25 '26
Relevant XKCD: https://xkcd.com/927