8

I've looked into aliases, functions... but I have not come up with anything satisfying. Let me give you a couple of scenarios:

  • 95% of times that I cd into a folder, I follow up with a ls command.
  • 90% of times that I mkdir, I cdmyself into it.

You get the idea.

What would be the "cleanest" way to bind those commands together (or other functionality)?

  • 5
    Why didn't aliases work for you? – Thorbjørn Ravn Andersen Sep 20 '15 at 23:00
  • 2
    So should the cd result of mkdir then tack on the ls? – bmike Sep 20 '15 at 23:10
  • @bmike Well, a newly created directory will be empty, so ls prints nothing. – Random832 Sep 21 '15 at 5:56
  • @Random832 you assume no flags on ls, but more importantly my question will reveal how broken / heavy handed a change is desired. I prefer aliases over functions, but both can attempt what is asked. – bmike Sep 21 '15 at 11:41
  • 1
    @fd0 I am not doubting that aliases did not work. I'm asking why. There might be a simpler solution to the problem. – Thorbjørn Ravn Andersen Sep 21 '15 at 15:31
4

You can put these lines in your .zsrhc or .bashrc

[ -z "$PS1" ] && return
function cd {
builtin cd "$@" && ls -F
}

Result ->

enter image description here

Explanation from this answer:

Earlier in my .bashrc I have: [ -z "$PS1" ] && return, and everything after that line only applies to interactive sessions, so this doesn't affect how cd behaves in scripts.

Further info from this comment:

[ -z "$PS1" ] checks if the $PS (interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The && return part exits from sourcing .bashrc at this point, under these conditions.

Btw, thanks for the question, it's really cool :)

Edit :

Another solution would be to integrate your ls to your prompt; I'm sure that you can do that with OhMyZsh ;)

  • 1
    In general don't use the same name as standard commands as cd ls rm etc as you will need to use these standard commands – Mark Sep 23 '15 at 14:31
  • @Mark I'm agree for 'rm -rf ~/*' ^^ But for a cd command, it's not really dangerous. And Carles want to 'upgrade existing commands', so... – StrawHara Sep 23 '15 at 14:34
  • I have to say that I've used this solution ever since @StrawHara published it and I've had 0 problems. I wanted to provide the "how it went" :) And it's really useful; this is how I use it (.zshrc): i.imgur.com/WHpROIE.png – Carles Alcolea Mar 28 at 4:25
13

I'd tend to make a new command for this. I think it would even be logical to combine them into a single one.

go() {
    if [ -d "$1" ]; then
        cd "$1" && ls
    else
        mkdir -p "$1" && echo "Created directory $1" && cd "$1"
    fi
}
  • I like your idea combining these ones! :-) – Garex Sep 21 '15 at 8:07
  • 5
    This answer is also the cleanest as it doesn't disturb default definition of cd/mkdir when used by scripts – holroy Sep 21 '15 at 9:18
  • 1
    Sure, but what if you decide to start using the Go programming language? :P – arxanas Sep 21 '15 at 15:00
  • @arxanas: You use makefiles? ;-) – DevSolar Sep 21 '15 at 15:36
  • 2
    hmmm I like this one. but if you accidentally mistype the name, you create a new directory. Is it hard to implement a check on a leave (cd /.... or../ or something) and delete the directory if it's empty? I mean if you leave a directory just check if it's empty and if so, delete it. if you don't want that, you can still use cd instead of go – Schwertspize Sep 21 '15 at 16:04
6

I have tried adding things like these to my .bashrc:

cd() {
    command cd "$@"
    command ls
}

mkdir() {
    command mkdir "$@"
    command cd "$@"
}

However, I've found that this can mess up scripts that use the overridden commands, and the option handling can be fragile (for example, if you want to pass -p to the above mkdir command, it's also passed to cd). Better would be just to define aliases with different names (say, c or mcd).

  • 1
    inserting [ -z "$PS1" ] && return at the end of the file, before those functions, should help – Carles Alcolea Dec 8 '18 at 20:19
5

I think functions are the way to go. Something like

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

as an example.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .