用最少配置打造高效的 Zsh 工作环境

用最少配置打造高效的 Zsh 工作环境

zsh简介

zsh(Z Shell)是一款可用作交互式登录的shell及脚本编写的命令解释器。

zsh 包含了许多 bash 的特性,同时还加入了新的功能,例如拼写检查、更高级的自动补全和脚本兼容性。

从 macOS Catalina(10.15 版本)开始,zsh 成为了 Mac 默认的 shell,取代了之前的默认 shell bash

ubuntu和archlinux的默认shell都是bash,可玩性比较差,为了获取更好的自动补全和语法高亮,建议使用zsh

可以使用echo $SHELL查看当前系统使用的shell。

echo $SHELL

将zsh配置好了(字体,透明度,自动补全,语法高亮等)开发也会事半功倍,从此爱上linux,让linux不再是纯手敲的黑框框!
命令补全
使用fastfetch(仅限archlinux)查看系统相关信息
fastfetch

zsh配置

对于ubuntu用户

sudo apt install git zsh zsh-completions
sudo apt install zsh-autosuggestions zsh-syntax-highlighting
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.zsh/powerlevel10k

zsh-completions:提供了额外的命令行自动补全规则。这些补全规则能够帮助用户更快地输入命令。

zsh-autosuggestions:zsh 的一个插件,可以根据历史命令自动建议可能的命令补全。

zsh-syntax-highlighting:为 zsh 提供了语法高亮功能。

powerlevel10k:是一款为 Zsh(一种 Unix shell)设计的主题,有着良好的用户体验和美观的外观。

powerlevel10k 提供一个内置的配置向导,通过运行 p10k configure 命令即可启动。这个向导通过一系列问题帮助用户选择符合个人偏好的提示符样式和功能。

如果上述zsh-autosuggestionszsh-syntax-highlighting无法下载可以使用git clone到本地

git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/zsh-syntax-highlighting

对于archlinux用户,直接使用pacman包管理器下载

sudo pacman -S zsh zsh-completions zsh-autosuggestions zsh-syntax-highlighting zsh-theme-powerlevel10k
chsh
# 输入密码
# /usr/bin/zsh

注意:不是oh-my-zsh让你可以选皮肤,而是p10k,oh-my-zsh并不是必须!

通过vim ~/.zshrc加入以下内容:

SHHISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
autoload -Uz compinit && compinit
zstyle ':completion:*' menu select

source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source ~/.zsh/powerlevel10k/powerlevel10k.zsh-theme

重启终端,一般现在就可以配置powerlevel10k的主题啦!

如果哪一步配置错了,想重新配置可以使用p10k configure命令再次配置一遍。

p10k configure

如果报错找不到插件的位置可以使用命令ls -R / | grep zsh查看

/usr/share/zsh-autosuggestions:
zsh-autosuggestions.zsh
/usr/share/zsh-syntax-highlighting:
zsh-syntax-highlighting.zsh

再将搜索到的位置填入到~/.zshrc中的

source /xxx/xxx #zsh-autosuggesions路径
source /xxx/xxx #zsh-syntax-highlighting路径
source /xxx/xxx #powerlevel10k主题路径

以下是我为ubuntu配置的zsh的配置文件

