From 6fad82d653457bf63dc3adaa1a6c95499764b2e1 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 19 Jul 2023 09:06:21 -0700 Subject: [PATCH] Add xargs Ignores Alias Substitution By Default as a Unix TIL --- README.md | 3 +- ...s-ignores-alias-substitution-by-default.md | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 unix/xargs-ignores-alias-substitution-by-default.md diff --git a/README.md b/README.md index d257b2a..d644ee5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1326 TILs and counting..._ +_1327 TILs and counting..._ --- @@ -1347,6 +1347,7 @@ _1326 TILs and counting..._ - [Watch This Run Repeatedly](unix/watch-this-run-repeatedly.md) - [Where Are The Binaries?](unix/where-are-the-binaries.md) - [xargs Default Command Is echo](unix/xargs-default-command-is-echo.md) +- [xargs Ignores Alias Substitution By Default](unix/xargs-ignores-alias-substitution-by-default.md) ### Vercel diff --git a/unix/xargs-ignores-alias-substitution-by-default.md b/unix/xargs-ignores-alias-substitution-by-default.md new file mode 100644 index 0000000..218ff79 --- /dev/null +++ b/unix/xargs-ignores-alias-substitution-by-default.md @@ -0,0 +1,33 @@ +# xargs Ignores Alias Substitution By Default + +I have a number of aliases set up in my shell's RC file. For instance, I use +`nvim` as my main editor, but because of muscle memory, I've aliased `vim` to +`nvim`. + +```bash +❯ alias vim +vim=nvim +``` + +So, I was surprised when I ran the following `xargs` command. + +```bash +❯ rg 'some pattern' -l | xargs vim +``` + +It opened the matching files in `vim` rather than `nvim`. + +The reason for this is that `xargs` is a separate function that does not have +an internal concept of aliases that need to be substituted. + +There is, however, a trick built in to `alias` that we can use. By leaving a +trailing space in an alias, we tell the shell to check for an alias +substitution to expand in the following word. + +So, I can alias `xargs` to `'xargs '` and it will respect my `vim` alias. + +``` +❯ alias xargs='xargs ' +``` + +[source](https://unix.stackexchange.com/a/244516/5916)