Finish Getting started

This commit is contained in:
fishBone000 2024-09-17 07:40:59 +00:00
parent 45fd6f398a
commit c3f1f7748d
No known key found for this signature in database
GPG Key ID: 8B184E316F948E2A
1 changed files with 23 additions and 26 deletions

View File

@ -1,9 +1,9 @@
* 目录 * 目录
- [[#intro][介绍]] - [[#介绍][介绍]]
- [[#getting-started][Getting started]] - [[#开始][开始]]
- [[#telling-zsh-which-function-to-use-for-completing-a-command][Telling zsh which function to use for completing a command]] - [[#让zsh知道用哪个函数补全命令][让zsh知道用哪个函数补全命令]]
- [[#completing-generic-gnu-commands][Completing generic gnu commands]] - [[#补全gnu格式命令][补全gnu格式命令]]
- [[#copying-completions-from-another-command][Copying completions from another command]] - [[#从其它命令复制补全][从其它命令复制补全]]
- [[#writing-your-own-completion-functions][Writing your own completion functions]] - [[#writing-your-own-completion-functions][Writing your own completion functions]]
- [[#utility-functions][Utility functions]] - [[#utility-functions][Utility functions]]
- [[#writing-simple-completion-functions-using-_describe][Writing simple completion functions using _describe]] - [[#writing-simple-completion-functions-using-_describe][Writing simple completion functions using _describe]]
@ -26,24 +26,22 @@ Zsh官方讲解补全函数的文档令人费解而且也没提供多少示
还请公开你所作的任何脚本比如fork这个仓库然后[[id:64bcd501-b0f0-48c7-b8e2-07af708b95ec][pr]])。 还请公开你所作的任何脚本比如fork这个仓库然后[[id:64bcd501-b0f0-48c7-b8e2-07af708b95ec][pr]])。
此外如果你有任何更多补充内容或对此教程的改进,欢迎作出贡献。 此外如果你有任何更多补充内容或对此教程的改进,欢迎作出贡献。
* Getting started * 开始
** Telling zsh which function to use for completing a command ** 让zsh知道用哪个函数补全命令
Completion functions for commands are stored in files with names beginning with an underscore _, and these files should 补全命令用的补全函数储存于名字以下划线“_”起始的文件这些文件应存于$fpath变量所列出的某目录中。
be placed in a directory listed in the $fpath variable. 你可以将下面的代码加入你的~/.zshrc以在$fpath中新增目录
You can add a directory to $fpath by adding a line like this to your ~/.zshrc file:
#+BEGIN_SRC sh #+BEGIN_SRC sh
fpath=(~/newdir $fpath) fpath=(~/newdir $fpath)
#+END_SRC #+END_SRC
The first line of a completion function file can look something like this: 一个补全函数文件的第一行长这个样:
#+BEGIN_SRC sh #+BEGIN_SRC sh
#compdef foobar #compdef foobar
#+END_SRC #+END_SRC
This tells zsh that the file contains code for completing the foobar command. 这行代码表示这个文件含有补全foobar命令的代码。
This is the format that you will use most often for the first line, but you can also use the same file for completing 多数情况下第一行都采用这个格式,但你也可以用同一个文件补全多个不同的函数。
several different functions if you want. See [[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Autoloaded-files][here]] for more details. 查阅[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Autoloaded-files][官方文档]]以了解更多细节。
You can also use the compdef command directly (e.g. in your ~/.zshrc file) to tell zsh which function to use for completing 你也可以直接使用compdef命令(比如在你的~/.zshrc文件)来告诉zsh用哪个函数补全命令
a command like this:
#+BEGIN_SRC sh #+BEGIN_SRC sh
> compdef _function foobar > compdef _function foobar
#+END_SRC #+END_SRC
@ -55,25 +53,24 @@ or if you want to supply arguments:
#+BEGIN_SRC sh #+BEGIN_SRC sh
> compdef '_function arg1 arg2' foobar > compdef '_function arg1 arg2' foobar
#+END_SRC #+END_SRC
See [[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Functions-4][here]] for more details. 查阅[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Functions-4][官方文档]]以了解更多细节。
** Completing generic gnu commands ** 补全gnu格式命令
Many [[https://www.gnu.org/][gnu]] commands have a standardized way of listing option descriptions (when the --help option is used). 很多[[https://www.gnu.org/][gnu]]命令以标准化的方式列出选项描述(使用--help选项时
For these commands you can use the _gnu_generic function for automatically creating completions, like this: 对于这些命令你可以使用_gnu_generic函数自动创建补全比如这样
#+BEGIN_SRC sh #+BEGIN_SRC sh
> compdef _gnu_generic foobar > compdef _gnu_generic foobar
#+END_SRC #+END_SRC
or to use _gnu_generic with several different commands: 或者对多个不同命令使用_gnu_generic
#+BEGIN_SRC sh #+BEGIN_SRC sh
> compdef _gnu_generic foobar goocar hoodar > compdef _gnu_generic foobar goocar hoodar
#+END_SRC #+END_SRC
This line can be placed in your ~/.zshrc file. 你可以把这行代码放进~/.zshrc文件里。
** Copying completions from another command ** 从其它命令复制补全
If you want a command, say cmd1, to have the same completions as another, say cmd2, which has already had 如果你想要一个命令比如cmd1和另一个已有补全的命令比如cmd2)拥有相同的补全,你可以:
completions defined for it, you can do this:
#+BEGIN_SRC sh #+BEGIN_SRC sh
> compdef cmd1=cmd2 > compdef cmd1=cmd2
#+END_SRC #+END_SRC
This can be useful for example if you have created an alias for a command to help you remember it. 比如当你给一个命令创建了一个助记alias的时候会很有帮助。
* Writing your own completion functions * Writing your own completion functions
A good way to get started is to look at some already defined completion functions. A good way to get started is to look at some already defined completion functions.
On my linux installation these are found in /usr/share/zsh/functions/Completion/Unix On my linux installation these are found in /usr/share/zsh/functions/Completion/Unix