r/commandline • • Jun 18 '13

You Need A 'Go' Command

http://www.matthewhughes.co.uk/you-need-a-go-command/
7 Upvotes

30 comments sorted by

18

u/dave4420 Jun 18 '13

If the cd fails for some reason, you will often want to do an ls to find out why, but you won't always want that to be of the current directory. So go should not always ls.

go() {
    cd "$1" && ls
}

1

u/haddada Jun 19 '13

I did the same thing but I called mine cdls. Good to know I'm not the only person who thinks this way.

1

u/DanielFGray Jun 19 '13 edited Jul 11 '13

I use cdl, but mine passes $2 to ls which is pretty handy

1

u/haddada Jun 19 '13

So "cdls <dir> <pattern>" ? I like that idea but I don't know if I would use the second argument very often.

2

u/DanielFGray Jun 19 '13

mine is

function cdl { cd "$1"; ls $2 }

the $2 is optional, and will only support a single group of options, like -lhA

3

u/dave4420 Jun 19 '13

Support more than a single group of options with

cdl() { cd "$1" && ls "${@:2}" ; }

?

1

u/z-brah Jul 10 '13

Why don't you use 'cd' as the function name ?
Wy function is:

cd() {  
    builtin cd $@ && ls -CF  
}

6

u/Boojum Jun 18 '13

But I've already got one!

alias go="xdg-open"

8

u/ChoHag Jun 18 '13

This is incredibly inefficient. Why write two separate commands just to change your directory and list its contents?

3 keystrokes is not incredibly anything.

4

u/eikenberry Jun 18 '13

In ZSH you can just add this to your zshrc file...

autoload -U add-zsh-hook
add-zsh-hook -Uz chpwd (){ ls; }

3

u/CaptSpify_is_Awesome Jun 18 '13

You could alias this in bash as well, but I'm guessing there's a lot of times when you want to cd without running ls. Having a separate command let's you do that

4

u/sharplikeginsu Jun 18 '13

If you're worried about this kind of time-saving, you should check out the z command.

Tracks your most used directories, based on 'frecency'. After a short learning phase, z will take you to the most 'frecent' directory that matches ALL of the regexes given on the command line.

So if you're often going to ~/Desktop/, z will notice and just typing

    $ z desk

will take you to the right place. z desk;ls is 2 fewer characters than cd desktop, if we're counting.

1

u/[deleted] Jun 19 '13

What package is this in?

1

u/sharplikeginsu Jun 19 '13

Sorry, replied to the wrong comment. 'z' above links to https://github.com/rupa/z.

It's not in any packages as far as I know; it's a single file, I keep it in my dotfiles repo.

1

u/[deleted] Jun 19 '13

Cool, I found the archlinux packages, they're under z-git, and there's also a similar package called autojump.

6

u/[deleted] Jun 18 '13

What about go command from go programing language? :-)

1

u/Rainfly_X Jun 18 '13

That's why I would take dave4420's version and rename it "cdls".

1

u/[deleted] Jun 19 '13

But that's so obvious what it does, why would I use that!?

1

u/adam_ay Jul 04 '13

That saves you 1 keystroke. I get the point, but... really?

(It saves you the second <ENTER>.)

EDIT:

1st Case:

cd <somedir> <ENTER> ls <ENTER>

2nd Case:

cdls <somedir> <ENTER>

1

u/Rainfly_X Jul 04 '13

It saves you an awkward keystroke in the middle of what ought to be a cohesive command. That's the value for me. But I see your point. "cl" might be an acceptable improvement depending on personal taste.

1

u/[deleted] Oct 14 '13

Or alias it to cls.

3

u/ryobiguy Jun 18 '13

Why should a command call "go" do anything but go? Making a "go" command print a directory makes it seem like it is misnamed.

3

u/oknowton Jun 19 '13

I have my own 'go' command, aliased to 'g', that works quite a bit differently. I took Zachary Wasserman's go command idea just a little bit further.

The 'go' command that I use tries to do the right thing:

  • If the argument is a text file according to file), open it in a text editor

  • If that file is read only, offer to open it up as root

  • If that isn't appropriate, pass it to xdg-open

  • If the argument is a directory, cd to it

  • and if all else fails, pass it to autojump (I'm using fasd instead now)

It does the right thing for me most of the time. My most common problem is intending to open an html file in my browser, but having it end up opening in a Emacs instead.

5

u/keito Jun 18 '13

Am I missing something here? Instead of creating a function to change directory and list contents of a folder, why not sack the function off entirely, skip the cd step and just list the contents of the folder?

cd ~/Music && ls && cd -

becomes

ls ~/Music

7

u/sysop073 Jun 18 '13

Because you probably want to be in that directory; the thought is that you're cd-ing to the directory for some reason, and while you're at it you might as well get the file list, not that you need the file list so while you're at it you might as well cd

1

u/ChoHag Jun 18 '13

I carry out this cd-then-ls act a lot. Invariably I realise I didn't need to cd at all so I'm getting myself out of this habit.

1

u/Nesman64 Jun 18 '13

I often find that the ls returns several directories, and I might want to know what's in them. That being the case, it's easier to ls them from their parent than it is from their grandparent.

1

u/ChoHag Jun 18 '13

Ctrl-P, or Up.

$ ls -F /foo/bar/     # Tab completion adds the /
baz/  bing/
$ <ctrl-p>bing/
go
$ cat <alt-dot>go
bingo!
$

4

u/anatolya Jun 18 '13 edited Jun 18 '13

the author didn't even know about quoting problems and writes an ambitious blog post. people should really stop writing about topics in which they're newbies at.

ok, maybe not stop writing, but they should keep it to themselves.

1

u/sharplikeginsu Jun 19 '13

The 'z' above is a link to the github directory. It's a single file I just have checked out with my dotfiles.