Linux终极shell Z Shell 用强大的zsh & oh-my-zsh把Bash换掉

Bash确实是不错的Shell,但仍有用很多不尽人意的地方,如自动补全的功能不够强大,定位较长路径不够方便,命令历史管理不够完善等。

介绍使用zsh,功能十分强大…推荐给大家!

1 介绍zsh

1.1 Linux shell

常用的Shell有这么几种,sh、bash、csh等,想知道你的系统有几种shell,可以通过以下命令查看:

cat /etc/shells
未安装zsh,显示结果如下:
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

1.2 zsh简介

Zsh是一个Linux下强大的shell, 由于大多数Linux产品安装,以及默认使用bash shell, 但是丝毫不影响极客们对zsh的热衷, 几乎每一款Linux产品都包含有zsh,通常可以用apt-get、urpmi或yum等包管理器进行安装

Zsh具有以下主要功能
开箱即用、可编程的命令行补全功能可以帮助用户输入各种参数以及选项
在用户启动的所有shell中共享命令历史
通过扩展的文件通配符,可以不利用外部命令达到find命令一般展开文件名
改进的变量与数组处理
在缓冲区中编辑多行命令
多种兼容模式,例如使用/bin/sh运行时可以伪装成Bourne shell
可以定制呈现形式的提示符;包括在屏幕右端显示信息,并在键入长命令时自动隐藏
可加载的模块,提供其他各种支持:完整的TCP与Unix域套接字控制,FTP客户端与扩充过的数学函数
完全可定制化

1.3 zsh与oh-my-zsh终极配置

之前是因为看到这篇文章:终极Shell——Zsh 才选择使用zsh,被它的自动完成、补全功能吸引了。

<官网:www.zsh.org

这也是“工欲善其事,必先利其器”系列文章。

选择oh-my-zsh, oh-my-zsh是基于zsh的功能做了一个扩展,方便的插件管理、主题自定义,以及漂亮的自动完成效果。

在Github上找关于zsh的项目时发现的,试用了一下觉得很方便,不用像上面文章里面提到的那么复杂,配置一些插件的名称即可使用相应的功能。

官网:https://github.com/robbyrussell/oh-my-zsh
评价:https://github.com/robbyrussell/oh-my-zsh/wiki/Testimonials

2 安装zsh

2.1 安装zsh

对于一般的Ubuntu系统,配置好正确的源之后,就能直接键入以下命令安装:

sudo apt-get install zsh

对于linux系统,直接使用以下命令安装:

yum -y install zsh

2.2 配置zsh

把.zshrc拷贝到相应用户的home目录即可
(也可以把你的bash的配置文件(/.bash_prorile或者/.profile等)给拷贝到zsh的配置文件~/.zshrc里,因为zsh兼容bash)

使用 Zsh 扩展集合:oh-my-zsh
oh-my-zsh 帮我们整理了一些常用的 Zsh 扩展功能和主题:https://github.com/robbyrussell/oh-my-zsh
我们无需自己去捣搞 Zsh,直接用 oh-my-zsh 就足够了,如果你想继续深造的话那再去弄。
先安装 git:sudo yum install -y git
安装 oh-my-zsh(这个过程可能会有点慢,或者需要重试几次):wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O - | sh
安装效果图如下:
在这里插入图片描述

在以 root 用户为前提下,oh-my-zsh 的安装目录:/root/.oh-my-zsh
在以 root 用户为前提下,Zsh 的配置文件位置:/root/.zshrc
为 root 用户设置 zsh 为系统默认 shell:chsh -s /bin/zsh root
如果你要重新恢复到 bash:chsh -s /bin/bash root
现在你关掉终端或是重新连上 shell,现在开头是一个箭头了,如下图:
在这里插入图片描述
现在zsh和oh-my-zsh都安装好了,现在重点讲下zsh的配置!

创建~/.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/amoscykl/.oh-my-zsh
 
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
# zhs的主题
ZSH_THEME="ys"		
# Set list of themes to load
# Setting this variable when ZSH_THEME=random
# cause zsh load theme from this variable instead of
# looking in ~/.oh-my-zsh/themes/
# An empty array 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 change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
 
# 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.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# 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? (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.
 
# z命令快速跳转目录     x命令解压一切文件         命令行可以直接google  
plugins=(
  git z zsh-autosuggestions extract web-search zsh-syntax-highlighting 
)
 
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"
 
# ssh
# export SSH_KEY_PATH="~/.ssh/rsa_id"
 
# 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"
#

保存并执行命令,使配置生效:

cd ~ && source .zshrc

猜你喜欢

转载自blog.csdn.net/guo_qiangqiang/article/details/85700258