Getting to where you were, terminal navigation all fast-like


Update: modus_pwnens on proggit pointed me to cdbm, which seems to do what I want. Thanks!


Chalk this one up to the category of I’m-sure-this-exists-already-but-I-don’t-know-what-it’s-called UNIX tools.

I work with a lot of terminals. I’m constantly switching between them, switching directories, and closing and opening new ones. If I’m working in some directory (say ~/work/projects/foo/src/bar/blah) in one terminal (call it termA), I often want to open a new terminal (termB) and switch to the same deep directory structure as in termA. I got tired of having to copy and paste, or relying on tab-completion (still slow), so here are three useful bash aliases I whipped up in a couple minutes. I lamely term them the ‘god’ series.

alias saved='echo `pwd` > ~/.savedir'
alias showd='cat ~/.savedir'
alias god='cd `cat ~/.savedir`'

'saved' will save your current directory in whatever terminal you’re using
'showd' will show your current saved directory
'god' will change your current working directory to whatever’s saved in ~/.savedir

In this manner, I can simply type saved in one terminal, open up a handful of new ones, and cd into my saved directory with a single command. I like it, you might too?

Also, if this exists already, I’d like to know what it’s called? I know about pushd and popd already, but those don’t seem to span multiple bash sessions. Is there some setting I can set to make it do this? There’s also autojump, but I’d like something more deterministic and works with infrequently used directories.

  1. #1 by e at May 18th, 2009

    If you’re running inside of GNU screen (and why wouldn’t you be?), typing in `screen’ will open up a new screen window with the same current directory.

  2. #2 by michael at May 18th, 2009

    check out ‘popd’ and ‘pushd’

  3. #3 by michael at May 18th, 2009

    Doh, didn’t read your last paragraph properly :P I like that you can open a bunch of terminals and cd to the right place with one short command, with your solution.

  4. #4 by fire at May 18th, 2009

    zsh has a built-in option called ZHARE_HISTORY. If set, the history will be always in sync between all open zsh sessions.

    the option AUTOPUSHD is also very helpfull.

    and don’t forget `cd -` ;)

    hth

  5. #5 by Bron Gondwana at May 18th, 2009

    Konsole CTRL-SHIFT-N opens a new shell in the current directory by default.

    I also usually have about 10 directories on my system that are “frequent customers”. Generally the root directory of the checkout of something.

    Each of these has an entry in my ~/.bash_aliases file with a two or three letter alias that expands to “cd $projectdir” for that project. The aliases make sense to me, and so I just type “ce” to switch to my cyrus checkout, or “hm” for my work tree (it makes sense in context). “lx” is my linux git repository. Etc, etc.

    Funny how much typing a few tens of lines in a .alias file can save you. Work habits don’t change THAT fast - and every time something annoys you enough, the effort of changing the aliases file is worth it.

    Check the thing into source control as well, and symlink it into your home dir from your regular checkout directory. Then so long as you update your personal repository on whatever machines you use regularly, it will tend to stay up to date. I use “sa” for “source aliases file”, so a quick “4p; svn up; sa; cd” will switch into my personal area, update, re-source the aliases and put me back in my home directory ready to move on. I could probably alias that too if it was done often enough!

  6. #6 by Bron Gondwana at May 18th, 2009

    Oh, if you want to be a bit more advanced about it, have the command instead be something that sources a .rc file that changes the environment to suit the current project, sets up all the aliases that make sense within that context and provides an “exit” command to dump you back to your regular environment. Then you can be in exactly the right space for that particular project in a single command.

    And believe me on that change control thing. If you use more than one computer, maintaining diffs by hand gets old really fast. Add a .shellrc.`hostname -s` and source it from the main one if there are different configs for different machines.

  7. #7 by philip at May 18th, 2009

  8. #8 by Jesse McNelis at May 18th, 2009

    the current working directory propagates to child processes.
    thus,
    for i in `seq 2`; do sh -c “xterm &” ; done

    will launch two xterms at your current directory.
    Make an alias for it.

  9. #9 by Josh at May 18th, 2009

    Jesse:

    You can shorten that up as follows:

    for i in 1 2; do xterm & done

    The ‘&’ acts as a line separator too.

  10. #10 by teabag at May 18th, 2009

    Uhhh second ‘pushd’ ‘popd’ and ‘dirs’…. this is a classic Unix problem already solved.

  11. #11 by Josef A at May 18th, 2009

    e :
    If you’re running inside of GNU screen (and why wouldn’t you be?), typing in `screen’ will open up a new screen window with the same current directory.

    It gets better. According to Google SoC, GNU screen is getting scripting ability. Should be based on this:

    http://repo.or.cz/w/screen-lua.git?a=shortlog;h=refs/heads/lua-scripting

  12. #12 by Ton Tobius at May 20th, 2009

    Re the site title: perhaps you were thinking of the word “broccoli”?

  13. #13 by KrisBelucci at June 2nd, 2009

    Great post! Just wanted to let you know you have a new subscriber- me!

  14. #14 by AndrewBoldman at June 4th, 2009

    Hi, good post. I have been woondering about this issue,so thanks for posting. I’ll definitely be coming back to your site.

  1. No trackbacks yet.

Comments are closed.