当前位置: 代码网 > it编程>前端脚本>Powershell > 手把手带你从bash无痛切换到zsh,命令行效率轻松翻倍

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

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

如果你使用的是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

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com