在Linux上安装zsh

简单介绍:

相对于绝大多数linux发行版默认的shell--bash,zsh绝对是一个优秀的替代品.zsh是交互型shell,同一时候它也是一个强大的编程语言,很多bash,ksh,tcsh优秀的地方在zsh都被实现.同一时候zsh有很多原生的优秀特点.

诞生:

Paul Falstad 在1990年公布了zsh的第一版,当时他还是Princeton University的一名学生.

名字的来源:

这个名字来自耶鲁大学的Zhong Shao教授,那时他在Princeton University做助教.Paul Falstad觉得Shao的登录名"zsh"做为1个shell的名字挺合适的.于是zsh这个名字诞生了

安装:

本文以ubuntu10.10发行版为蓝本.

sudo apt-get install zsh

第一次执行

第一次执行时你会得到下列输出

This is the Z Shell configuration function for new users,zsh-newuser-install.  

You are seeing this message because you have no zsh startup files(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory~). This function can help you with a few settings that shouldmake your use of the shell easier.  

You can:  

(q) Quit and do nothing. The function will be run again next time.  

(0) Exit, creating the file ~/.zshrc containing just a comment.That will prevent this function being run again.  

(1) Continue to the main menu.  

(2) Populate your ~/.zshrc with the configuration recommended by the system administrator and exit (you will need to edit the file by hand, if so desired).  

由于是第一次执行,所以会出现配置界面.我们在这里临时先选择0,以便实现随后的定制.

特色

Tab补全

zsh实现了全面可编程化的补全方式,同意用户让shell自己主动补全各种命令的參数(即使那些与shell本身无关的命令), 自己主动填充shell的很多转换的定义以及很多恰当的參数类型.比方:输入tar xvf ,然后Tab键,系统会自己主动选择tar.gz文件,略过那些不符合的.如今zsh默认有500多个补全定义

实现方法:

%autoload -U compinit  

%compinit  

更高级的补全:

% zstyle ':completion:*:descriptions' format '%U%B%d%b%u' 

% zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b' 

它能够自己主动补全命令、參数、文件名称、进程、username、变量、权限符等。

选择提示符

zsh有很多默认的提示符主题.首先你要初始化高级提示符支持

%autoload -U promptinit  

%promptinit  

查看可用的提示符主题: prompt -p

这里我选择 promot elite2 red

定制历史信息:

这里我们设置记录命令历史文件位~/.histfile,在当前shell中记录1000个命令,在shell关闭后保存使用过的最后1000个命令

HISTFILE=~/.histfile  

HISTSIZE=1000  

SAVEHIST=1000  

假设你不想保存反复的历史

setopt hist_ignore_all_dups hist_ignore_space  

自己主动化的CD命令

zsh同意你只敲入你要进入的目录的路径,而不用输入cd.比方你要进入/etc/init.d

首先打开autocd选项 setopt autocd

然后输入 /etc/init.d 回车就可以进入目录中

扩展的文件名称替换通配符

文件名称替换实现了在展开文件通配符,来出一些特定的文件.广大的shells一直都在使用文件名称替换.比方:

%ls foo*  

foo1 foo2 

强大的重定向功能:

同一时候重定向stdout和stderr到file: command |& >file 同一时候重定向到多个文件: command >file.1 >file.2

zsh的确是个强大的shell,它支持很多有趣的通配符扩展.你能够使用通配符号,他们是一些有特殊意义的字符.比如:列出当前目录下全部的符号链接文件:

%ls *(@) 

zsh的通配符包含了"/"来表示文件夹,"."代表普通文件.很多其它的man zshexpn查看.假设没有匹配,zsh返回错误.

还有其它的类型,比方"/",表示让zsh匹配当前文件夹及当前文件夹的全部子文件夹.比如,找到当前文件夹下以及子文件夹下的不论什么".sh"或者".py"文件,

%ls -l **/*.(sh|py)  

让配置保持生效

为了一直使用扩展的文件名称替换,将下边命令加入到~/.zshrc

autoload -Uz compinit

compinit

zstyle :compinstall filename '/home/capecchi/.zshrc'

zstyle ':completion:*:descriptions' format '%U%B%d%b%u'

zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'

Prompt setup

autoload -U promptinit

promptinit

prompt elite2 yellow

History

HISTFILE=~/.histfile

HISTSIZE=1000

SAVEHIST=1000

We set some options here

setopt appendhistory autocd hist_ignore_all_dups hist_ignore

注: ~/.zshrc文件能够来自交互和登录shell。假设你希望为非交互执行的zsh(即,通过cronjob)设定选项,那么你须要把那些命令加入在~/.zshenv后面

更改用户默认的登录shell:

chsh -s /bin/zsh username  普通用户能够更改自己的,root用户则可更改全部用户的

參考:http://www.linuxaria.com/howto/introduzione-a-zsh?lang=it

http://www.builder.com.cn/2007/0328/383678.shtml

http://linuxtoy.org/archives/zsh.html

posted @ 2017-07-11 10:46  wzjhoutai  阅读(1940)  评论(0编辑  收藏  举报