使用oh-my-zsh美化你的终端

xiaohai 2018-10-08 21:56:27 4061人围观 标签: Bash  终端 
简介平时在使用终端的时候总是感觉样式不太美观,智能提示不太友好。恰巧今天在无意间看到了oh my zsh这个框架,让你瞬间觉得终端还可以这样玩。本文主要介绍安装方式,留着以后使用。

oh-my-zsh官网地址:https://ohmyz.sh/

Your terminal never felt this good before
一、查看系统shell信息

Linux的shell有很多种,可以使用以下命令查看:

[root@localhost ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin

但是系统默认使用的是bash,可以使用下面的命令查看:

[root@localhost ~]# echo $SHELL /bin/bash
二、安装zsh

在使用oh-my-zsh的时候,首先需要安装zsh,从上面我们可以看出,Centos7的系统没有安装zsh,那么我们可以通过下面的命令来安装:

[root@localhost ~]# yum -y install zsh

安装完成后,重新查看:

[root@localhost ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /bin/zsh

最后一行已经有了zsh了。

系统默认为bash,这里我们需要切换shell为zsh:

[root@localhost ~]# chsh -s /bin/zsh Changing shell for root. Shell changed.

切换后,需要重启系统,reboot,重启完成后,再次查看当前的shell:

[root@localhost]~# echo $SHELL /bin/zsh
三、安装oh my zsh

oh-my-zsh的源码放在GitHub上,所以首先确保自己的系统已经安装了git,如果没有安装,可以使用下面的命令安装:

[root@localhost]~# yum install git

安装oh-my-zsh:

[root@localhost]~# wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

安装完成后,界面如下:

Cloning Oh My Zsh... Cloning into '/root/.oh-my-zsh'... remote: Enumerating objects: 947, done. remote: Counting objects: 100% (947/947), done. remote: Compressing objects: 100% (833/833), done. remote: Total 947 (delta 26), reused 765 (delta 18), pack-reused 0 Receiving objects: 100% (947/947), 620.51 KiB | 109.00 KiB/s, done. Resolving deltas: 100% (26/26), done. Looking for an existing zsh config... Using the Oh My Zsh template file and adding it to ~/.zshrc __ __ ____ / /_ ____ ___ __ __ ____ _____/ /_ / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ /____/ ....is now installed! Please look over the ~/.zshrc file to select plugins, themes, and options. p.s. Follow us at https://twitter.com/ohmyzsh. p.p.s. Get stickers and t-shirts at https://shop.planetargon.com.

安装过程中如果出现如下错误:

fatal: unable to access 'https://github.com/robbyrussell/oh-my-zsh.git/': Peer reports incompatible or unsupported protocol version. 可以执行下面的命令: [root@localhost]~# yum update nss curl

上面安装成功后,可以使用下面的命令是oh-my-zsh生效:

[root@localhost]~# source ~/.zshrc #下面就是生效后的信息 ➜ ~ ➜ ~ ➜ ~

默认情况下使用的主题为:

ZSH_THEME="robbyrussell"

还有很多其他的主题,主题地址:https://github.com/robbyrussell/oh-my-zsh/wiki/themes

这里我将主题修改为:

ZSH_THEME="dallas"

更新配置:

[root@localhost]~# source ~/.zshrc #下面为生效后的信息 {18-10-08 17:46}localhost:~ root# {18-10-08 17:46}localhost:~ root# {18-10-08 17:46}localhost:~ root#
四、自动补全插件(智能提示)

虽然上面使用oh-my-zsh美化了界面,但是linux的命令非常的多,还有就是目录下的文件名非常的多,如何能友好的智能提示和补全也是我们非常需要的功能。这里我们就需要安装一些插件了。这里有一个incr.zsh 补全插件:http://mimosa-pudica.net/zsh-incremental.html
1、首先我们在~/.oh-my-zsh/plugins/目录下创建incr目录来存放该插件

{18-10-08 18:39}localhost:~ root# mkdir ~/.oh-my-zsh/plugins/incr

2、将插件下载到上一步创建的目录下

{18-10-08 18:40}localhost:~ root# wget -P ~/.oh-my-zsh/plugins/incr http://mimosa-pudica.net/src/incr-0.2.zsh

3、在~/.zshrc最后一行添加如下内容:

source ~/.oh-my-zsh/plugins/incr/incr*.zsh

4、重新更新配置

{18-10-08 18:46}localhost:~ root# source ~/.zshrc

现在就可以使用智能补全功能了,但是可能在使用的过程中,如果使用vim的时候出现以下问题:

{18-10-08 17:44}localhost:~ root# vim ~/ _arguments:451: _vim_files: function definition file not found 解决方式,执行下面两条命令即可: {18-10-08 17:44}localhost:~ root# rm -rf ~/.zcompdump* {18-10-08 17:45}localhost:~ root# exec zsh

以上就是自己使用zsh、oh-my-zsh和智能补全插件的整个过程做以上记录,方面今后使用。