2

Suppose for example, the following implementation of ll:

alias ls="ls -F -G"         # colourise and display dir,exec,etc. icons
alias ll="ls -l -h -o"      # long-list, use KB,MB,etc. sizes, no group id

Now suppose I use ll, and while usually I'm glad for the lack of (what is usually) clutter, I actually want the group IDs.

Is there any way of 'cancelling' an option, for example something like ll -!o?

More simply put, but without obvious use case, a command like ! here that renders foo -B -!B equivalent to foo?

Tagged because that's what I use, but if a portable solution does exist obviously (?) that's preferred.

  • Cumbersome workaround: You could do C-x a to expand the alias under the cursor, and then delete the unwanted options. – muru Dec 28 '15 at 17:31
  • @muru Do you know how to do that in vi-mode? – OJFord Dec 28 '15 at 17:39
  • I have bindkey -v in my .zshrc, so presumably I'm in Vi mode, and C-x a works for me. – muru Dec 28 '15 at 17:40
  • @muru Oh I thought C-x was the emacs-mode equivalent of <esc> (where obviously a in vi-mode takes us back to insert mode) – OJFord Dec 28 '15 at 17:42
  • @muru It just types the character a or ^A when I try this (according to whether or not I'm still holding ctrl). If in normal mode then C-x seems to have no affect, and a takes me back to insert. What have I got wrong? – OJFord Dec 28 '15 at 17:48
3

In case of ls and some others commands you can add another option(s) which will negate those existing in an alias.

But as a general solution I think the best approach is the following:

  • write an aliased command (do not put space after command)
  • run a widget _expand_alias (M-x _expand_alias)
  • remove unnecessary options and hit enter

You can bind this widget to some key (^Xa by default) if you need this procedure frequently.

  • I don't think -o can be negated. – muru Dec 28 '15 at 17:43
  • As discussion above, for some reason C-x and M-x are having no effect. However, if I bindkey '\C-x\C-a' '\M-x _expand_alias' then I get "No such widget". – OJFord Dec 28 '15 at 18:16
  • @Ollie Ford What do you see after zle -la | grep expand | grep alias? Oh, and you should of course run bindkey '\C-x\C-a' '_expand_alias', without \M-x - this is not a widget name, but a command bindings (meta/alt - x). – jimmij Dec 28 '15 at 18:26
  • I did, but then it says "No such shell function _expand_alias". Grep output is "_expand_alias". – OJFord Dec 28 '15 at 18:28
  • @OllieFord This widget needs to be activated via autoload -U compinit && compinit, but if zle -la sees it then it should work. What do you see if you start new shell with zsh -f and then just run _expand_alias? – jimmij Dec 28 '15 at 20:16

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.