Skip to content

Commit 5f15096

Browse files
committed
docs(aliases): refactor the ls shadow alias
1 parent 0ec45fb commit 5f15096

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

book/aliases.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ displaying all listed files and folders in a grid.
6666
When replacing commands it is best to "back up" the command first and avoid recursion error.
6767
:::
6868

69+
::: tip Note
70+
In some cases, shadowing commands may cause arguments to no longer exist. This could be seen when aliasing a `do` command.
71+
:::
72+
6973
How to back up a command like `ls`:
7074

7175
```nu
@@ -96,20 +100,32 @@ The recommended way to replace an existing command is to shadow the command.
96100
Here is an example shadowing the `ls` command.
97101

98102
```nu
99-
# An escape hatch to have access to the original ls command
100-
alias core-ls = ls
101-
102-
# Call the built-in ls command with a path parameter
103-
def old-ls [path] {
104-
core-ls $path | sort-by type name -i
105-
}
106-
107-
# Shadow the ls command so that you always have the sort type you want
108-
def ls [path?] {
109-
if $path == null {
110-
old-ls .
111-
} else {
112-
old-ls $path
113-
}
103+
# alias the built-in ls command to ls-builtins
104+
alias ls-builtin = ls
105+
106+
# List the filenames, sizes, and modification times of items in a directory.
107+
def ls [
108+
--all (-a), # Show hidden files
109+
--long (-l), # Get all available columns for each entry (slower; columns are platform-dependent)
110+
--short-names (-s), # Only print the file names, and not the path
111+
--full-paths (-f), # display paths as absolute paths
112+
--du (-d), # Display the apparent directory size ("disk usage") in place of the directory metadata size
113+
--directory (-D), # List the specified directory itself instead of its contents
114+
--mime-type (-m), # Show mime-type in type column instead of 'file' (based on filenames only; files' contents are not examined)
115+
--threads (-t), # Use multiple threads to list contents. Output will be non-deterministic.
116+
...pattern: glob, # The glob pattern to use.
117+
]: [ nothing -> table ] {
118+
let pattern = if ($pattern | is-empty) { [ '.' ] } else { $pattern }
119+
(ls-builtin
120+
--all=$all
121+
--long=$long
122+
--short-names=$short_names
123+
--full-paths=$full_paths
124+
--du=$du
125+
--directory=$directory
126+
--mime-type=$mime_type
127+
--threads=$threads
128+
...$pattern
129+
) | sort-by type name -i
114130
}
115131
```

0 commit comments

Comments
 (0)