9

I would like to alias my ls command, but there is a previously defined alias for it which I believe gets invoked first before my definition! I am using .zshrc to define my alias, and here is what I did:

alias ls="ls --classify --almost-all --group-directories-first"

And here is the alias command output:

$ alias | grep "ls="
ls='ls --color=tty'

Is there any possible actions to see where that another alias has been defined? Or overwrite it with the new one?

Note: I use ZSH, and I've already searched for it in .profile and other related files I knew might help. If there is any file I need to check, comment please.

2
  • I've also tried grep -RIF 'ls --color=tty' ~ and with the output, I believe .oh-my-zsh/lib/theme-and-appearance.zsh is the file causing this.
    – Farhad
    Dec 15, 2021 at 20:02
  • 1
    unalias ls, and add --color=tty to your alias, OR change the name of your alias to fls. Check with type ls to see what ls means to your shell.
    – waltinator
    Dec 15, 2021 at 21:04

1 Answer 1

16

The alias command overrides a previous alias by the same name. So generally, if you want your aliases to override the ones defined by a zsh framework, put them at the end of your .zshrc. The same goes for other things such as function definitions, variable assignments, key bindings, etc. Generally speaking, the latest definition wins.

An edge case of “latest definition wins” is that it's only the latest definition for the same kind of object. In particular, aliases, functions and external commands are different kinds of objects, and for example defining a function after defining an alias of the same name does not override the alias. You need to call unalias first. (Also note that the syntax foo () { … } when foo is an alias expands the alias — make sure to unalias first, or use the function foo { … } syntax.)

Occasionally a setting can be overridden at runtime through hook functions. For example frameworks to help with version control often change variables and possibly aliases or functions when entering or leaving a version-controlled directory. When this happens, you have to modify the existing hook or run your own hook after the one you don't fully like.


If you can't figure out where an alias is set, run script -c 'zsh -x', then type exit. This creates a file called typescript with a log of all the commands that zsh runs during its initialization. In that file, search for alias ls=. (It could also be something like alias foo=bar ls=whatever.)

You must log in to answer this question.

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