oh-my-zsh安装、配置与使用

为什么要使用oh-my-zsh

Linux下默认shell是bash,虽然bash的功能已经很强大了,但是还是要求管理员要记大量的shell命令,即使用man或者-h查询命令的拼写和使用方法仍然很麻烦,一切以轻松快速的目的,oh-my-zsh 提供了大量的插件,可以便捷的对命令进行提示,大量的主题对shell进行美化。

oh-my-zsh安装

1.安装zsh

以RHEL系为例:

  • 查看当前系统是否安装了zsh

    [root@master ~]# cat /etc/shells 
    /bin/sh
    /bin/bash
    /usr/bin/sh
    /usr/bin/bash
    

    可以看出,当前系统中并没有安装zsh

  • 安装zsh

    [root@master ~]# dnf -y install zsh
    
  • 安装git

    [root@master ~]# dnf -y install git
    

    克隆oh-my-zsh用

2.安装oh-my-zsh

  • 克隆oh-my-zsh

    [root@master ~]# git clone https://gitee.com/mirrors/oh-my-zsh.git ~/.oh-my-zsh
    
  • 切换bash为zsh

    [root@master ~]# cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
    [root@master ~]# chsh -s /bin/zsh 
    Changing shell for root.
    Shell changed.
    [root@master ~]# omz reload
    ➜  ~
    
  • oh-my-zsh的主题和插件路径

    ➜  ~ cd ~/.oh-my-zsh/        
    ls                                                                                                                    
    ➜  .oh-my-zsh git:(master) ls
    CODE_OF_CONDUCT.md  CONTRIBUTING.md  LICENSE.txt  README.md  SECURITY.md  cache  custom  lib  log  oh-my-zsh.sh  plugins  templates  themes  tools
    
    

    plugins:为插件路径

    themes:为主题路径

  • 查看官方插件

    ➜  .oh-my-zsh git:(master) cd plugins 
    ➜  plugins git:(master) ls
    

    image-20240224211659574

  • 查看官方主题

    ➜  plugins git:(master) cd .. 
    ➜  .oh-my-zsh git:(master) cd themes 
    ➜  themes git:(master) ls
    

    image-20240224211857625

oh-my-zsh 配置

  • 编辑家目录下的.zshrc对oh-my-zsh进行配置

    • 配置主题

       ➜ vim ~/.zshrc
        1 # If you come from bash you might have to change your $PATH.
        2 # export PATH=$HOME/bin:/usr/local/bin:$PATH
        3 
        4 # Path to your oh-my-zsh installation.
        5 export ZSH=$HOME/.oh-my-zsh
        6 
        7 # Set name of the theme to load --- if set to "random", it will
        8 # load a random theme each time oh-my-zsh is loaded, in which case,
        9 # to know which specific one was loaded, run: echo $RANDOM_THEME
       10 # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
       11 ZSH_THEME="robbyrussell"
       12 #ZSH_THEME="ys"
      
      

      默认主题为:robbyrussell 可以修改进行切换ramdom

         ➜ vim ~/.zshrc
         1 # If you come from bash you might have to change your $PATH.
         2 # export PATH=$HOME/bin:/usr/local/bin:$PATH
         3 
         4 # Path to your oh-my-zsh installation.
         5 export ZSH=$HOME/.oh-my-zsh
         6 
         7 # Set name of the theme to load --- if set to "random", it will
         8 # load a random theme each time oh-my-zsh is loaded, in which case,
         9 # to know which specific one was loaded, run: echo $RANDOM_THEME
        10 # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
        11 ZSH_THEME="random"
      
      

      保存退出后随机切换为mrtazz主题,每次登录shell都会随机切换:

      ➜  ~ omz reload
      [oh-my-zsh] Random theme 'mrtazz' loaded
      master:~:# 
      
    • 配置插件

       master:~:# vim .zshrc
       69 # Which plugins would you like to load?
       70 # Standard plugins can be found in $ZSH/plugins/
       71 # Custom plugins may be added to $ZSH_CUSTOM/plugins/
       72 # Example format: plugins=(rails git textmate ruby lighthouse)
       73 # Add wisely, as too many plugins slow down shell startup.
       74 plugins=(git)
       75 
       76 source $ZSH/oh-my-zsh.sh
       77 
       78 # User configuration
      

      在plugins=(git)在括号中添加相应的插件即可获得相应的提示效果 ,下面以docker命令为例:

      安装前:敲完docker后按tab补全是这样的效果

      ➜  ~ docker anaconda-ks.cfg 
      

      添加插件后

       master:~:# vim .zshrc
       69 # Which plugins would you like to load?
       70 # Standard plugins can be found in $ZSH/plugins/
       71 # Custom plugins may be added to $ZSH_CUSTOM/plugins/
       72 # Example format: plugins=(rails git textmate ruby lighthouse)
       73 # Add wisely, as too many plugins slow down shell startup.
       74 plugins=(git docker)
      
      master:~:# omz reload
      [oh-my-zsh] Random theme 'sporty_256' loaded
       ~
       
      

      出现了docker命令的提示

      image-20240224214928954

oh-my-zsh总结

  • 安装zsh
  • 克隆oh-my-zsh
  • cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc配置文件到家目录下为.zshrc
  • 配置插件和主题

oh-my-zsh其他

克隆第三方插件

-<ROOT@master.sumail.com:~>-                                                                                                     
-<#>- git clone https://gitee.com/szdongxf/zsh-syntax-highlighting.git .oh-my-zsh/plugins/zsh-syntax-highlighting
-<ROOT@master.sumail.com:~>-                                                                                                    
-<#>- git clone https://gitee.com/funckzs/zsh-autosuggestions.git .oh-my-zsh/plugins/zsh-autosuggestions

简单的插件配置示例

 [~]$ vim .zshrc
 69 # Which plugins would you like to load?
 70 # Standard plugins can be found in $ZSH/plugins/
 71 # Custom plugins may be added to $ZSH_CUSTOM/plugins/
 72 # Example format: plugins=(rails git textmate ruby lighthouse)
 73 # Add wisely, as too many plugins slow down shell startup.
 74 plugins=(git docker aliases  z zsh-syntax-highlighting web-search zsh-autosuggestions)
 75 

[~]$ omz reload 
[oh-my-zsh] plugin 'zsh-autosuggestions' not found
[oh-my-zsh] Random theme 'refined' loaded
                                                                                                                                                                          
~   
❯