Skip to content

Commit 6fad82d

Browse files
committed
Add xargs Ignores Alias Substitution By Default as a Unix TIL
1 parent cee26f1 commit 6fad82d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1326 TILs and counting..._
13+
_1327 TILs and counting..._
1414

1515
---
1616

@@ -1347,6 +1347,7 @@ _1326 TILs and counting..._
13471347
- [Watch This Run Repeatedly](unix/watch-this-run-repeatedly.md)
13481348
- [Where Are The Binaries?](unix/where-are-the-binaries.md)
13491349
- [xargs Default Command Is echo](unix/xargs-default-command-is-echo.md)
1350+
- [xargs Ignores Alias Substitution By Default](unix/xargs-ignores-alias-substitution-by-default.md)
13501351

13511352
### Vercel
13521353

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# xargs Ignores Alias Substitution By Default
2+
3+
I have a number of aliases set up in my shell's RC file. For instance, I use
4+
`nvim` as my main editor, but because of muscle memory, I've aliased `vim` to
5+
`nvim`.
6+
7+
```bash
8+
alias vim
9+
vim=nvim
10+
```
11+
12+
So, I was surprised when I ran the following `xargs` command.
13+
14+
```bash
15+
❯ rg 'some pattern' -l | xargs vim
16+
```
17+
18+
It opened the matching files in `vim` rather than `nvim`.
19+
20+
The reason for this is that `xargs` is a separate function that does not have
21+
an internal concept of aliases that need to be substituted.
22+
23+
There is, however, a trick built in to `alias` that we can use. By leaving a
24+
trailing space in an alias, we tell the shell to check for an alias
25+
substitution to expand in the following word.
26+
27+
So, I can alias `xargs` to `'xargs '` and it will respect my `vim` alias.
28+
29+
```
30+
❯ alias xargs='xargs '
31+
```
32+
33+
[source](https://unix.stackexchange.com/a/244516/5916)

0 commit comments

Comments
 (0)