手把手带你从bash无痛切换到zsh,命令行效率轻松翻倍
文章目录
- 什么是shell?
- zsh有什么优势?
- zsh配置
- oh my zsh!
- 安装oh my zsh
- 配置oh my zsh
- 定制独一无二的zsh
- 切换默认shell
如果你使用的是bash shell,那么你大概率正在使用某个linux发行版。
(图片来源网络,侵删)目前,大部分linux发行版使用的默认shell仍然是bash。
Windows默认使用Powershell。
从Catalina开始,MacOS已经将默认shell从bash切换到了zsh。
什么是shell?
简单说,shell是你与应用程序交互的媒介。
通常情况下,你将你想要使用的应用程序和参数输入到shell,shell在PATH中查找你希望调用的应用程序,对参数进行解析,并传入参数。最终将结果返回给你。
zsh有什么优势?
相比bash,zsh有庞大的插件社区和成熟的插件管理框架如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 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
如有问题,欢迎在评论区讨论。