linux美化命令行
centos8
安装和配置 Zsh
首先,安装 Zsh:
sudo yum install -y zsh
将默认 shell 更改为 Zsh:
chsh -s $(which zsh)
重新登录以使更改生效,或者使用以下命令启动 Zsh:
zsh
安装和配置 Oh My Zsh
下载并安装 Oh My Zsh:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
安装并启用 Powerline
安装 Powerline 及其字体支持:
sudo yum install -y epel-release
sudo yum install -y powerline powerline-fonts
编辑 ~/.zshrc
文件以启用 Powerline:
vim ~/.zshrc
在文件末尾添加以下内容:
if [ -f /usr/share/powerline/zsh/powerline.zsh ]; then
source /usr/share/powerline/zsh/powerline.zsh
fi
保存并退出,然后重新加载 ~/.zshrc
:
source ~/.zshrc
安装并配置插件
安装 zsh-syntax-highlighting 插件
该插件提供命令语法高亮:
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
安装 zsh-autosuggestions 插件
该插件提供命令自动建议:
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
安装 fzf 插件
该插件提供模糊搜索功能:
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
Ctrl+R开启搜索
安装 zsh-history-substring-search 插件
该插件提供历史记录子字符串搜索:
git clone https://github.com/zsh-users/zsh-history-substring-search ~/.oh-my-zsh/custom/plugins/zsh-history-substring-search
上箭头键(↑):在历史记录中查找当前输入的子字符串的上一条记录
下箭头键(↓):在历史记录中查找当前输入的子字符串的下一条记录
安装 z
插件
克隆 z
插件仓库,用于目录跳转的非常方便的工具:
git clone https://github.com/rupa/z.git /var/local/z
这里不能放在~
目录,有报错,没有目录自己创建
z <部分目录名>
:跳转到包含指定部分目录名的路径中访问频率最高的目录
z -l <部分目录名>
:列出包含指定部分目录名的所有目录,并按访问频率排序
z -c <部分目录名>
:列出包含指定部分目录名的所有目录,并按访问时间排序
配置插件
编辑 ~/.zshrc
文件,添加并启用插件:
vim ~/.zshrc
找到 plugins
这一行,并将其修改为如下内容:
plugins=(
git
zsh-syntax-highlighting
zsh-autosuggestions
zsh-history-substring-search
z
)
在文件末尾添加以下内容以启用 zsh-history-substring-search
和 fzf
:
source ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.oh-my-zsh/custom/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# z 插件配置
source /var/local/z/z.sh
# zsh-history-substring-search configuration
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
# fzf configuration
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
保存并退出,然后重新加载 ~/.zshrc
:
source ~/.zshrc
安装并配置 Powerlevel10k 主题
Powerlevel10k 是一个高性能的 Zsh 主题,提供了丰富的美化选项:
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
编辑 ~/.zshrc
文件,设置 Powerlevel10k 主题:
vim ~/.zshrc
将 ZSH_THEME
行修改为:
ZSH_THEME="powerlevel10k/powerlevel10k"
保存并退出,然后重新加载 ~/.zshrc
:
source ~/.zshrc
第一次启动时,Powerlevel10k 会引导你完成配置,选择你喜欢的配置选项即可
p10k configure # 重新配置主题
Zsh 别名
编辑 ~/.zshrc
文件:
vim ~/.zshrc
添加常用别名:
alias ll='ls -la'
alias gs='git status'
保存并应用更改:
source ~/.zshrc
一键脚本
创建文件zsh-setup.sh
vim zsh-setup.sh
,将下面代码粘贴进来
#!/bin/bash
# 更新系统并安装 Zsh
sudo yum update -y
sudo yum install -y zsh
# 将默认 shell 更改为 Zsh
chsh -s $(which zsh)
# 安装 Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 安装 Powerline 和字体支持
sudo yum install -y epel-release
sudo yum install -y powerline powerline-fonts
# 配置 Powerline
cat >> ~/.zshrc << 'EOF'
if [ -f /usr/share/powerline/zsh/powerline.zsh ]; then
source /usr/share/powerline/zsh/powerline.zsh
fi
EOF
# 安装 zsh-syntax-highlighting 插件
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
# 安装 zsh-autosuggestions 插件
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
# 安装 fzf 插件
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install --all
# 安装 zsh-history-substring-search 插件
git clone https://github.com/zsh-users/zsh-history-substring-search ~/.oh-my-zsh/custom/plugins/zsh-history-substring-search
# 安装 z 插件
git clone https://github.com/rupa/z.git /var/local/z
# 配置插件
cat >> ~/.zshrc << 'EOF'
plugins=(
git
zsh-syntax-highlighting
zsh-autosuggestions
zsh-history-substring-search
z
)
source ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.oh-my-zsh/custom/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# z 插件配置
source /var/local/z/z.sh
# zsh-history-substring-search configuration
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
# fzf configuration
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
EOF
# 安装 Powerlevel10k 主题
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
# 配置 Powerlevel10k 主题
sed -i 's/ZSH_THEME=".*"/ZSH_THEME="powerlevel10k\/powerlevel10k"/' ~/.zshrc
# 重新加载 Zsh 配置
source ~/.zshrc
# 提示用户重新登录以使更改生效
echo "Please log out and log back in to apply the changes."
chmod +x zsh-setup.sh
赋予执行权限
./zsh-setup.sh
执行两次脚本,再执行source ~/.zshrc
恢复为bash
echo $SHELL #查看当前命令行
which bash # 查看bash所在目录
chsh -s /bin/bash # 后面目录为你上述命令执行结果,切换命令行
ubuntu
和centos基本一致,将yum换成apt安装命令即可
一键脚本也是将yum换成apt