# 高级配置 `Starship 功能繁多,有时您必须在编辑 starship.toml` 之外做更多工作才能实现某些效果。 此页面详细介绍了一些在 starship 中使用的高级配置技巧。 ::: warning 本节所述的配置内容可能随 Starship 未来版本的更新而改变。 ::: ## 在 Bash 中自定义预提示和预执行命令 Bash 并没有类似大多数其它 shell 的正式预执行/预命令框架。 因此,很难在 `bash` 中提供完全可自定义的 hook 机制。 然而,Starship 确实能使您有限地在提示符渲染过程中插入自己的函数执行: - 若要在提示符显示之前运行自定义函数,需要定义此函数,然后将函数名赋值给 `starship_reserved_user_func`。 例如,要在提示符之前绘制一枚火箭,您应该写 ```bash function blastoff(){ echo "🚀" } starship_precmd_user_func="blastoff" ``` - 要在一个命令运行前运行自定义函数,您可以使用 [`DEBUG` trap 机制](https://jichu4n.com/posts/debug-trap-and-prompt_command-in-bash/)。 然而,您**必须**在捕捉 DEBUG 信号*之前*启动 Starship! Starship 可以保留 DEBUG trap 的值,但如果该 trap 在 starship 启动后被覆盖,一些功能将会被破坏。 ```bash function blastoff(){ echo "🚀" } trap blastoff DEBUG # 启动 starship *之前* 设置 DEBUG trap eval $(starship init bash) ``` ## Custom pre-prompt and pre-execution Commands in PowerShell PowerShell does not have a formal preexec/precmd framework like most other shells. Because of this, it is difficult to provide fully customizable hooks in `powershell`. 然而,Starship 确实能使您有限地在提示符渲染过程中插入自己的函数执行: Create a function named `Invoke-Starship-PreCommand` ```powershell function Invoke-Starship-PreCommand { $host.ui.Write("🚀") } ``` ## Change Window Title Some shell prompts will automatically change the window title for you (e.g. to reflect your working directory). Fish even does it by default. Starship does not do this, but it's fairly straightforward to add this functionality to `bash` or `zsh`. First, define a window title change function (identical in bash and zsh): ```bash function set_win_title(){ echo -ne "\033]0; YOUR_WINDOW_TITLE_HERE \007" } ``` You can use variables to customize this title (`$USER`, `$HOSTNAME`, and `$PWD` are popular choices). In `bash`, set this function to be the precmd starship function: ```bash starship_precmd_user_func="set_win_title" ``` In `zsh`, add this to the `precmd_functions` array: ```bash precmd_functions+=(set_win_title) ``` If you like the result, add these lines to your shell configuration file (`~/.bashrc` or `~/.zshrc`) to make it permanent. For example, if you want to display your current directory in your terminal tab title, add the following snippet to your `~/.bashrc` or `~/.zshrc`: ```bash function set_win_title(){ echo -ne "\033]0; $(basename "$PWD") \007" } starship_precmd_user_func="set_win_title" ``` You can also set a similar output with PowerShell by creating a function named `Invoke-Starship-PreCommand`. ```powershell # edit $PROFILE function Invoke-Starship-PreCommand { $host.ui.Write("`e]0; PS> $env:USERNAME@$env:COMPUTERNAME`: $pwd `a") } Invoke-Expression (&starship init powershell) ``` ## Enable Right Prompt Some shells support a right prompt which renders on the same line as the input. Starship can set the content of the right prompt using the `right_format` option. Any module that can be used in `format` is also supported in `right_format`. The `$all` variable will only contain modules not explicitly used in either `format` or `right_format`. Note: The right prompt is a single line following the input location. To right align modules above the input line in a multi-line prompt, see the [fill module](/config/#fill). `right_format` is currently supported for the following shells: elvish, fish, zsh. ### 示例 ```toml # ~/.config/starship.toml # A minimal left prompt format = """$character""" # move the rest of the prompt to the right right_format = """$all""" ``` Produces a prompt like the following: ``` ▶ starship on  rprompt [!] is 📦 v0.57.0 via 🦀 v1.54.0 took 17s ``` ## 样式设定 Style strings are a list of words, separated by whitespace. The words are not case sensitive (i.e. `bold` and `BoLd` are considered the same string). Each word can be one of the following: - `bold` - `italic` - `underline` - `dimmed` - `inverted` - `bg:` - `fg:` - `` - `none` where `` is a color specifier (discussed below). `fg:` and `` currently do the same thing, though this may change in the future. `inverted` swaps the background and foreground colors. The order of words in the string does not matter. The `none` token overrides all other tokens in a string if it is not part of a `bg:` specifier, so that e.g. `fg:red none fg:blue` will still create a string with no styling. `bg:none` sets the background to the default color so `fg:red bg:none` is equivalent to `red` or `fg:red` and `bg:green fg:red bg:none` is also equivalent to `fg:red` or `red`. It may become an error to use `none` in conjunction with other tokens in the future. A color specifier can be one of the following: - 标准终端颜色之一:`black`,`red`,`green`,`blue`,`yellow`,`purple`,`cyan`,`white`。 您可以使用可选前缀 `bright-` 来获取明亮版本的颜色(例如,`bright-white`)。 - 一个 `#` 后跟一个六位十六进制数。 这将指定一个 [十六进制 RGB 颜色代码](https://www.w3schools.com/colors/colors_hexadecimal.asp)。 - 0-255 之间的数字。 这将指定一个 [8 位 ANSI 颜色码](https://i.stack.imgur.com/KTSQa.png)。 If multiple colors are specified for foreground/background, the last one in the string will take priority.