-7

I never use my bin or pkg golang dirs. So when I do this:

cd go i'd rather BASH assume I mean cd go/src

how can I tell bash do this everytime?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Andrew Arrow
  • 4,248
  • 9
  • 53
  • 80
  • 1
    Write an alias `cdgo` to mean `cd /home/you/go/src`. – Savior Apr 11 '16 at 16:56
  • 2
    This question is about bash, belongs on SuperUser and is just a terrible idea in general. You wan to alias cd because you're to lazy to type two more keystrokes to finish the path? – evanmcdonnal Apr 11 '16 at 16:59
  • that's like telling a race car driver, you wanna shave 200 milliseconds off your time for each lap? You're just a lazy race car driver. – Andrew Arrow Apr 11 '16 at 17:01
  • @Pillar: That's not a comment, it's an answer - and a good one! – Component 10 Apr 11 '16 at 17:01
  • Pillar's answer is not good. I'd have to remember to type a whole new thing. I want to just do a normal cd and have it auto-matically read my brain and put me where I wanna be. – Andrew Arrow Apr 11 '16 at 17:02
  • Well, I, too, actually against such "micro-optimizations". But I'd say that supposedly Andrew is hardly a seasnoed developer yet, and he'll certainly develop a more down-to-Earth perspective to such things ;-) As of now he's supposedly learned a bit on how shell works so why not after all? ;-) – kostix Apr 11 '16 at 17:20
  • years and years of cd'ing into java com dot blah dot this dot that has ruined me I guess. I long someday to be a seasnoed developer. – Andrew Arrow Apr 11 '16 at 17:47
  • 3
    What I don't understand is what good cd'ing into `src/` will do? I've been writing Go for about as long as anyone, and I've never has occasion to cd directly into the GOPATH src directory. Seems like just setting [CDPATH](https://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Variables) would be much more useful, and not break any builtin functionality. – JimB Apr 11 '16 at 19:00
  • i use vim. so I like being in src to type vim foo.go – Andrew Arrow Apr 11 '16 at 20:50
  • There should not be any .go files in your GOPATH/src directory. – JimB Apr 11 '16 at 21:01
  • there are not. but there might be a directory will a file called foo.go in a directory called bar, so the full thing I type is vim bar/foo.go from src. then I might type vim kangaroo/something.go next, also from src. – Andrew Arrow Apr 12 '16 at 03:12

1 Answers1

1

Put

function cd()
{
    test $# -eq 0 && return 0
    if [ `basename $1` == "go" ]; then
        builtin cd "$1/src"
    else
        builtin cd "$@"
    fi
}

in your ~/.bashrc.

Note that you won't be able to use command-line options when cd-ing into your Go directory because ideally we'd process only the last argument passed to cd but you can't easily do this in a POSIX shell (and bash).

kostix
  • 51,517
  • 14
  • 93
  • 176