`z` tracks your most used directories. After a short learning phase, it will take you to the directory, based on its usage frequency and a hint you give it on the command line. Say I am often cd'ing into /var/www - then after a while I can just type `z ww`.
I love projects like this. They so elegantly show what using the right tool for the job can do for you, in terms of code simplicity and conciseness. Doing this in, say, Perl or Python or Ruby, would be entirely possible, and folks who code exclusively in those languages might assume it would be easier in a "more powerful" language...but Bash has so many nice little built-ins that make it really concise and portable. One file, no modules, and it does exactly what it's supposed to do.
I think it's illustrative that the developer went through a Python-based version on the path to building z.
I want to be able to preview the path or directory name that z will navigate to. Something like the way the webkit developer console autocompletes an object, method, or property in light gray color. Or it could be displayed in the terminal title bar. Is that possible?
I first was excited by title "Don't waste your time by cd-ing in the terminal", but then it just turned out to be a blog post about making cd'ing quicker. If you want to boost your productivity, my advice is to stop cd'ing altogether.
I see a lot of people -- particularly vi users -- cd'ing back and forth through a large directory tree. I usually tell them to get a terminal emulator that lets you easily manage many terminals open. Open one per directory you want to operate in, for instance. Learn how to switch back and forth between the different shells.
But most importantly, don't quit the program to switch back and forth between directories and files. Learn to use your editor of choice properly: how to view directory listings, how to switch back and forth, etc. Vim can do this just fine btw. The choice of tool here doesn't matter so much. Just pick one and learn it. This applies to your choice of terminal emulator, shell, editor, etc.
Open one per directory you want to operate in, for instance.
This is a common source of headache when using the terminal as an IDE. Not only is it easy to get lost in a sea of terminal-tabs, but it's also quite cumbersome to restore the state of 5+ tabs after a shutdown or disconnect. Even more so when GNU screen enters the mix.
What I'd really like to have is a terminal that can attach to a remote GNU screen and display the screen-windows as local tabs.
Also, pushd and popd. And "cd -". And ctrl-Z and fg to jump out of your editor for a moment. And dragging dirs from the Finder (or whatever) to the terminal.
a terminal emulator which has a session sasving feature built in to restore terminal tabs with their working directories is: termit
find it on github.
lua scripting api included :)
Yet another time saver: if you need to execute only a single command in another directory, use:
(cd /path; command)
This will cd to /path, run command, but return you to your original working directory. This works because the parens create a sub-process, and the cd command only affects that sub-process.
I prefer (cd /path && command), since that way if the cd fails for some reason (like you've typo'd the path), it won't still attempt to run the command.
You can also use the "dirs" command (I think part of bash as well) that tells you the state of the stack. If you set the -v option, you can get the depth of different directories, and you can just do pushd +N to jump to the specific directory.
Just to help sell it some more: autojump basically watches where you cd and builds a model (of some sort) that guesses where you want to go based on a few characters.
For instance, I have a project called "structured-prediction" deep in some folder hierarchy, and I can just type "j stru" or "j pred" or even "j uct" and it goes to the directory.
The best part is that it figures this out automatically. You don't have to remember to bookmark anything.
I've also wondered to myself why there isn't a terminal program with a directory tree by the side so you could just click on the directory that you want to be in, instead of ls -cd-tab-blah-^H-tab-^M. It would also a have a list of favorite and most-recently-used directories.
Because clicking a mouse takes you away from the keyboard .. a context switch if you like, which added up over time slow you down when you have work to do.
Another built-in worth knowing about is CDPATH.[1] I find that setting a sane CDPATH and bash-completion makes cd-ing anywhere I go regularly pretty trivial - just a few letters and a few TABs and I'm good to go.
Thank you for this awesome little script.
However I found out that bashmarks doesn't work with folders which have whitespaces in the name.
For example:
cd /Users/username/Library/Application\ Support
s app_support
works great but if you do this it won't work:
g app_support
I opened an issue on GitHub and after that I tried to fix it on my own. I never wrote a bash script and I'm really proud to have fixed this problem on my own.
Here are changes I made:
# save current directory to bookmarks
touch ~/.sdirs
function s {
cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
mv ~/.sdirs1 ~/.sdirs
escaped_path=${PWD/ /\\ }
echo "export DIR_$1=$escaped_path" >> ~/.sdirs
}
# jump to bookmark
function g {
source ~/.sdirs
path=$(eval $(echo echo $(echo \$DIR_$1)))
# replace whitespaces with "\ " for escaping
escaped_path=${path/ /\\ }
cd_eval="cd $escaped_path"
eval $cd_eval
}
Hope this helps you guys like it helped me.
And if there is a way to do this in an more elegant way, please let me know. This would help me to improve my none existing bash skills :D
Edit: I opened up a fork and commited all my changes to this repo. I also opened a pull request and I hope my fix will get accepted.
Maybe it's just because I run a lot of the same commands and don't do a lot of development from the terminal, but I avoid cding at all usually and just type the full path. It svaes time over multiple sessions thanks to ctrl+r, and the commands in my history work no matter what dir I'm in.
I'm always plugged in to my server with tramp and I've got multiple projects all bookmarked. It makes hopping around real easy, makes it feel like a browser more than an editor.
I use the following scripts, both by Petar Marinov, they've saved me an enormous amount of keystrokes. One replaces CTRL-R history search, the other makes a much friendlier replacement for pushd and popd.
I have usually a few dirs that i go back and forth frequently when coding, and I simply set them to my env. I add the following line to ~/.bashrc
export lib='/path/to/my/lib'
And 'cd $lib' will take me there. Very simple, and caters for most of my cd needs. Don't over-complicate things
I use "m1=`pwd`" and then "cd $m1" for example. No setup required, although admittedly slightly more typing and the need to quote directories with spaces in them (rare for Unix sysadmin tasks for which I'm using a shell in the first place).
mtrn|15 years ago
`z` tracks your most used directories. After a short learning phase, it will take you to the directory, based on its usage frequency and a hint you give it on the command line. Say I am often cd'ing into /var/www - then after a while I can just type `z ww`.
SwellJoe|15 years ago
I think it's illustrative that the developer went through a Python-based version on the path to building z.
webXL|15 years ago
stevelosh|15 years ago
It's become so ingrained in my fingers that whenever I switch to a new shell it's the first thing I port over:
https://github.com/sjl/z-zsh
https://github.com/sjl/z-fish
dekz|15 years ago
orlandu63|15 years ago
alanh|15 years ago
1. Go to the directory in question
2. Type "save nm" where "nm" is any short name for the directory, like "blog" or "blg"
3. From now on, type "cd nm" whenever you want to go there.
No session restart required, even.
I copied the code that enables this workflow from http://dotfiles.org/~jacqui/.bashrc (or was it https://gist.github.com/117528)
The idea is discussed here: http://hints.macworld.com/article.php?story=2002071600512379...
sjs|15 years ago
hyperbovine|15 years ago
anon_d|15 years ago
unknown|15 years ago
[deleted]
huyegn|15 years ago
This is my blog.
FWIW, I've actually switched away from using the script mentioned in the link posted by OP and have moved towards using an improved version below.
http://www.huyng.com/bashmarks-directory-bookmarks-for-the-s...
This new version has 3 commands:
philikon|15 years ago
I see a lot of people -- particularly vi users -- cd'ing back and forth through a large directory tree. I usually tell them to get a terminal emulator that lets you easily manage many terminals open. Open one per directory you want to operate in, for instance. Learn how to switch back and forth between the different shells.
But most importantly, don't quit the program to switch back and forth between directories and files. Learn to use your editor of choice properly: how to view directory listings, how to switch back and forth, etc. Vim can do this just fine btw. The choice of tool here doesn't matter so much. Just pick one and learn it. This applies to your choice of terminal emulator, shell, editor, etc.
moe|15 years ago
This is a common source of headache when using the terminal as an IDE. Not only is it easy to get lost in a sea of terminal-tabs, but it's also quite cumbersome to restore the state of 5+ tabs after a shutdown or disconnect. Even more so when GNU screen enters the mix.
What I'd really like to have is a terminal that can attach to a remote GNU screen and display the screen-windows as local tabs.
erikrose|15 years ago
holygoat|15 years ago
thet|15 years ago
cldwalker|15 years ago
* http://www.skamphausen.de/cgi-bin/ska/CDargs
* http://micans.org/apparix/
* http://github.com/joelthelion/autojump
* http://github.com/rupa/z
* http://github.com/flavio/jump
Also, the title is misleading. I thought it was actually a commandline tool that removed the need to cd most of the time, like lightning: http://tagaholic.me/2010/04/08/lightning-speed-for-your-shel...
medwezys|15 years ago
julian37|15 years ago
semanticist|15 years ago
technomancy|15 years ago
substack|15 years ago
And there's a wikipedia entry for this even: http://en.wikipedia.org/wiki/Pushd_and_popd
miratrix|15 years ago
bretthopper|15 years ago
Some helpful aliases to manage them:
alias 1='cd -1'
alias 2='cd -2'
alias 3='cd -3'
alias 4='cd -4'
alias 5='cd -5'
alias 6='cd -6'
alias 7='cd -7'
alias d='dirs -v'
alias h='history'
alias j='jobs'
Just one of the many reasons to use ZSH.
jrockway|15 years ago
What shell doesn't have a directory stack?
ringm|15 years ago
sjs|15 years ago
nolite|15 years ago
kevinburke|15 years ago
dlwh|15 years ago
For instance, I have a project called "structured-prediction" deep in some folder hierarchy, and I can just type "j stru" or "j pred" or even "j uct" and it goes to the directory.
The best part is that it figures this out automatically. You don't have to remember to bookmark anything.
lars512|15 years ago
askedrelic|15 years ago
jongraehl|15 years ago
aheilbut|15 years ago
A weekend project, perhaps...
old-gregg|15 years ago
xd|15 years ago
l0nwlf|15 years ago
alias tree="find . -name .svn -prune -o -name .DS_Store -prune -o -name .git -prune -o -print | sed -e 's;[^/]*/;|___;g;s;___|; |;g'"
It shows your directory listing in the form of a tree. Easier to visualize.
narkee|15 years ago
pbhjpbhj|15 years ago
Krusader also has a built in console pane.
Not strictly a terminal as the console is running over KDE over X, ..., but works for me.
jfr|15 years ago
xd|15 years ago
Will give it a shot however, good effort.
gnubardt|15 years ago
l0nwlf|15 years ago
telemachos|15 years ago
[1] http://caliban.org/bash/#bashtips
moe|15 years ago
Having "cd foo" potentially take you to an entirely different location can be dangerous, to say the least.
coenhyde|15 years ago
$ to coenhyde.com
orlandu63|15 years ago
> alias to='cd ~/Sites/'
unknown|15 years ago
[deleted]
nevinera|15 years ago
A bookmarking system seems like overkill to solve this problem.
oemera|15 years ago
For example:
works great but if you do this it won't work: I opened an issue on GitHub and after that I tried to fix it on my own. I never wrote a bash script and I'm really proud to have fixed this problem on my own.Here are changes I made:
Hope this helps you guys like it helped me. And if there is a way to do this in an more elegant way, please let me know. This would help me to improve my none existing bash skills :DEdit: I opened up a fork and commited all my changes to this repo. I also opened a pull request and I hope my fix will get accepted.
GitHub fork: https://github.com/Oemera/bashmarks
Thanks
Ă–mer
noibl|15 years ago
wanderr|15 years ago
pieceofpeace|15 years ago
Groxx|15 years ago
In the meantime, this kicks the pants off separate Finder + Terminal action: http://decimus.net/DTerm/
sayemm|15 years ago
I'm always plugged in to my server with tramp and I've got multiple projects all bookmarked. It makes hopping around real easy, makes it feel like a browser more than an editor.
sever|15 years ago
http://geocities.com/h2428/petar/bash_acd.htm
http://geocities.com/h2428/petar/bash_hist.htm
highly recommended.
charlieroot|15 years ago
2. Use real shell like ksh, where history search actually works
3. cd $OLDPWD is occasionally helpful. Occasionally.
Kids...
ajslater|15 years ago
iclelland|15 years ago
clu3|15 years ago
unknown|15 years ago
[deleted]
bluishgreen|15 years ago
rlpb|15 years ago
adsr|15 years ago
netghost|15 years ago
Instead of trying to remember where all your libraries are located, it will just open them up for you. Great for trying to debug misbehaving code.
philc|15 years ago
I've been using this for a few years and it's been a joy.
bherms|15 years ago
ie: hwst - cd /usr/Brad/Desktop/Dropbox/Brad/howas.it/repos/howasit_alpha/ etc...
slowpoison|15 years ago
Jach|15 years ago
pentarim|15 years ago
jesstaa|15 years ago
Nick_C|15 years ago
unknown|15 years ago
[deleted]
bretthellman|15 years ago