How can I set `ls -x` as my default command for `ls`?

0
11
Asked By SillyGoose92 On

Hey folks, I'm a bit of a newbie and like to experiment, so please feel free to ask for clarification if something doesn't make sense. I know I can create an alias for `ls`, but I ran into a problem: if `ls` is used elsewhere, it ignores my alias. For instance, I currently have this function in my .zshrc:

cd() {
builtin cd "$@" && ls
}

If I add `alias ls='ls -x'` below it, it won't execute as `ls -x` within my `cd` function. So, I'm wondering if there's a way to make `-x` the default behavior for `ls`, or if I really need to go through and replace every instance of `ls` with `ls -x`. Any advice?

1 Answer

Answered By TechieTommy On

You can redefine `ls` like this:

ls() {
command ls -x "$@"
}

This will allow all calls to `ls` to default to `ls -x`. But honestly, I’m curious why you need it as the default? It's not too hard to hit a few extra keys.

SillyGoose92 -

Thanks, that worked! And about needing it as default—it’s just more convenient for me. I prefer the output with `-x` and don’t want to add it each time.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.