News for hackers. Stuff that matters.
Re:OS X Command Line Utilities by eschaton on Thursday May 15, 2014 @03:30AM (#7748212)
What always surprises me is that so many don't know or use the directory stack commands, pushd and popd. I'll admit I was also ignorant of them until something like 2005, but once I learned of them I switched and never looked back. Now I can't see someone write or type "cd" without a little bit of a cringe.
Re:OS X Command Line Utilities by twic on Thursday May 15, 2014 @08:30AM (#7749021)
When i first found out about pushd and popd, i loved them, and used them all the time. These days, i never use them.

The way i work now is that i have a bunch of terminal tabs open. Each tab has a shell which pretty much just sits in a single directory. If i need to switch to a different directory, i switch tabs. A nice effect of this is that each tab builds up a recent shell history specific to the directory it sits in; the shell in the source directory has a history full of build commands, the shell in the installation directory has a history full of start and stop commands, and so on.

This works because i generally use a fairly small (<10) set of different directories, and i don't move between directories much as part of a single task. Partly, this is because i use qualified paths rather than cd'ing around everywhere. I am happy writing (mostly, tab-completing):

  vi lib/awesomeapp/dns/client.rb
Whereas a colleague of mine would always do:

  cd lib
  cd awesomeapp
  cd dns
  vi client.rb
He might get a lot of mileage out of pushd/popd. But for me, it just optimises something i don't spend a lot of time doing.
Re:OS X Command Line Utilities by Reich on Thursday May 15, 2014 @11:28AM (#7749980)
I have the same workflow for switching between tabs, although on Mac the default keyboard shortcut for switching Terminal tabs (Cmd + Shift + '[' or ']') was a little too cumbersome. I swapped it out with (Cmd + '[') which by default will take you to the next Terminal, not tab. I move left to right more, so it made more sense for me.

Easy enough to change in system preferences, though. And with using Vim NERDtree, or some other directory plugin, it makes sitting at the top level directory of your project, with multiple projects in multiple tabs, pretty easy.

Are you on Mac? If so, have you done anything to make tab-switching easier/faster? I'm interested because I only recently set mine up this way, and am always open to more efficient methods.

Re:OS X Command Line Utilities by ripter on Thursday May 15, 2014 @12:39PM (#7750551)
I'm on OSX using iTerm2. Cmd-1, Cmd-2, etc are how I switch between tabs. I keep specific things at specific numbers so it's easy to move around.

For example, the project I'm working on is always tab 1, pianobar (terminal pandora client) is always tab 4.

This is really nice because it's also how I switch tabs in Chrome. So both Chrome and iTerm have my current work on tab 1.

Re:OS X Command Line Utilities by twic on Thursday May 15, 2014 @06:56PM (#7752752)
I am on Mac, and i have to confess i just use the trackpad to click on the tab like some sort of caveman.

Although, now ripter has taught me that ⌘1 etc select tabs (i also use iTerm), i hope i'll switch to using that.

Re:OS X Command Line Utilities by cannam on Thursday May 15, 2014 @04:27AM (#7748359)
A related trick is to use parentheses to create a subshell, in the case where you have a fixed command sequence you want to execute in another directory but you don't want to stay there:

  $ pwd
  /tmp
  $ mkdir -p a/b/c
  $ ( cd a/b/c ; pwd )
  /tmp/a/b/c
  $ pwd
  /tmp
Re:OS X Command Line Utilities by Debilski on Thursday May 15, 2014 @07:20AM (#7748792)
This is probably the thing I’m missing the most in fish shell.
Re:OS X Command Line Utilities by ggreer on Thursday May 15, 2014 @04:10AM (#7748319)
Many people love them, but I never got the hang of pushd and popd. These days, I use a CDPATH and z[1]. I also use zsh, which supports handy little things such as cd - (cd to previous directory).

If you haven't tried it, I highly recommend z. It's impossible to Google for, but it's oh-so-convenient.

1. https://github.com/rupa/z

Re:OS X Command Line Utilities by bronson on Thursday May 15, 2014 @04:15AM (#7748329)
'cd -' is implemented by the cd command using the OLDPWD environment variable. It works in all shells.
Re:OS X Command Line Utilities by ggreer on Thursday May 15, 2014 @04:31AM (#7748370)
I did some digging and it turns out I had autopushd set. No wonder I didn't understand the usefulness of pushd/popd; I was already using them.

Also, by "cd -" I meant cd -[tab], cd -1, cd -2, etc.

Re:OS X Command Line Utilities by mokkol on Thursday May 15, 2014 @07:57AM (#7748900)
Thanks! didnt know the tab feature.
Re:OS X Command Line Utilities by zwegner on Thursday May 15, 2014 @04:56AM (#7748433)
cd is a shell built-in, not an external command. It has to be, since it changes the directory of the shell process, which can't be done by a child process. So shells do have to implement this specially.
Re:OS X Command Line Utilities by jdkenney on Thursday May 15, 2014 @01:28PM (#7750883)
Since no one has mentioned it and it would seem to be not well known, you can also cd to previous directory in bash:

cd ~-

Re:OS X Command Line Utilities by pilif on Thursday May 15, 2014 @08:45AM (#7749061)
In ZSH, you can

    setopt autopushd
to have cd work like pushd.

And

    setopt pushdignoredups
is probably helpful too as it will not push the same directory multiple times on the stack.
Re:OS X Command Line Utilities by juanre on Thursday May 15, 2014 @04:22AM (#7748347)
I tend to use "cd -" instead, which goes to the previous directory. Coupled with searching the command line history (C-r in the default emacs-like mode of bash, ESC-/ if you have set vi-like mode with "set -o vi") you can move around quite fast and with little thought.
Re:OS X Command Line Utilities by sigterm on Thursday May 15, 2014 @03:38AM (#7748234)
uhh... I have known pushd and popd for quite a long time, but never realized that pushd can be used in place of cd. So what I have been doing all the time is:

pushd .

cd some_dir

Re:OS X Command Line Utilities by pling on Thursday May 15, 2014 @03:52AM (#7748275)
I've been using Unix machines and pushd since the mid 1990s and just learned this as well. All those wasted years...

Some examples here:

http://en.wikipedia.org/wiki/Pushd_and_popd

Edit: change to desktop Wikipedia URL.

Re:OS X Command Line Utilities by barbs on Thursday May 15, 2014 @04:35AM (#7748375)
Wow. I never knew about those commands. I've been getting by with just plain cd, and "cd -" if I need to go back.
Re:OS X Command Line Utilities by pseudonym on Thursday May 15, 2014 @04:32AM (#7748373)
I've always just relied on zsh to deal with my pushd stack automatically. I don't frequently need to pushd (thanks to tmux's vertical splits), but when I do, it's inevitably something I don't think about until after I've swapped directories.
Re:OS X Command Line Utilities by archagon on Thursday May 15, 2014 @03:55AM (#7748281)
It's nice to alias them to something shorter. Mine are set to "pd" and "qd".