I'm trying to override 'ls' command to display dotfiles in "dotfiles" directory.

Here is my code.

def _ls():
    if $(pwd).rstrip(os.linesep) == $DOTFILES:
        ls -Ga
        ls -G
aliases['ls'] = _ls

This code goes into an endless loop because _ls function calls ls command and it calls _ls function.

Any ideas?


The infinite alias call shouldn't happen -- would you mind opening up an issue at github.com/xonsh/xonsh/issues?

In the interim, here's a way to structure your alias that will work without any fixes:

def _ls(args):
    args = args[0].replace('-', '') if args else ''
    if $(pwd).rstrip(os.linesep) == $DOTFILES:
        $(which -s ls) @(f"-{''.join(set(args + 'aG'))}")
        $(which -s ls) @(f"-{''.join(set(args + 'G'))}")
aliases['ls'] = _ls

The -s argument to which skips alias resolution, so that returns the path to the actual ls executable. Then the set logic is to make sure arguments aren't duplicated (although I'm not sure that's strictly necessary).

  • Thank you for your answer and I don't mind open issue. – Miyashita Yosuke Jun 10 at 7:32
  • I opened issue - github.com/xonsh/xonsh/issues/3159 – Miyashita Yosuke Jun 10 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.