8 ms·
Don't waste your time by cd-ing in the terminal
- aheilbut 16y agoI'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. A weekend project, perhaps...
- narkee 16y agoBecause that would require a mouse, and that would require an X server, etc...
- old-gregg 16y agohttp://en.wikipedia.org/wiki/Midnight_Commander http://en.wikipedia.org/wiki/Midnight_Commander
- utku_karatas2 16y agoAlso don't forget to turn on the "lynx like navigation" option. This lets you navigate all the way through arrow keys (no need to use Enter).
- xd 16y agoBecause 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.
- aheilbut 16y agoPerhaps, but it'd be less of a context switch than remembering what directory I'm in and where it was that I wanted to go. It may be that I'm growing old and dull, but I'm better at clicking on things than remembering them.
- l0nwlf 16y agoUnrelated but a nice alias "tree". 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.
- swaits 16y agoNice little hack!
- davidandgoliath 16y agoGorgeous!
- gnosis 16y agoOr, you could just use "tree": http://mama.indstate.edu/users/ice/tree/ http://mama.indstate.edu/users/ice/tree/
- pbhjpbhj 16y agoI use Dolphin sometimes for this reason, so that I can navigate the directory tree/places via mouse but use the console pane as normal. 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 16y agoThere is dolphin, and a few others. http://unix.stackexchange.com/questions/3990/is-there-a-good-combination-of-command-line-and-graphical-file-browser http://unix.stackexchange.com/questions/3990/is-there-a-good...
- kevinburke 16y agoTry autojump, I love it - https://github.com/joelthelion/autojump/wiki https://github.com/joelthelion/autojump/wiki
- dlwh 16y agoJust 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.
- drhodes 16y agoI use this tool all day, everyday. It should be built into bash.
- askedrelic 16y agoAutojump is my vote as well
- jongraehl 16y agoI also prefer autojump.
- lars512 16y agoAgreed, autojump seems far more principled too than the method described in OP.
- substack 16y agoThere's also the pushd and popd commands which are part of bash already. And there's a wikipedia entry for this even: http://en.wikipedia.org/wiki/Pushd_and_popd http://en.wikipedia.org/wiki/Pushd_and_popd
- miratrix 16y agoYou 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.
- dhimes 16y agofunny- I don't see man pages (nor info pages) for pushd, popd, or dirs. What am I missing about the documentation?
- mtrn 16y agoDon't waste your time bookmarking directories, jump right to them ;) https://github.com/rupa/z https://github.com/rupa/z `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`.
- stevelosh 16y agoz is great. I don't know how I lived without it. 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-zsh https://github.com/sjl/z-fish https://github.com/sjl/z-fish
- SwellJoe 16y agoI 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.
- dekz 16y agoI remember seeing this, but forgot entirely to use it. Have been spending a while looking for it again, or something equivalent. Thanks.
- mtrn 16y agoI forgot the url, too. But then I remembered the tagline "z is the new j" - which is enough for google to bring up the repo - even without quotes.
- webXL 16y agoI 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?
- noibl 16y agoI was just at the point of making a bunch of aliases for some long paths, to save my tab key from daily abuse. This is way more elegant. Thanks.
- coenhyde 16y agoI use this function in my bash profile to navigate to my projects from anywhere on the file system. function to { cd ~/Sites/$1/ } eg. cd ~/Sites/coenhyde.com $ to coenhyde.com
- deleted 16y ago[deleted]
- orlandu63 16y agoI would suggest mapping an alias instead of creating a function so that you will still be able to take advantage of parameter completion: > alias to='cd ~/Sites/'
- coenhyde 16y agoAh, much nicer. Thankyou. Edit: On testing it doesn't work. 'to sitename' just goes straight to ~/Sites/. I knew there must have been a reason i used a function instead of an alias.
- orlandu63 16y agoOh, that's right. 'to sitename' becomes 'cd ~/Sites/ sitename' which is not what you want. My apologies.
- xd 16y agoMaybe not as quick .. but `ctrl+r cd` gets me there quick enough and I don't have to remember what numeric shortcut is assigned to which directory. Will give it a shot however, good effort.
- bretthopper 16y agoNo mention of ZSH directory stacks yet? http://www.acm.uiuc.edu/workshops/zsh/dir_stack.html http://www.acm.uiuc.edu/workshops/zsh/dir_stack.html 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 16y agoJust one of the many reasons to use ZSH. What shell doesn't have a directory stack?
- ramen 16y agoWhat's the bash equivalent to zsh's "setopt autopushd"?
- deleted 16y ago[deleted]
- FaceKicker 16y agoI don't know what that is, but does it do more than alias cd='pushd'?
- ramen 16y agoThat's essentially what it does, but if you use the "autocd" option as well, you don't need to type "cd" - you can just use the name of a directory as a command. So, "autopushd" would ensure that these operations use the directory stack also.
- cicero 16y agosh
- nolite 16y agonice stuff. You got your dotfiles on a github?
- Groxx 16y agoA nice idea... I may end up using it, though a lot of my folders are the same name and I do like deterministic behavior. In the meantime, this kicks the pants off separate Finder + Terminal action: http://decimus.net/DTerm/ http://decimus.net/DTerm/
- nevinera 16y agoalias ba='vim ~/.bash_aliases; source ~/.bash_aliases' Just make aliases for all the directories you go to a lot. I have twenty-something different aliases that start with 'cd'. A bookmarking system seems like overkill to solve this problem.
- philikon 16y agoI 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.
- moe 16y agoOpen 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.
- peterwwillis 16y agoSo browsing graphical tabs is quicker for you then using "Ctrl+RightArrow" or "Ctrl+n" or some variation of one of those? Traversing my screen or tmux sessions is usually much faster than using a mouse.
- moe 16y agoTraversing is not the issue here, it's more about having an ad-hoc way to watch things side-by-side by dragging a tab to a window and vice versa, and about having a proper scrollback. Yes, screen, tmux and vim have split window modes, but those are cumbersome to operate (and I'm saying that as a year-long ion3 user) and the scrollback issue has not been addressed by either up to this day. Or, to put it more generally: Terminals are sadly stuck firmly in the 1970s. There has been near zero innovation beyond emulating them in tabbed windows and setting xterm titles. Heck, we're moving backwards. OSX ships with a terminal that doesn't even support 256 colors. ZModem is unheard of except in fairly exotic/old emulators such as Zoc. Support for "advanced" terminal features (double-size fonts, graphics mode) is rare. I spend >8hrs/day inside a terminal. I would happily pay a 3 digit license fee for a modern terminal emulator that adds the features I mentioned and innovates beyond. There's infinite room for innovation by leveraging special ESC-sequences (server tells terminal what to do), drag & drop, integrating with tools like screen, ssh or even building new CLI tools that interface with the terminal in a smart way... I want vim to tell my terminal to display NerdTree in a native side-car widget like TextMate. I want an "open" CLI-command that downloads the target-file from the remote server and displays it locally without me having to futz with scp. I want to drag & drop files onto the server that I'm currently ssh'd into. I want the term to maintain my entire session (including all tabs and remote connections) across reboots. And, yes, I'd like to have my remote screen windows line up neatly as native tabs.
- adsr 16y agoI manage with a combination of pushd/popd, cd - and terminal tabs most of the time.
- rlpb 16y agoI 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).
- bluishgreen 16y agoBack in 2009 I described another version of this here: http://www.commandlinefu.com/commands/view/117/fast-access-to-any-of-your-favorite-directory.#comment http://www.commandlinefu.com/commands/view/117/fast-access-t...
- telemachos 16y agoAnother 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. [1] http://caliban.org/bash/#bashtips http://caliban.org/bash/#bashtips
- moe 16y agoI'd be extremely careful with CDPATH, never use this on an important host. Having "cd foo" potentially take you to an entirely different location can be dangerous, to say the least.
- telemachos 16y agoI'm not sure I follow. There's nothing dangerous about cd-ing to a directory, though of course it could be dangerous to start work in 'foo', thinking it was 'bar'. Most people use a prompt, or the title bar of their shell to protect against that. But either way, I'm not sure how CDPATH makes that danger greater than any other trick that allows you to jump quickly to a directory using a bookmark, popd or the like.
- moe 16y agocd foo rm -f * Removing predictability from the 'cd' command is not a good idea. If you want a smart 'cd' then just call it 'j' or something else. Easier to type, too.
- telemachos 16y agoCertainly a possible scenario, but anyone who cds into any directory (by whatever means) and enters 'rm -f *' without first checking contents deserves what they get.
- moe 16y agoWell, this was just the most graphic example, there are more subtle ways to create a mess. Directory names are far from unique, a misfiring 'cd tmp' or 'cd src' can easily lead to nasty surprises, even without 'rm' ever getting involved at all. And you do know that CDPATH also affects shell scripts, right?
- deleted 16y ago[deleted]
- huyegn 16y agoHey Guys, 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-shell/ http://www.huyng.com/bashmarks-directory-bookmarks-for-the-s... This new version has 3 commands: - "s" for save current directory as a bookmark - "g" for jump to bookmark's directory - "l" for list all bookmarks.
- alanh 16y agoI like this workflow better: 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 http://dotfiles.org/~jacqui/.bashrc (or was it https://gist.github.com/117528 https://gist.github.com/117528) The idea is discussed here: http://hints.macworld.com/article.php?story=20020716005123797 http://hints.macworld.com/article.php?story=2002071600512379...
- sjs 16y agoOr in zsh: setopt cdablevars nm=/path/to/whatever cd nm
- unfletch 16y agoin Bash, change the first line to: shopt -s cdable_vars
- iuguy 16y agoin Bash you don't even need to do that, just do: foo=/etc/ cd $foo
- irfn 16y agoyou can put this in ~/.zshrc for persistent zsh bookmarks touch ~/.zshmarks && source ~/.zshmarks function b() { echo "hash -d $1=\"`pwd`\"" >> ~/.zshmarks && source ~/.zshmarks }
- gnubardt 16y agoAnother time saver is to have a function that calls ls after cd'ing: c(){ cd "$@" && ls;} This has probably saved me days over the years, as I almost always want to list a directory after I change into.
- l0nwlf 16y agoSilly me. I almost always do the same, but never thought of creating a function. Thanks anyways. :D
- netghost 16y agoIf you're a programmer, you may find Qwandry useful: github.com/adamsanderson/qwandry 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.
- Jach 16y agoI like this: https://github.com/MarcWeber/path-selector https://github.com/MarcWeber/path-selector
- julian37 16y agoYet 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.
- technomancy 16y agoAppending ";cd -" on the end will have the same effect.
- semanticist 16y agoI 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.
- julian37 16y agoOf course! I actually do always use double-ampersand for chaining commands in bash, not sure what made me use a semicolon in that example. Thanks for the correction, a small but important distinction.
- sayemm 16y agoInteresting reading this because this is exactly why I love emacs, especially when I came across this tip a while ago: "Emacs: TRAMP + bookmarks = awesome", http://marc-abramowitz.com/archives/2006/03/12/emacs-tramp-bookmarks-awesome/ http://marc-abramowitz.com/archives/2006/03/12/emacs-tramp-b... 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.
- jesstaa 16y agoWhy not just use symlinks for your bookmarks? pushd and popd are also useful.
- Nick_C 16y agoI got bitten with symlinks for bookmarks. They don't change the PWD to the correct one. For example: cd /home/nick ln -s /var/www www cd www echo $PWD ---> /home/nick/www which will break scripts.
- deleted 16y ago[deleted]
- clu3 16y agoI 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
- wanderr 16y agoMaybe 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.
- pieceofpeace 16y agoCtrl-r is what I use too. Usually requires fewer keystrokes than other methods discussed here. And it works for all commands in shell history.
- bretthellman 16y agoi wont
- bherms 16y agoI use textexpander to keep from cd'ing everywhere... Just stick in a text expander snippet for directories I use a lot... ie: hwst - cd /usr/Brad/Desktop/Dropbox/Brad/howas.it/repos/howasit_alpha/ etc...
- cldwalker 16y agoWhy is a home-rolled cd script interesting when there are many more mature, feature-rich cd-tools out there? * http://www.skamphausen.de/cgi-bin/ska/CDargs http://www.skamphausen.de/cgi-bin/ska/CDargs * http://micans.org/apparix/ http://micans.org/apparix/ * http://github.com/joelthelion/autojump http://github.com/joelthelion/autojump * http://github.com/rupa/z http://github.com/rupa/z * http://github.com/flavio/jump 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-shell.html http://tagaholic.me/2010/04/08/lightning-speed-for-your-shel...
- medwezys 16y agoDitto, this one feels like reinventing the wheel when we have the tools you named.
- charlieroot 16y ago1. How about we just learning to type fast(-er) and use file name completion 2. Use real shell like ksh, where history search actually works 3. cd $OLDPWD is occasionally helpful. Occasionally. Kids...
- philc 16y agoAdditionally, try typing fewer characters when you cd in the general case: https://github.com/philc/fuzzycd https://github.com/philc/fuzzycd I've been using this for a few years and it's been a joy.
- pentarim 16y agoI cant remember my bookmarks so CTRL + R, then searching in history most of a time
- sever 16y agoI 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. http://geocities.com/h2428/petar/bash_acd.htm http://geocities.com/h2428/petar/bash_acd.htm http://geocities.com/h2428/petar/bash_hist.htm http://geocities.com/h2428/petar/bash_hist.htm highly recommended.
- oemera 16y agoThank 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. GitHub fork: https://github.com/Oemera/bashmarks https://github.com/Oemera/bashmarks Thanks Ömer
- slowpoison 16y agoThe suggestion about putting "mdump" in .bash_logout, to automatically save all your bookmarks on exiting shell, is really useful.