nushell icon indicating copy to clipboard operation
nushell copied to clipboard

Adding arguments to an alias with brackets

Open frstrich opened this issue 2 years ago • 2 comments

Question

What I am trying to achieve is:

alias locate = (rg --files | rg $* )

so that I can type "locate foo" into my terminal and get the results from ripgrep. Basically a replacement for find ./ -name foo since find is overwritten by nu and I prefer nu's use-case.

This does not work, because $* is not a valid variable. Sadly the only preset variable I could find in the docs was $in, which misses the point, since it seems to be only used in piping.

Questions:

Is there any documentation on preset variables other than $in? Is there any preset variable for my specific use-case? The brackets needed for functional piping seem to interfere with further arguments. Is there another way to achieve my goal?

Additional context and details

No response

frstrich avatar Feb 26 '23 14:02 frstrich

  1. You can use ^ to indicate you want to use external commands like ^find
  2. I'm not sure what $* means in this context so I'm not sure how to help you. Maybe others know and can help further.

fdncred avatar Feb 26 '23 14:02 fdncred

1. You can use `^` to indicate you want to use external commands like `^find`

Thanks

2. I'm not sure what `$*` means in this context so I'm not sure how to help you. Maybe others know and can help further.

$* as in Bash's "all arguments", aka for the command foo 1 2 3 $* is 1 2 3

frstrich avatar Feb 26 '23 15:02 frstrich

We don't have such a thing as $* for aliases. We do have a ...rest argument for custom commands. I'm not sure it would work for you or not. e.g.

def locate [...rest] {
  rg --files | rg $rest
}

Is this what it's supposed to do? image

fdncred avatar Feb 27 '23 15:02 fdncred

Writing a function instead of an alias didn't even come to my mind. Thank you, it's precisely what i was looking for.

frstrich avatar Feb 27 '23 16:02 frstrich