Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions zh-CN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ winget install nushell --scope machine

## 文档

- [入门](/zh-CN/book/getting_started.html) 指引你熟悉 Nushell
- [Nu 之异同](/zh-CN/book/coming_to_nu.html) 描述了 Nu 与其他语言和 Shell 的异同之处
- [Nu 基础](/zh-CN/book/nu_fundamentals.html) 是对 Nu 基础知识更详尽、更有条理的描述
- [Nu 编程](/zh-CN/book/programming_in_nu.html) 将 Nu 作为一门编程语言进行描述
- [作为 Shell 的 Nu](/zh-CN/book/nu_as_a_shell.html) 让你深入了解 Shell 环境中的交互功能和可配置性
- [入门](/zh-CN/book/getting_started.md) 指引你熟悉 Nushell
- [Nu 之异同](/zh-CN/book/coming_to_nu.md) 描述了 Nu 与其他语言和 Shell 的异同之处
- [Nu 基础](/zh-CN/book/nu_fundamentals.md) 是对 Nu 基础知识更详尽、更有条理的描述
- [Nu 编程](/zh-CN/book/programming_in_nu.md) 将 Nu 作为一门编程语言进行描述
- [作为 Shell 的 Nu](/zh-CN/book/nu_as_a_shell.md) 让你深入了解 Shell 环境中的交互功能和可配置性

## 社区

Expand Down
24 changes: 12 additions & 12 deletions zh-CN/book/advanced.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# (并不那么)高级的功能
# (没那么)高级

虽然“高级”这个标题可能听起来令人生畏,你可能会想跳过本章,但实际上,一些最有趣和最强大的功能可以在这里找到。
虽然“高级”这个标题听起来可能令人生畏,你可能会想跳过本章,但事实上,一些最有趣、最强大的特性就在这一章里。

除了内置命令,Nushell 还有一个[标准库](standard_library.md)。

