【开发工具推荐】好评如潮的shell——oh my zsh🌟

3,422 阅读3分钟

背景

在日常代码开发过程中,mac自带的shell提供的功能有限,一些常用指令在输入完毕后,下次输入时不能自动联想、补全,只能不断上翻或者重拼一遍,很麻烦。

作为程序员,我们推崇Elegant code, 那其实我们也需要Elegant Shell。

在此向你安利业界好评如潮的shell——oh my zsh


为什么好用

  1. 插件系统: oh-my-zsh拥有大量的插件,可以增强我们的命令行体验。比如,有自动完成插件、Git插件、别名插件等,它们可以让你更高效地完成日常任务。
  • history:此插件提供了一些增强命令行历史记录管理的功能。这些功能可以帮助我们更有效地浏览、搜索和调用之前执行过的命令。我自己在开发过程中最常用的是历史搜索历史命令补全。举个例子: 假设我们在项目工程的终端执行过yarn start,git checkout feature/xxx,git commit -m "chore:something"等指令,后续我们只要在终端输入yarn s,然后按下键盘的方向键,终端即可自动补全yarn start,同理,当我们想要切换git分支时,也只要在终端输入git ch,然后按下键盘的方向键,终端即可自动补全git checkout feature/xxx。 省去了我们回忆某些指令关键字以及手动拼写完整的指令代码的时间,减少了一些因为工作的繁琐性而产生的烦恼,让自己有一个好心情~

  • git: 此插件为 Git 版本控制系统提供了一系列的别名和辅助函数,别名的存在让我们执行git指令更加轻松(毕竟可以少写一些代码~)。 举个例子:gst (相当于 git status),gl (相当于 git log),gco (相当于 git checkout)。说真的,帮我们节省了大量的指令输入时间,再结合上方的history插件,真是锦上添花。

  1. 主题: 可以改变你的命令行提示符的样式、颜色和其他视觉元素,从而提高命令行的可读性和美观性。oh-my-zsh 提供了大量的预定义主题,用户可以根据自己的喜好来选择和定制(在这里贴上官网的几个主题的图片,供大家参考)。

image.png

image.png

image.png

  1. autojumpextra等等,介于本文主要还是期望作为一个短小精悍的工具类文章,旨在帮助大家在对oh-my-zsh有个初步的了解和感受后,能够正确的安装并运行。因此仅展开说说我自己用了之后体感特别好的2个好处,更多的信息可以通过官网了解~

安装

  1. 访问官网,点击“install”
  2. 在命令行工具执行这行命令sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  3. 安装成功

问题

安装完之后,终端执行node -vnpm -vbit -v均报错“command not found”

40ef56c4a077589a57762f65705fd6c.jpg

解决

修改.zshrc文件,为其增加2行如下代码

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" 
export PATH=$HOME/bin:$PATH

修改完成后,打开一个的终端,验证。

3f3ff8014d09139d9b1f138af1b4b8c.jpg

为什么能解决?

详情看下方两个链接

  1. 解决nvm、node问题
  2. 解决bit问题

扩展

修改.zshrc文件内容的方式有很多种

1.直接通过终端进行修改(硬核🧑‍🔬)

  • 终端输入vi .zshrc
  • 按下E,表示我要编辑该文件
  • 按下I,表示我要进入Insert模式,此时可以往该文件插入代码
  • 将上面两段代码插入完毕后,按下ESC,然后输入: wq,表示写入完成

2.通过vscode修改(👈推荐)

  • 在vscode中执行command + shift + p ,安装“code”

image.png

  • 在命令行工具执行code .zshrc,在vscode内部打开该文件,方便修改

image.png

附录

用于对照的增加完代码后的.zshrc源文件

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# This loads nvm
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" 


# 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"

# 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)

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"
export PATH=$HOME/bin:$PATH