How to remove a parameter set in an alias from command line

0

I have an alias for "ls" set like this:

alias ls='ls -lh'

If at some point I wanted to perform a one-time regular ls without either of those parameters (without l or without h), how would I do that without changing the alias?

I tried ls !l, ls -!l and ls --l but those don't work. Is there a syntax for negating a parameter?

OMA

Posted 2019-11-24T23:26:28.010

Reputation: 1 221

Answers

1

Alias replaces one string with another string. It's a simple replacement before processing the line further. If the alias is

alias ls='ls -lh'

then ls at the beginning of the line will be replaced by ls -lh. Period.

If ls itself provided some syntax for negating an option, so a later option could override what's in front, then you could do this. With some options you can, e.g.:

ls --color=yes --color=no

If ls was aliased to ls --color=yes and you typed ls --color=no, then the actual command would include both options and the typed one would win, because this is how ls parses and understands its options. But you cannot make an option like -l just disappear. If the alias makes it appear then it's there.

Some tools may provide options that contradict each other and the last one wins; or the first one wins. Some tools may provide a way to negate almost any of their options (e.g. --foo vs. --no-foo; --bar vs. --no-bar). It's always up to the tool and there is no widespread convention, as far as I know.

You may not use alias:

\ls
command ls
/bin/ls
"ls"
'ls'

Each of these will not trigger the alias, so you can append your desired set of options.

In Bash you can expand the line before you hit Enter. The default binding is Ctrl+Alt+e. This makes the following possible:

  1. Type ls.
  2. Hit Ctrl+Alt+e, you will see your alias expanded.
  3. Edit the line and remove the unwanted option.
  4. Prepend \, so the alias will not be expanded again after the next step.
  5. Hit Enter to execute.

Other shells may provide similar functionality.

With the particular alias in question this is not very spectacular because you can type the desired command easily by hand. With a complex alias it may be very useful: you edit a bit instead of typing everything anew.

Kamil Maciorowski

Posted 2019-11-24T23:26:28.010

Reputation: 38 429

Thanks for your thorough explanation. I think I'll opt to type \ls when I don't want to display a long list. Losing the -h parameter as well is OK because a short list doesn't include file size anyway. – OMA – 2019-11-26T01:15:26.907