Mac终端人性化配置记录(Bash&item2+oh my zsh)

一、Bash

觉得 item2 + oh my zsh 太麻烦的,可以试试这个简约版的,支持换颜色

配置步骤

.bash_profile和.bashrc的区别

.bash_profile 针对系统所有用户的权限配置(建议安装在这个文件里)
.bashrc 针对某一个用户的权限配置

具体操作

方法一

  1. 根目录下点击快捷键shift + command + . 显示隐藏文件

  2. 按顺序:访达->前往->前往文件夹->输入~/.bash_profile(必须显示隐藏文件才可以找到,不然就算打开文件所在文件夹也会看不到)

    1. 没有就新建
    2. 双击打开
  3. 将下面得代码复制到文件最下面

    function git_branch {
       branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
       if [ "${branch}" != "" ];then
    	   if [ "${branch}" = "(no branch)" ];then
    		   branch="(`git rev-parse --short HEAD`...)"
    	   fi
    	   echo " ($branch)"
       fi
    }
    
    export PS1='\u@\h \[\033[01;36m\]\W\[\033[01;34m\]$(git_branch)\[\033[00m\] \$ '
    
    # 30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)
    
  4. 保存并退出

  5. 终端执行

source ~/.bash_profile

  1. 重新打开有仓库的文件夹即可显示

方法二

  1. 打开终端 输入

vim ~/.bash_profile

  1. 将下面得代码复制到文件最下面

    function git_branch {
       branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
       if [ "${branch}" != "" ];then
    	   if [ "${branch}" = "(no branch)" ];then
    		   branch="(`git rev-parse --short HEAD`...)"
    	   fi
    	   echo " ($branch)"
       fi
    }
    
    export PS1='\u@\h \[\033[01;36m\]\W\[\033[01;34m\]$(git_branch)\[\033[00m\] \$ '
    
    # 30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)
    
  2. 保存退出,并执行

:wq

source ~/.bash_profile

  1. 退出终端界面,重新打开有仓库的文件夹即可显示
![image](https://img2022.cnblogs.com/blog/651943/202204/651943-20220421180102354-1449660047.png)

遇到的问题

Mac 电脑重启后不生效

因为系统不会自动加载.bashr文件,所以不生效,建议还是配置在.bash_profile文件里面
给文件改名字吧!执行以下命令即可~

mv .bashrc .bash_profile


二、item2+oh my zsh

参考文章:https://blog.csdn.net/weixin_31463675/article/details/113081803

背景

系统默认的 shell / zsh / bash使用起来不是很方便。为了用户能更好的和电脑系统进行交流,我们就可以借助 item2 感觉比 Mac 自带的终端好用。
oh my zsh 可配置主题

安装方式

第一步、安装iterm2

这是前提
官网地址:https://iterm2.com/

第二步、安装 oh my zsh

官网地址:https://ohmyz.sh/

我选择的安装:

shc"(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

其他安装方式

shc"(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

安装完成后如下图所示

  1. cat /etc/shells 可查看当前所有
  2. echo $SHELL 查看当前 shell
  3. chsh -s /bin/bash 切换到 bash
  4. chsh -s /bin/zsh 切换到 zsh

第三步、安装主题

主题地址:https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
默认配置的主题:robbyrussell
主题列表如下:
路径:~/.oh-my-zsh/theme(访达->前往->前往文件夹)
image

配置主题位置如下:路径:~/.zshrc(访达->前往->前往文件夹)
image

第四步、自定义主题

如果配置的主题某些部分不太满意,比如不想显示计算机名,可以拷贝一份出来,修改名字,把${user_host}去掉
按需修改即可
image

遇到的问题:

用 chsh -s /bin/zsh 命令不生效,提示 chsh: Shell not changed

参考解决文章https://blog.csdn.net/FungLeo/article/details/99448425

mac系统:zsh切换bash提示chsh: no changes made问题

参考解决文章https://blog.csdn.net/weixin_42326230/article/details/104028338

.zshrc配置参考

# 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/robbyrussell/oh-my-zsh/wiki/Themes
# ZSH_THEME="robbyrussell"geoffgarside
# ZSH_THEME="pure"
ZSH_THEME="ys"

# 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 ~/.oh-my-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 the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=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.
# 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 ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-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"

# zsh-theme-pure start
# autoload -U promptinit; promptinit

# optionally define some options
# PURE_CMD_MAX_EXEC_TIME=10

# change the path color
# zstyle :prompt:pure:path color white

# change the color for both `prompt:success` and `prompt:error`
# zstyle ':prompt:pure:prompt:*' color cyan

# turn on git stash status
# zstyle :prompt:pure:git:stash show yes

# prompt pure
#zsh-theme-pure end

# source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh


export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

source ~/.bash_profile

posted @   EuniceShaw  阅读(381)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示