Repository: ipython/ipython

Issue:
`ls` magic alias to `ls -F --color`
I have many different profiles used in linux. I use one profile when my terminal does not support any color.
Is there any option to control the `ls` aliases so that it uses `ls -F` instead of `ls -F --color`?

I already used `c.InteractiveShell.colors = 'NoColor'` but it does not affect the `ls` alias


Discussion:
`ls` is also an alias in IPython, you can redefine it interactively to just `ls` with

```
In [1]: alias ls ls
```

use `hist -t ` to see the equivalent Python code:
```
In [2]: hist -t
get_ipython().run_line_magic('alias', 'ls ls')
get_ipython().run_line_magic('hist', '-t')
```

Place the equivalent code into `~/.ipython/profile_default/startup/alias.py` (choose the nname you like) with any other logic you like, like looking at the `$TERM` env or `infocmp` to know whether color is supported or not.

Does that fits your need ? 

If so feel free to close the issue.

@Carreau Yes. This solves my problem. Thank you.

But don't you think it better to set the alias in `core.alias` according to `c.InteractiveShell.colors`? If it is 'NoColor', the `--color` should be remove in defining the aliases. This will avoid confusing other uses like me.
Sure, but that would require someone to code it. Most if not all of the
work is volunteer, so it's mostly 'if someone implement it'.

On Mon, Oct 22, 2018, 21:44 liushapku <notifications@github.com> wrote:

> @Carreau <https://github.com/Carreau> Yes. This solves my problem. Thank
> you.
>
> But don't you think it better to set the alias in core.alias according to
> c.InteractiveShell.colors? If it is 'NoColor', the --color should be
> remove in defining the aliases.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/ipython/ipython/issues/11431#issuecomment-432089915>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAUez2pkTLujJi1pofMU-K5LjkDd8a76ks5unp67gaJpZM4XywgZ>
> .
>

I have a workaround that is to change `AliasManger.init_aliases` in `IPython/core/alias.py` to

    def init_aliases(self):
        # Load default & user aliases
        for name, cmd in self.default_aliases + self.user_aliases:
            if cmd.startswith('ls ') and self.shell.colors == 'NoColor':
                cmd = cmd.replace(' --color', '')
            self.soft_define_alias(name, cmd)

Do you think this an acceptable solution? If yes I can submit the pull request.

I don't quite understand what is a `Configurable` and what does `List(default_aliases()).tag(config=True)` imply. Why should the `default_aliases` be a class data member instead of instance data member? If it is an instance data member, we can easily let the function `default_aliases()` take a parameter `nocolor` and when it is `False` do not set `--color` in the aliases for `ls`.
> Do you think this an acceptable solution? If yes I can submit the pull request.

Yes, that sounds good. 

> I don't quite understand what is a `Configurable` and what does `List(default_aliases()).tag(config=True)` imply. Why should the `default_aliases` be a class data member instead of instance data member?

Don't worry about that, it's the metaclass magic that  allow `c.AliasManger.default_aliases.append(...)/.prepend(...)` to work in the configuration file, and `--AliasManger.default_aliases=...` to work similarly as a command line argument. 

You  can tread `default_aliases` as a instance attribute which is of type list for all purposes.
Get it. Thanks for explanation. I will take some time to digest the details of the configuration system.

Let me create an pull request for this change first.
Do I need to consider the order of the initialization of the `colors` property and the `alias` properties?
Will the configuration system automatically fix the dependence?

Resolving patch:
diff --git a/IPython/core/alias.py b/IPython/core/alias.py
--- a/IPython/core/alias.py
+++ b/IPython/core/alias.py
@@ -204,6 +204,8 @@ def __init__(self, shell=None, **kwargs):
     def init_aliases(self):
         # Load default & user aliases
         for name, cmd in self.default_aliases + self.user_aliases:
+            if cmd.startswith('ls ') and self.shell.colors == 'NoColor':
+                cmd = cmd.replace(' --color', '')
             self.soft_define_alias(name, cmd)
 
     @property


Test patch: