What cd tool do you use, if any (Autojump, j, z, etc)?
I use cd. And on a rare occasion pushd and popd.
A better 'cd'
autocomplete - cd -1, -2, -3 etc in Z shell - Stack Overflow
zoxide: a smarter cd command, inspired by z and autojump, that remembers visited directories and allows you to jump directly to them
Videos
Some I've found:
https://github.com/wting/autojump
https://github.com/rupa/j
https://github.com/rupa/z
I use cd. And on a rare occasion pushd and popd.
One thing is named directories, a feature of zsh:
$ export SEAL=~/projects/contoso/backend/seal # possibly in .zshrc
$ cd ~SEAL
Or simply
$ cd ~S<tab><enter>
Or even
$ ~S<tab><enter>
Another thing is the automatic directory buffer (another zsh feature):
$ cd ~/projects/contoso/backend/dolphin/src/test/java
$ # someting
$ cd -<tab>
-- directory stack --
0 -- /Users/me
1 -- /Users/me/projects
2 -- /Users/me/Downloads
From here it's a couple of <tab> and <enter>.
Plain 'cd' is all well and good - but I find myself re-typing the same old paths time and again as I bop about the file system.
It gets tedious even though TAB completion helps a lot.
For many years I used a pushd/popd wrapper called acd-func - but pushd/popd's stack is local to every session and is ephemeral hence I lost my breadcrumbs every time I logged in or jumped to another terminal window.
So I wrote ccd - it keeps a permanent store of directories that I visit and uses fzf to let me choose one. It also offers the directories under $PWD.
To try it out, install fzf, download and chmod +x ccd to your $PATH and do this:
source <( ccd -x )
Now you have a new 'cd' using fzf and the persistent store of breadcrumbs. If you like it, put that line into your .bashrc, log out and log back in again. All your terminals (and the system console) will see the same persistent store. More details in:
ccd -h
... written in bash, for bash - but it seems to work in zsh too. It's not working in fish yet.
Enjoy! If you like it and decide to use it, feel free to give me an upvote here, for encouragement.
EDIT: pardon the interregnum but I just became a grandad again!! Much excitement chez moi.
thanks to everyone suggesting alternative solutions, it's an interesting discussion to have and I've looked at them all. Some of them (like autojump, z, fasd) are far more ambitious than my 100 lines of bash (excl comments/fluff). Each to his own, of course, I'd encourage folks to try them all out and choose. Even though some of these projects can be adapted in some way to fit my workflow (and are far more impressive than my little script) I'm sticking with my own concoction because:
-
it fits my flow - 'cd' is still 'cd' and I don't have to re-train my fingers. Same with 'cd -' and 'cd --' which work very much like the old acd-func script.
-
perhaps because of that, TAB completion works the same as before
-
it uses fzf which I've grown to like - it really lets me be lazy and vague (ie human) while selecting something. Possibly because it's quite like emacs' fuzzy ido-mode and I'm very into that.
-
... but, despite my vagueness, fzf tells me exactly what directory I've selected before I make the jump - some of the others rely on taking a leap of faith - 'please jump to a directory that looks a bit like <pattern>' It's a bit un-nerving.
-
Alt-C is so cool - I can be halfway through typing a command, realise I'm in the wrong directory, do Alt-C to fix that and go on to complete my command. Perhaps it's just the cuteness of the feature (which comes from fzf) rather than the practicality ... but I'm sure I'll use it one day!!
-
I don't have to think about it - "so I changed directory, should I bookmark it or not?" or more likely: "blast! I wish I'd remembered to bookmark that directory"
-
it's small and simple
-
it's my own bike shed and I think it should be red :-)
As I mentioned, each to his own but thanks for all the suggestions, comments and upvotes.
If you have setopt AUTO_PUSHD in your .zshrc then cd will automatically do a pushd of each directory you change to. Taking the example from ZyX:
$ setopt AUTO_PUSHD
$ mkdir -p 1/2/3/4
$ cd 1
$ cd 2
$ cd 3
$ cd 4
You can see a list of the directories using dirs:
$ dirs -v
0 ~/1/2/3/4
1 ~/1/2/3
2 ~/1/2
3 ~/1
4 ~
To be able to tab complete the list you can use the + and - arguments with cd (<TAB> meaning you hit the tab key):
$ cd +<TAB>
1 -- ~/1/2/3
2 -- ~/1/2
3 -- ~/1
4 -- ~
Or the reverse:
$ cd -<TAB>
0 -- ~
1 -- ~/1
2 -- ~/1/2
3 -- ~/1/2/3
Then just select the number to go to that directory:
$ cd +2
$ pwd
~/1/2
Tab Complete Directories
I always forget the magic sequence to do the following so I updated the answer to explain this part.
The + and - will only take you to the directory, you can't tab complete the path in the stack (i.e. cd -2/<TAB> gives you nothing). To make this work, you can use a tilde (~).
Make some directories in 3 to make this example better.
$ mkdir 3/foo 3/bar 3/baz
Then find the directory in the stack.
$ cd ~+<TAB>
1 -- ~/1/2/3/4
2 -- ~/1/2/3
3 -- ~/1
4 -- ~
Then use tab completion on an entry.
$ cd ~+2/<TAB>
4/ bar/ baz/ foo/
If you use pushd instead of cd, then you can list directories with dirs and cd to old directory with popd. You can also set autopush option to get cd behave much like pushd -q. Here is an example:
setopt pushdsilent # Omit printing directory stack
setopt autopush # Make cd push directories onto stack
setopt pushdminus # Invert meanings of +N and -N arguments to pushd
mkdir -p 1/2/3/4
cd 1
cd 2
cd 3
cd 4
popd # Go to previous directory (3) and remove it from directory stack
pushd - # Go to previous directory (4)
pushd -2 # Go 2 directories back the directory stack (2)
Note that pushd does not remove anything from the directory stack, it only rotates it. See man zshbuiltins for more details.