# ~/.zshrc
  1 # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.                                                   
  2 # Initialization code that may require console input (password prompts, [y/n]
  3 # confirmations, etc.) must go above this block; everything else may go below.
  4 if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  5   source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
  6 fi
  7 
  8 # Lines configured by zsh-newuser-install
  9 HISTFILE=~/.histfile
 10 HISTSIZE=10000
 11 SAVEHIST=10000
 12 bindkey -e
 13 
 14 # alias
 15 alias ls='ls --color=auto'
 16 alias ll='ls -alh --color=auto'
 17 alias grep='grep --color=auto'
 18 alias git-log='git log --pretty=oneline --all --graph --abbrev-commit'
 19 
 20 # End of lines configured by zsh-newuser-install
 21 bindkey '^[[H' beginning-of-line
 22 bindkey '^[[F' end-of-line
 23 bindkey "^[[1;5C" forward-word
 24 bindkey "^[[1;5D" backward-word
 25 bindkey '^H' backward-kill-word
 26 
 27 # The following lines were added by compinstall
 28 autoload -Uz compinit
 29 compinit
 30 # End of lines added by compinstall
 31 zstyle ':completion:*' menu select
 32 
 33 source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
 34 source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
 35 source ~/.zsh/powerlevel10k/powerlevel10k.zsh-theme
 36 
 37 # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
 38 [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh                                                   

输入chsh,在随后的屏幕中输入zsh,敲回车。

现在你就可以开始使用zsh了。

以下是我为archlinux配置的zsh的配置文件,除了source后的插件路径,其余配置基本相同。

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTSIZE=10000
SAVEHIST=10000
bindkey -e
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '/home/shawn/.zshrc'

# alias
alias ls='ls --color=auto'
alias ll='ls -alh --color=auto'
alias grep='grep --color=auto'
alias git-log='git log --pretty=oneline --all --graph --abbrev-commit'

autoload -Uz compinit
compinit
# End of lines added by compinstall

source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme

# highlight tab menu
zstyle ':completion:*' menu select

bindkey '^[[H' beginning-of-line
bindkey '^[[F' end-of-line
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey '^H' backward-kill-word

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

使修改生效

source ~/.zshrc

为tab的移动提供高亮

# vim ~/.zshrc
zstyle ':completion:*' menu select

# exec zsh

vim相关配置

配置文件第52行:set mouse=a打开后就可以使用鼠标了,但也难以使用先选中,再鼠标中键粘贴的技巧了。

ubuntu的vim默认打开语法高亮 (配置文件第23行:" line enables syntax highlighting by default.)

首先进入vimrc配置文件:

ubuntu是下面这个路径

sudo vim /etc/vim/vimrc

archlinux是

sudo vim /etc/vimrc

当进入相关命令行后,敲击i键,进入insert模式

 " 开启这个就有状态行了
 set nu           "在左侧行号                                                
 " set tabstop      "tab 长度设置为 4
 " 可能因为这个导致使用vim需要两次回车
 " set nobackup     "覆盖文件时不备份
 " 开这个和nu就够了
 set cursorline   "突出显示当前行
 " set ruler        "在右下角显示光标位置的状态行
 " set autoindent   "自动缩进

之后按esc退出编辑模式,:wq保存文件修改即可生效

  • 31
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要在Linux配置zsh,您可以按照以下步骤进行操作: 1. 首先,确保您的系统已经安装了zsh。如果没有,请使用适合您系统的包管理器进行安装。例如,在Ubuntu上,可以运行以下命令安装zsh: ``` sudo apt-get install zsh ``` 2. 安装zsh后,您需要设置zsh为默认的shell。可以使用以下命令将zsh设置为默认shell: ``` chsh -s $(which zsh) ``` 您可能需要输入您的密码来确认更改。 3. 重新登录系统以使更改生效,或者使用以下命令重新加载shell: ``` source ~/.zshrc ``` 4. 现在,您可以通过编辑.zshrc文件来自定义zsh配置。该文件位于您的用户主目录下。使用文本编辑器打开该文件: ``` nano ~/.zshrc ``` 5. 在.zshrc文件中,您可以添加各种配置选项和插件来定制zsh的外观和行为。例如,您可以更改主题、启用自动补全、添加别名等。以下是一些示例配置: - 更改主题:在.zshrc文件中找到`ZSH_THEME`行,并将其设置为您喜欢的主题,例如`ZSH_THEME="agnoster"`。 - 启用自动补全:在.zshrc文件中找到`plugins=(...)`行,并确保其中包含了`zsh-autosuggestions`和`zsh-syntax-highlighting`插件。如果没有,请添加它们。 - 添加别名:在.zshrc文件中找到`aliases`部分,并在其中添加您想要的别名。例如,您可以添加`alias ll="ls -al"`以将`ll`命令映射到`ls -al`。 6. 保存并关闭.zshrc文件后,重新加载配置: ``` source ~/.zshrc ``` 现在,您已经成功配置zsh作为默认的shell,并进行了一些基本的自定义设置。您可以根据个人喜好和需求进一步调整和扩展zsh的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值