手把手带你从bash无痛切换到zsh,命令行效率轻松翻倍

如果你使用的是bash shell,那么你大概率正在使用某个linux发行版。
目前,大部分linux发行版使用的默认shell仍然是bash
Windows默认使用Powershell
Catalina开始,MacOS已经将默认shell从bash切换到了zsh

什么是shell?

简单说,shell是你与应用程序交互的媒介。
通常情况下,你将你想要使用的应用程序和参数输入到shell,shell在PATH中查找你希望调用的应用程序,对参数进行解析,并传入参数。最终将结果返回给你。

zsh有什么优势?

相比bashzsh有庞大的插件社区和成熟的插件管理框架如oh my zsh,这使得zsh的功能扩展变得极为容易,你可以向管理vim插件一样管理zsh插件。

zsh配置

zsh的全局配置文件位于/etc/zsh.zshc
单用户配置文件位于~/.zshrc

但我不推荐你现在修改任何一个文件,因为我们有更加优雅的配置方式:

oh my zsh!

项目地址: https://github.com/ohmyzsh/ohmyzsh

oh my zsh是当下最流行的zsh插件管理工具,相当于vim中的vim-plug或vundle。

安装oh my zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

如果上面的链接不可用,可以试试

sh -c "$(curl -fsSL https://install.ohmyz.sh/)"

配置oh my zsh

现在查看.zshrc文件,你会发现oh my zsh已经几乎帮你配置好了。

如果你想使用插件,可以找到plugins,在其中添加即可。

这里给出我的.zshrc供你参考。
(请注意其中几个插件最要单独安装,已在注释中指明,安装方式见项目github首页)

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
#ZSH_THEME="robbyrussell"
ZSH_THEME="fino-time"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
 CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
 zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
 ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
    #git
    history-substring-search
    autojump
    autoenv     #auto run .env and .env.leave
    autopep8    #autoformat python code
    catimg
    colored-man-pages
    colorize    #use command ccat to get a colorized cat output
    command-not-found   # give a suggestion of packedge to install when command not fonud
    copybuffer  #use ctl-o to copy the command in terminal
    copyfile    #Puts the contents of a file in your system clipboard so you can paste it anywhere.
    copypath    #copy absolute path into the clipboard
    emoji       #random_emoji display_emoji $emoji
    extract     # a extrace tool much powerful than unar: extract whatever_you_have
    qrcode      #generate qrcode with command qrcode  & qrsvg for svg output
    sudo        # Easily prefix your current or previous commands with sudo by pressing esc twice.
    web-search   # websearch the_platform your_prompt
    zsh-syntax-highlighting     #take a glance of the github page to install seperately
    zsh-autosuggestions         #install seperately like the one above
    zsh-completions             #need seperately installed as well
    #frontend-search    #for web development
    #dirhistory     #alt and arrows to cd
    #coffee      #coffiescript support
)
#ack    not a zsh extension -> can search match text in all file under . recursively

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

source ~/.autoenv/activate.sh
export AUTOENV_ENABLE_LEAVE=1   #enable .env.leave file

#sudo apt install zsh-syntax-hightling
# you can add any command you want to run to init terminal here

定制独一无二的zsh

还记得上文中我提到不建议修改.zshrc,这是因为我们在使用oh my zsh框架后,oh my zsh会建立一个~/oh-my-zsh目录用于存放相关文件,其中有一个名为custom的文件夹,我们可以将我们的配置脚本放在此处,oh my zsh会自动加载这一目录下的vim脚本。
基本支持bash的配置命令。以博主为例,bashrc脚本迁移到zsh未作任何改动。
我创建了一个名为my_custom.zsh的脚本以存储配置,文件如下供你参考。

#                                   _                            _     
#                                  | |                          | |    
# _ __ ___  _   _     ___ _   _ ___| |_ ___  _ __ ___    _______| |__  
#| '_ ` _ \| | | |   / __| | | / __| __/ _ \| '_ ` _ \  |_  / __| '_ \ 
#| | | | | | |_| |  | (__| |_| \__ \ || (_) | | | | | |_ / /\__ \ | | |
#|_| |_| |_|\__, |   \___|\__,_|___/\__\___/|_| |_| |_(_)___|___/_| |_|
#            __/ |_____                                                
#           |___/______|                            
#
#==========================================================================
#   My Costomizations Start
#==========================================================================
#
#set nvim as manpager of man command
export MANPAGER="nvim +Man!"
#
# 设置软件运行环境为64位
export MTI_VCO_MODE=64
#This is the variable of Gowin
#export PATH=$PATH:/opt/Gowin_V1.9.8.09_Education_linux/IDE/bin/

# This is the variable of Modelsim2020.4
# 设置软件可执行文件所在目录
export PATH=$PATH:/opt/modelsim2020.4/modeltech/bin/
# 设置License文件路径(虽然此时还未生成License,但可以提前设置)
export LM_LICENSE_FILE=/opt/modelsim2020.4/license.lic

#export MODULAR_HOME=/home/ahji/.modular
#export PATH=/home/ahji/.modular/pkg/packages.modular.com_mojo/bin:$PATH
export PATH=$PATH:/opt/google/chromedriver-linux64/

export PATH=$PATH:/opt/gcc-arm-none-eabi-10.3-2021.10/bin/

#alias gowin='gw_ide'
#alias gaoyun='gw_ide'
#alias gw='gw_ide'
#alias modelsim='vsim'
#alias cubeide='stm32cubeide'
alias python='python3'
alias md='/opt/marktext/marktext-x86_64.AppImage'
alias netron='/opt/Netron/Netron-7.3.7.AppImage'
#alias bili='/opt/bilibili-v1.12.5-2-x86_64/bin/bilibili &'
#alias bilibili='/opt/bilibili-v1.12.5-2-x86_64/bin/bilibili &'


#alias l='ls'
#alias ll='ls -l'
#alias la='ls -a'
alias c='clear'
alias e='exit'
#alias cl='clear && ls'
#alias v='vim'
alias v='nvim'
alias nv='nvim'
alias py='python3'
alias bt='btop'
#alias bt='bashtop'

#My scripts
alias ahji-connect='sh /home/ahji/Documents/ahji_scripts/connect_Ahji_s_phone.sh'
alias icjlu-connect='python3 /home/ahji/Documents/ahji_scripts/iCGLU_online/iCGLU_online.py'
alias llm-chatglm="/home/ahji/Documents/github/ai/chatglm.cpp/build/bin/main -m /home/ahji/Documents/github/ai/chatglm.cpp/chatglm3-ggml-4_0.bin -i"


PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
export PKG_CONFIG_PATH
export PATH=$PATH:/opt/Gowin_V1.9.8.09_Education_linux/openFPGALoader/build/

export EDITOR=nvim
export VISUAL=nvim

#==========================================================================

切换默认shell

chsh -s /bin/zsh

如有问题,欢迎在评论区讨论。

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值