Nushell 操作的是 _结构化数据_。
Nushell 操作的是*结构化数据*。
你可以说 Nushell 是一个“数据优先”的 shell 和编程语言。
为了进一步探索以数据为中心的方向,Nushell 包含了一个功能齐全的数据帧处理引擎,使用 [Polars](https://github.com/pola-rs/polars) 作为后端。
如果你想直接在 shell 中高效地处理大量数据,请务必查看[数据帧文档](dataframes.md)。
为了进一步探索以数据为中心的方向,Nushell 内置了一个功能完备的 dataframe 处理引擎,使用 [Polars](https://github.com/pola-rs/polars) 作为后端。
如果你想直接在 shell 中高效处理大量数据,请务必查看 [Dataframes 文档](dataframes.md)。

Nushell 中的值包含一些额外的[元数据](metadata.md)。
例如,这些元数据可以用来[创建自定义错误](creating_errors.md)。
Nushell 中的值带有一些额外的[元数据](metadata.md)。
这些元数据可以例如用来[创建自定义错误](creating_errors.md)。

由于 Nushell 严格的作用域规则,[并行迭代集合](parallelism.md)非常容易,这可以帮助你通过只输入几个字符来加速长时间运行的脚本。
得益于 Nushell 严格的作用域规则,[并行遍历集合](parallelism.md)非常容易,只需敲几个字符就能帮你加速长时间运行的脚本。

你可以使用 [`explore`](/commands/docs/explore.md) 命令[交互式地探索数据](explore.md),也可以通过 `tui` 命令族[构建你专属的终端用户界面](tui.md)(如选择器、对话框和监控仪表盘)。
你可以用 [`explore`](/commands/docs/explore.md) 命令[交互式地探索数据](explore.md),也可以用 `tui` 系列命令[构建你自己的终端用户界面](tui.md),例如选择器、对话框和仪表板。

你可以使用[插件](plugins.md)来扩展 Nushell 的功能。
几乎任何东西都可以成为插件,只要它以 Nushell 理解的协议与 Nushell 通信。
你可以用[插件](plugins.md)扩展 Nushell 的功能。
几乎任何东西都可以成为插件,只要它用 Nushell 能理解的协议与 Nushell 通信。

最后,Nushell 可以作为 [MCP 服务器](mcp.md)运行,使 AI Agent 和其他 Model Context Protocol 客户端能够查询其命令并执行 Nushell 代码。
最后,Nushell 可以作为 [MCP 服务器](mcp.md)运行,这样 AI agent 和其它模型上下文协议客户端就能查询它的命令并运行 Nushell 代码。
185 changes: 138 additions & 47 deletions zh-CN/book/aliases.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,134 @@
# 别名

Nushell 中的别名提供了一种简单的命令调用替换方式(包括外部和内部命令)。这允许你为一个较长的命令(包括其默认参数)创建一个简写名称。
Nushell 中的别名提供了一种简单的命令调用替换方式(包括外部命令和内部命令)。这让你可以为一个较长的命令(包括其默认参数)创建一个简写名称。

例如,让我们创建一个名为 `ll` 的别名,它将展开为 `ls -l`。
例如,我们来创建一个名为 `ll` 的别名,它会展开为 `ls -l`。

```nu
alias ll = ls -l
```

我们可以通过别名来调用它:
现在我们可以调用这个别名:

```nu
ll
```

一旦我们这样做了,就如同我们输入了 `ls -l` 一样。这也允许我们传入标志或位置参数。例如,我们现在也可以这样写:
一旦我们这样做了,就如同我们输入了 `ls -l` 一样。这也允许我们传入标志或位置参数。例如,我们现在还可以这样写:

```nu
ll -a
```

可得到与输入了`ls -l -a`一样的结果。
得到的就相当于输入了 `ls -l -a`。

## 为父命令设置别名

别名也可以指向一个带有子命令的命令,例如 `math` 或 `str`。这样这些子命令也可以通过该别名使用:

```nu
alias m = math
[1 2 3 4] | m sum
# => 10
```

这对拥有许多子命令的插件尤其方便。例如,在执行 `alias pl = polars` 之后,你就可以写 `pl into-df`、`pl select` 和 `pl collect`。

## 列出所有已加载的别名

你可以在 `scope aliases` 和 `help aliases` 中看到所有可用的别名。
你可以在 `scope aliases` 和 `help aliases` 中看到可用的别名。

对某个别名运行 `help` 会显示它展开成什么,随后是被别名指向的那个命令的帮助。例如,为一个小的自定义命令设置别名:

```nu
# Say hello to someone
def greet [name: string] { $"Hello, ($name)!" }
alias hi = greet
help hi
# => Alias for `greet`
# =>
# => Alias: hi
# =>
# => Expansion:
# => greet
# =>
# => Say hello to someone
# =>
# => Usage:
# => > greet <name>
# =>
# => Flags:
# => -h, --help: Display the help message for this command
# =>
# => Command Type:
# => > custom
# =>
# => Parameters:
# => name <string>
# =>
# => Input/output types:
# => ╭───┬───────┬────────╮
# => │ # │ input │ output │
# => ├───┼───────┼────────┤
# => │ 0 │ any │ any │
# => ╰───┴───────┴────────╯
```

当别名指向一个子命令时,对父命令运行 `help` 会以别名自身的名称列出该别名,紧跟在它所运行的子命令之后:

```nu
# Manage notes
def notes [] {}
# Add a note
def "notes add" [text: string] {}
# List notes
def "notes list" [] {}
alias nl = notes list
help notes
# => Manage notes
# =>
# => Usage:
# => > notes
# =>
# => Subcommands:
# => notes add (custom) - Add a note
# => notes list (custom) - List notes
# => nl (alias) - Alias for `notes list`
# =>
# => Flags:
# => -h, --help: Display the help message for this command
# =>
# => Command Type:
# => > custom
# =>
# => Input/output types:
# => ╭───┬───────┬────────╮
# => │ # │ input │ output │
# => ├───┼───────┼────────┤
# => │ 0 │ any │ any │
# => ╰───┴───────┴────────╯
```

内置命令也同样如此:在执行 `alias st = str trim` 之后,`help str` 会在 `str trim` 之后紧跟着列出 `st (alias)`。隐藏命令和别名不会被列出。

## 持久化

为了使你的别名持久化,它必须被添加到你的 _config.nu_ 文件中。通过运行 `config nu` 打开编辑器并插入它们,然后重新启动 nushell。
例如,对于上面的 `ll` 别名,你可以在 _config.nu_ 的任何地方添加 `alias ll = ls -l`。
要使别名持久生效,必须把它们添加到你的 _config.nu_ 文件中:运行 `config nu` 打开编辑器并插入它们,然后重启 nushell。
例如,对于上面的 `ll` 别名,你可以在 _config.nu_ 的任意位置添加 `alias ll = ls -l`。

```nu
$env.config = {
# 主要配置
# main configuration
}

alias ll = ls -l

# 其他一些配置和脚本加载
# some other config and script loading
```

## 在别名中使用管道

请注意,`alias uuidgen = uuidgen | tr A-F a-f`(使 mac 上的 uuidgen 行为与 linux 一致)将无法工作。
请注意,`alias uuidgen = uuidgen | tr A-F a-f`(让 mac 上的 uuidgen 表现得像 linux 上一样)是行不通的。
解决方案是定义一个不带参数的命令,通过 `^` 调用系统程序 `uuidgen`。

```nu
Expand All @@ -52,67 +137,67 @@ def uuidgen [] { ^uuidgen | tr A-F a-f }

更多信息请参阅本书的[自定义命令](custom_commands.md)部分。

或者一个更符合 nushell 风格的内部命令示例:
或者一个更符合 Nushell 风格的内部命令示例

```nu
def lsg [] { ls | sort-by type name -i | grid -c | str trim }
```

在网格中显示所有列出的文件和文件夹。
以网格形式显示所有列出的文件和文件夹。

## 使用别名替换现有命令

::: warning 警告!
替换命令时,最好先“备份”命令,以避免递归错误。
::: warning 谨慎!
替换命令时,最好先“备份”该命令,以避免递归错误。
:::

::: tip 注意
诸如 `if`、`for` 或 `let` 这样的解析器关键字无法被替换。把其中一个用作别名(或自定义命令)的名称会导致 `nu::parser::name_is_keyword` 错误。
:::

如何备份像 `ls` 这样的命令:

```nu
alias core-ls = ls # 这将为 ls 创建一个名为 core-ls 的新别名
alias core-ls = ls # This will create a new alias core-ls for ls
```

现在你可以在你的 nu 编程中使用 `core-ls` 作为 `ls`。你将在下面看到如何使用 `core-ls`。
现在即使 `ls` 本身已经被替换,你也可以在 nu 编程中把 `core-ls` 当作 `ls` 来使用。

你需要使用别名的原因是,与 `def` 不同,别名是位置相关的。因此,在重新定义旧命令之前,你需要先用别名“备份”它。
如果你不备份命令,而是使用 `def` 替换命令,你将会得到一个递归错误。
你需要使用别名的原因是:与 `def` 不同,别名是位置相关的。所以,在重新定义旧命令之前,你需要先用别名“备份”它。
如果你不备份命令,而是用 `def` 替换命令,就会得到递归错误。

```nu
def ls [] { ls }; ls # *不要*这样做!这会抛出递归错误

#输出:
#Error: nu::shell::recursion_limit_reached
#
# × Recursion limit (50) reached
# ╭─[C:\Users\zolodev\AppData\Roaming\nushell\config.nu:807:1]
# 807 │
# 808 │ def ls [] { ls }; ls
# · ───┬──
# · ╰── This called itself too many times
# ╰────
def ls [] { ls }; ls # Do *NOT* do this! This will throw a recursion error
# => Error: nu::shell::recursion_limit_reached
# =>
# => × Recursion limit (50) reached
# => ╭─[repl_entry #1:1:11]
# => 1 │ def ls [] { ls }; ls # Do *NOT* do this! This will throw a recursion error
# => · ───┬──
# => · ╰── This called itself too many times
# => ╰────
```

推荐的替换现有命令的方法是遮蔽(shadow)该命令。
这是一个遮蔽 `ls` 命令的例子。
推荐的替换现有命令的方式是遮蔽该命令,并在新定义中用
`%` 符号调用原始的内置命令。`%ls` 始终运行内置的 `ls`,即使它被
自定义命令或别名遮蔽,也不会产生递归。
下面是一个遮蔽 `ls` 命令的例子。

```nu
# 将内置的 ls 命令别名为 ls-builtins
alias ls-builtin = ls

# 列出目录中项目的的文件名、大小和修改时间。
# List the filenames, sizes, and modification times of items in a directory.
def ls [
--all (-a), # 显示隐藏文件
--long (-l), # 获取每个条目的所有可用列(较慢;列取决于平台)
--short-names (-s), # 只打印文件名,不打印路径
--full-paths (-f), # 将路径显示为绝对路径
--du (-d), # 显示目录的视在大小(“磁盘使用情况”)而不是目录元数据大小
--directory (-D), # 列出指定的目录本身而不是其内容
--mime-type (-m), # 在类型列中显示 mime-type 而不是 'file'(仅基于文件名;不检查文件内容)
--threads (-t), # 使用多个线程列出内容。输出将是不确定的。
...pattern: glob, # 要使用的 glob 模式。
--all (-a), # Show hidden files
--long (-l), # Get all available columns for each entry (slower; columns are platform-dependent)
--short-names (-s), # Only print the file names, and not the path
--full-paths (-f), # display paths as absolute paths
--du (-d), # Display the apparent directory size ("disk usage") in place of the directory metadata size
--directory (-D), # List the specified directory itself instead of its contents
--mime-type (-m), # Show mime-type in type column instead of 'file' (based on filenames only; files' contents are not examined)
--threads (-t), # Use multiple threads to list contents. Output will be non-deterministic.
...pattern: glob, # The glob pattern to use.
]: [ nothing -> table ] {
let pattern = if ($pattern | is-empty) { [ '.' ] } else { $pattern }
(ls-builtin
(%ls
--all=$all
--long=$long
--short-names=$short_names
Expand All @@ -125,3 +210,9 @@ def ls [
) | sort-by type name -i
}
```

你也可以在提示符下输入 `%ls`,以便在它被遮蔽时运行内置的 `ls`。

在 `%` 符号出现之前,函数体改为调用一个备份别名,例如用 `alias ls-builtin = ls` 创建的 `ls-builtin`。这种做法现在仍然有效,但前提是该别名是在 `ls` 仍指向内置命令时创建的。

有关 `%` 符号的更多内容,请参阅[遮蔽内置命令](custom_commands.md#遮蔽内置命令)。
18 changes: 18 additions & 0 deletions zh-CN/book/background_jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ job list
# => ╰────────────╯
```

## 描述任务

为了更容易区分不同的任务,你可以在用 `job spawn --description` 启动任务时给它一个描述,或者稍后用 [`job describe`](/commands/docs/job_describe.md) 修改它。该描述会显示在 `job list` 额外增加的一列 `description` 中:

```nu
let id = job spawn --description "long nap" { sleep 1day }
job describe $id "longer nap"

job list
# => ╭───┬────┬────────┬────────────────┬─────────────╮
# => │ # │ id │ type │ pids │ description │
# => ├───┼────┼────────┼────────────────┼─────────────┤
# => │ 0 │ 3 │ thread │ [list 0 items] │ longer nap │
# => ╰───┴────┴────────┴────────────────┴─────────────╯

job kill $id
```

## 任务挂起

在 Unix 目标(如 Linux 和 macOS)上,Nushell 还支持使用 <kbd>Ctrl</kbd>+<kbd>Z</kbd> 挂起外部命令。当一个正在运行的进程被挂起时,它会变成一个“冻结”的后台任务:
Expand Down
7 changes: 5 additions & 2 deletions zh-CN/book/cheat_sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ module greetings {
}
use greetings hello
hello "world"
# => hello world!
```

从文件导入模块并在当前作用域中使用其环境:
Expand All @@ -488,18 +489,20 @@ export-env {
export def hello [] {
$"hello ($env.MYNAME)"
}
```

```nu
use greetings.nu
$env.MYNAME
# => Arthur, King of the Britons
greetings hello
# => hello Arthur, King of the Britons!
# => hello Arthur, King of the Britons
```

在模块中使用 main 命令:

```nu
# greetings.nu
# salutations.nu
export def hello [name: string] {
$"hello ($name)!"
}
Expand Down
2 changes: 1 addition & 1 deletion zh-CN/book/coloring_and_theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,4 @@ $env.config = {

Reedline(Nu 的行编辑器)样式不使用 `color_config` 键。
相反,每个菜单都有自己的样式需要单独配置。
有关此内容的更多信息,请参阅[专门介绍 Reedline 菜单配置的部分](line_editor.md#menus)。
有关此内容的更多信息,请参阅[专门介绍 Reedline 菜单配置的部分](line_editor.md#菜单)。
Loading
Loading