windows下powershell与linux下bash美化教程(使用starship)

starship美化教程

Win11 + Powershell

安装

在命令行使用下面命令安装

cmd 复制代码
# 安装starship
winget install starship

将以下内容添加到 Microsoft.PowerShell_profile.ps1,可以在 PowerShell 通过 $PROFILE 变量来查询文件的位置

复制代码
Invoke-Expression (&starship init powershell)

配置

C:\用户\{user}\.config目录下新建starship.toml文件,可以在starship官网查看官方预设配置,点击主题即可查看配置文件代码,复制到toml配置文件中即可。

需要注意的是,基本上所有的主题都包含了Nerd Font字体中的一些符号支持,因此当符号显示异常的话,先检查是否安装了最新版的Nerd Font字体族。

在排除字体问题后,某些符号显示异常很可能是使用了过时的符号编码,具体可见oh-my-posh美化教程中的Nerd Font部分。

启动终端后第一行空白

因为默认配置中add_newline = true,意味着每次执行完命令会空出一行,防止命令与输出结果之间缺少空行而降低可读性。根据个人喜好,可以将其设置为false以去除终端最上方的空行。

Linux(无root权限) + bash

安装Nerd Font

在linux终端中,可以使用fc-list查看已安装的字体。

若linux上没有可用的Nerd Font字体,而你又恰好没有root权限,则可以在本地主机上下载字体包(.ttf文件即可)后上传至~/.local/share/fonts目录下,若没有此路径,新建即可。

需要注意的是,在上传Nerd Font字体文件时,最好把某个字体的所有格式文件均上传,如light、bold、italic等多个文件。

然后在linux上执行以下命令来刷新字体缓存,检测字体是否可用。

bash 复制代码
fc-cache -vf
fc-list | grep "JetBrainsMonoNLNerdFontMono"

安装starship

starship的github官网上下载最新的starship压缩包,上传到linux用户目录下,解压即可得到starship的可执行文件,将该目录添加到环境变量中,再在~/.bashrc文件的最后添加一行命令即可。

bash 复制代码
# 将starship所在路径添加到环境变量
export PATH=/home/{user}/{starship所在目录}:$PATH
# 使用starship初始化bash
eval "$(starship init bash)"

配置

~/.config路径下新建starship.toml配置文件即可。

我的配置

基于Tokyo Night主题修改,增加了对conda环境的显示支持与命令执行时长的显示支持。

大部分符号均需要在NerdFont下查看。

toml 复制代码
add_newline = true

format = """
[░▒▓](#a3aed2)\
[  ](fg:#090c0c bg:#a3aed2)\
[](fg:#a3aed2 bg:#769ff0)\
$directory\
[](fg:#769ff0 bg:#596db0)\
$git_branch\
$git_status\
[](fg:#596db0 bg:#394260)\
$conda\
$nodejs\
$rust\
$golang\
$php\
[](fg:#394260 bg:#212736)\
$time\
[ ](fg:#212736 bg:#1d2230)\
$cmd_duration\
[](fg:#1d2230)\
\n$character"""

[directory]
style = "fg:#e3e5e5 bg:#769ff0"
format = "[ $path ]($style)"
truncation_length = 7
truncation_symbol = ".../"

[directory.substitutions]
"Documents" = "󰈙 "
"Downloads" = " "
"Music" = " "
"Pictures" = " "

[git_branch]
symbol = ""
style = "bg:#394260"
format = '[[ $symbol $branch ](fg:#769ff0 bg:#596db0)]($style)'

[git_status]
style = "bg:#394260"
format = '[[($all_status$ahead_behind )](fg:#769ff0 bg:#596db0)]($style)'

[conda]
symbol = " "
style = "bg:#394260"
format = '[[ $symbol ($environment) ](fg:#769ff0 bg:#394260)]($style)'

[nodejs]
symbol = ""
style = "bg:#394260"
format = '[[ $symbol ($version) ](fg:#769ff0 bg:#394260)]($style)'

[rust]
symbol = ""
style = "bg:#394260"
format = '[[ $symbol ($version) ](fg:#769ff0 bg:#394260)]($style)'

[golang]
symbol = ""
style = "bg:#394260"
format = '[[ $symbol ($version) ](fg:#769ff0 bg:#394260)]($style)'

[php]
symbol = ""
style = "bg:#394260"
format = '[[ $symbol ($version) ](fg:#769ff0 bg:#394260)]($style)'

[time]
disabled = false
time_format = "%R" # Hour:Minute Format
style = "bg:#212736"
format = '[[  $time ](fg:#a0a9cb bg:#212736)]($style)'

[cmd_duration]
disabled = false
min_time = 500
show_milliseconds = true
style = "bg:#1d2230"
format = '[[ 󰔟 $duration ](fg:#a0a9cb bg:#1d2230)]($style)'
相关推荐
qwer555883 分钟前
linux-----------------库制作与原理(下)
linux·运维·服务器
m0_519523107 分钟前
Linux——UDP/TCP协议理论
linux·tcp/ip·udp
vortex520 分钟前
Bash fork 炸弹 —— :(){ :|:& };:
运维·服务器·开发语言·网络安全·bash
xmweisi0231 分钟前
【Linux】ssh命令 – 安全的远程连接服务
linux·安全·ssh
Pacify_The_North37 分钟前
【进程控制二】进程替换和bash解释器
linux·c语言·开发语言·算法·ubuntu·centos·bash
ephemerals__1 小时前
【Linux】简易版Shell实现(附源码)
linux·运维·chrome
JeffersonZU1 小时前
【shell】shell和shell脚本简介
linux·ubuntu·bash
烦躁的大鼻嘎2 小时前
【Linux】ELF与动静态库的“暗黑兵法”:程序是如何跑起来的?
linux·运维·服务器·c++·vscode·ubuntu
qq21084629532 小时前
【Linux驱动】Linux 按键驱动开发指南
linux·驱动开发
L_cl2 小时前
【NLP 75、如何通过API调用智谱大模型】
linux·服务器·windows