SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
Introduction To Zsh

          freetstar
 ID:freetstar
 Twitter:freetstar

 天津师范大学
 www.freetstar.com
Zsh 初识

  绝大多数 linux 发行版默认的 shell : bash ,而 zsh 可
能是你需要的终极 shell ,



 zsh 不仅仅是一个 shell ,同时还是一个强大的编程语言,
有强大的补全功能,更广泛的文件名匹配,可以重定向多个 I/O
流,丰富的提示符设计,自动更改拼写错误
截图
zsh 的诞生

   与 debian 的爱情无关,与 linux 的命名不同, zsh 诞生在
一个蛋定的学术环境中,在 1990 年, Princeton Universi-
ty 的 Paul Falstad 发布了 zsh 第一版,那时耶鲁教授
Zhong Shao 在 Princeton 做助教, shao 貌似和 shell
比较配套,于是 zsh 这个名字诞生了




* 引自: http://en.wikipedia.org/wiki/Z_shell
Zsh 之安装

    Deb 系:
         apt-get install zsh
    Rpm:
         yum install zsh
    Arch:
        pacman -S zsh

    推荐额外安装 zsh-lovers
    切换用户的默认 shell:chsh        -s /bin/zsh freetstar
Zsh 提示符
首先:初始化支持
 autoload -U promptinit
 promptinit
 prompt random
再次:查看并预览可见提示符主题 :
 prompt -p
选择一个:
 prompt elite2 red
随机来一个:
 prompt -s random
Zsh 之 TAB

  强大的 TAB 补全:
  允许补全各种参数,自动填充 shell 的许多定义和参数类型。
补全文件类型,忽略其他文件类型 .

 打开 TAB 补全 :
 autoload -Uz compinit
 compinit
Zsh 之补全
ssh , scp , ftp 补全:
    zstyle -e ':conpletion:*:*:*:hosts' hosts 'reply=(${=${$
{(f)”$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N)
/dev/null)”}%%[# ]*}//,/})'

补全项菜单列出
setopt AUTO_LIST
setopt AUTO_MENU
Zsh 之补全扩展
当出现一个错误时自动更正

zstyle ':completion:*' completer _complete _math _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric


补全 kiil 命令
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:*:pkill:*' menu yes select
zstyle ':completion:*:*:*:*:processes' fore-list always
Zsh 之历史信息
定制历史信息
 HISTFILE=histfile
 HISTSIZE=1000
 SAVEHIST=1000
忽略重复的历史和以空格开头的命令
 setopt hist_ignore_alll_dups hist_ignore_space
开启时间的标签戳
 export HISTTIMEFORMAT='%F %T '
Zsh   之路径
输入路径名回车自动进入
setopt autocd
路径缩写
setopt complete_in_word
快速切换目录
% pwd
~/data/foo/horrible/dir/names
% cd foo bar
% pwd
~/data/bar/horrible/dir/names
Zsh      重定向

重定向 stdout 和 stderr 到一个文件
 command |& >file
重定向到多个文件
 command > file1 >file2
同时读取多个文件
 cat test <file1 <file2
比较两个不同目录的文件列表
 diff =(ls new) =(ls old)
Zsh 匹配文件之一
/ 目录        w 0200    S 2000
F 非空目录      x 0100    **/ 当前目录及子目录
/^F 空目录     A 0040    ***/ 当前目录及子目录
                      还有符号链接
, 普通文件      I 0020    %b 块设备
@ 软链接文件     E 0010    %c 字符文件
= 套接字       R 0004
* 可执行普通文件   W 0002
% 设备文件      X 0001
r 0400      s 4000
Zsh   通配符之二


ls **/*.(sh|py)   显示当前目录下及其子目录下的所有 sh 和 py 文
件
ls **/*test*(.)   当前目录及其子目录下的文件名包含 test 的普通
文件
ls **/*(.x) 文件属主有执行权限的普通文件
ls **/*(.:g-w:) 对组没有写权限得普通文件, g-w 首位加” :”
ls **/*(.G) 属于当前用户所在主组的文件
ls **/*(u:freetstar:) 显示 freetstar 用户所拥有的权限
ls *(u0WLk+10m0) root 用户所有,其他用户可写,大于
10k ,在 1h 时内修改过
zsh 匹配文件之时间 & 大小
时间:
     修改 / 访问时间 m/a, 默认时间单位天,月( M ) , 星期
(w), 小时 (h), 分( m ) , 秒 (s)
   ls **/*(.aM-1) // 上个月访问过的文件
大小:
   标志 L ,默认是字节,也可以是 k , m
 ls **/*(.Lm+10) // 文件大小 10m
zsh 历史
^old^new 将以前出现过 old 的命令中替换成 new 并执行
语法: event[:word][:mod1:mod2...]
event
! 历史扩展
!! 前一个命令
! n 第 n 个命令
! -n 当前命令的前几 n 个
! str 开启最近以 str 开启的命令
!? str[?] 包含 str 的命令
Zsh 之 alias
Global Alias
alias -g H=”| head”
alias -g T=”| tail ”
alias -g C=”wc -l”
alias -g HL=”--help”
alias -g NUL=”> /dev/null 2>&1”
Zsh 之 alias
Normal alias
 alias ...=”../..”
 alias ….=”../../..”
 alias …..=”../../../..”
 alias ls=”ls –color=auto”
Suffix alias
 # 开启支持
 autoload -U zsh-mime-setup:
 zsh-mime-setup
 alias -s pdf=evince
 alias -s txt=vim
Zsh 之扩展路径

hash -d www=”/var/www/html”
hash -d init=”/etc/init.d”
hash -d lib=”/usr/lib”
zsh 之 ZLE
编写 ZLE widget
# 设置 sudo, 插入光表后 , 光表移动到行末尾
sudo-command-line(){
    [[ -z $BUFFER ]] && zle up-history
    [[ $BUFFER != sudo * ]] && BUFFER="sudo
$BUFFER"
 }
zle -N sudo-command-line
#Esc Esc 在命令前插入 sudo
bindkey "ee" sudo-command-line
Zsh 之数组
定义数组
A : set -A buffer a   bc
B : buffer=(a,b,c)
数组操作
单元素:
     $buffer[1] $buffer[-1] $buffer[*] $buffer[@]
范围:
     $buffer[1,3] $buffer[1,-1]
Zsh 之 small stricks
# 倒转一个字符串
echo "${(j::)${(@Oa)${(s::):-hello}}}"

zstyle [tab] 查看可用选项
alt+x 输入 zle 命令
# 内置的 pager:
 <readme
zsh 资料
zshworkshops
zsh-lovers
zsh offical document
zshwiki

zshrc:
https://github.com/robbyrussell/oh-my-zsh

Weitere ähnliche Inhalte

Was ist angesagt?

Linux 教育訓練
Linux 教育訓練Linux 教育訓練
Linux 教育訓練Bo-Yi Wu
 
Effective linux.2.(tools)
Effective linux.2.(tools)Effective linux.2.(tools)
Effective linux.2.(tools)wang hongjiang
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash ShellLI Daobing
 
Effective linux.1.(commandline)
Effective linux.1.(commandline)Effective linux.1.(commandline)
Effective linux.1.(commandline)wang hongjiang
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharingYi-Jun Zheng
 
Python 入门
Python 入门Python 入门
Python 入门kuco945
 
Linux1文字介面操作
Linux1文字介面操作Linux1文字介面操作
Linux1文字介面操作jiannrong
 
Linux基础
Linux基础Linux基础
Linux基础zhuqling
 
Linux常用命令
Linux常用命令Linux常用命令
Linux常用命令Tony Deng
 
Command line 初級寶典
Command line 初級寶典Command line 初級寶典
Command line 初級寶典Tom Chen
 
shell script introduction
shell script introductionshell script introduction
shell script introductionJie Jin
 
我的 Windows 平台自動化經驗:基礎批次檔撰寫實務
我的 Windows 平台自動化經驗:基礎批次檔撰寫實務我的 Windows 平台自動化經驗:基礎批次檔撰寫實務
我的 Windows 平台自動化經驗:基礎批次檔撰寫實務Will Huang
 
Virtual file system (VFS)
Virtual file system (VFS)Virtual file system (VFS)
Virtual file system (VFS)Waylin Ch
 
Using Shell & Mastering Shell
Using Shell & Mastering ShellUsing Shell & Mastering Shell
Using Shell & Mastering ShellJiyee Sheng
 
realtime-twitter-search
realtime-twitter-searchrealtime-twitter-search
realtime-twitter-search亚军 汪
 
Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出wang hongjiang
 
Svn安裝與使用簡易教學
Svn安裝與使用簡易教學Svn安裝與使用簡易教學
Svn安裝與使用簡易教學Ray Wei
 
智能Dns工作流程及配置
智能Dns工作流程及配置智能Dns工作流程及配置
智能Dns工作流程及配置Yiwei Ma
 
微博实时搜索
微博实时搜索微博实时搜索
微博实时搜索亚军 汪
 

Was ist angesagt? (20)

Linux 教育訓練
Linux 教育訓練Linux 教育訓練
Linux 教育訓練
 
Effective linux.2.(tools)
Effective linux.2.(tools)Effective linux.2.(tools)
Effective linux.2.(tools)
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash Shell
 
Effective linux.1.(commandline)
Effective linux.1.(commandline)Effective linux.1.(commandline)
Effective linux.1.(commandline)
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing
 
Python 入门
Python 入门Python 入门
Python 入门
 
Linux1文字介面操作
Linux1文字介面操作Linux1文字介面操作
Linux1文字介面操作
 
Linux基础
Linux基础Linux基础
Linux基础
 
Linux常用命令
Linux常用命令Linux常用命令
Linux常用命令
 
Command line 初級寶典
Command line 初級寶典Command line 初級寶典
Command line 初級寶典
 
Netdrive
NetdriveNetdrive
Netdrive
 
shell script introduction
shell script introductionshell script introduction
shell script introduction
 
我的 Windows 平台自動化經驗:基礎批次檔撰寫實務
我的 Windows 平台自動化經驗:基礎批次檔撰寫實務我的 Windows 平台自動化經驗:基礎批次檔撰寫實務
我的 Windows 平台自動化經驗:基礎批次檔撰寫實務
 
Virtual file system (VFS)
Virtual file system (VFS)Virtual file system (VFS)
Virtual file system (VFS)
 
Using Shell & Mastering Shell
Using Shell & Mastering ShellUsing Shell & Mastering Shell
Using Shell & Mastering Shell
 
realtime-twitter-search
realtime-twitter-searchrealtime-twitter-search
realtime-twitter-search
 
Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出
 
Svn安裝與使用簡易教學
Svn安裝與使用簡易教學Svn安裝與使用簡易教學
Svn安裝與使用簡易教學
 
智能Dns工作流程及配置
智能Dns工作流程及配置智能Dns工作流程及配置
智能Dns工作流程及配置
 
微博实时搜索
微博实时搜索微博实时搜索
微博实时搜索
 

Andere mochten auch

Lets enjoy-terminal-life
Lets enjoy-terminal-lifeLets enjoy-terminal-life
Lets enjoy-terminal-lifeYuta Totsuka
 
Coming Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix ShellsComing Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix ShellsKel Cecil
 
Zsh shell-for-humans
Zsh shell-for-humansZsh shell-for-humans
Zsh shell-for-humansJuan De Bravo
 
Why zsh is Cooler than Your Shell
Why zsh is Cooler than Your ShellWhy zsh is Cooler than Your Shell
Why zsh is Cooler than Your Shellbrendon_jag
 
開発環境と言語
開発環境と言語開発環境と言語
開発環境と言語ujihisa
 
Why Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your ShellWhy Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your Shelljaguardesignstudio
 
Zsh & fish: better *bash* for hackers
Zsh & fish: better *bash* for hackersZsh & fish: better *bash* for hackers
Zsh & fish: better *bash* for hackersRuslan Sharipov
 

Andere mochten auch (11)

Lets enjoy-terminal-life
Lets enjoy-terminal-lifeLets enjoy-terminal-life
Lets enjoy-terminal-life
 
ZSH and RVM
ZSH and RVMZSH and RVM
ZSH and RVM
 
Zsh: what i like
Zsh: what i likeZsh: what i like
Zsh: what i like
 
Coming Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix ShellsComing Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix Shells
 
Vim kindergarten
Vim kindergartenVim kindergarten
Vim kindergarten
 
The hacker choice
The hacker choiceThe hacker choice
The hacker choice
 
Zsh shell-for-humans
Zsh shell-for-humansZsh shell-for-humans
Zsh shell-for-humans
 
Why zsh is Cooler than Your Shell
Why zsh is Cooler than Your ShellWhy zsh is Cooler than Your Shell
Why zsh is Cooler than Your Shell
 
開発環境と言語
開発環境と言語開発環境と言語
開発環境と言語
 
Why Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your ShellWhy Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your Shell
 
Zsh & fish: better *bash* for hackers
Zsh & fish: better *bash* for hackersZsh & fish: better *bash* for hackers
Zsh & fish: better *bash* for hackers
 

Ähnlich wie Zsh

Unix常用命令 1
Unix常用命令 1Unix常用命令 1
Unix常用命令 1tony2yy_fish
 
4, files & folders
4, files & folders4, files & folders
4, files & foldersted-xu
 
Linux必学的60个命令
Linux必学的60个命令Linux必学的60个命令
Linux必学的60个命令yiditushe
 
1, shell intro
1, shell intro1, shell intro
1, shell introted-xu
 
Linux安全配置终极指南
Linux安全配置终极指南Linux安全配置终极指南
Linux安全配置终极指南wensheng wei
 
Ruby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for MacRuby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for MacMarsZ Chen
 
Shell(bash) Scripting
Shell(bash) ScriptingShell(bash) Scripting
Shell(bash) ScriptingRobby Lee
 
第9章 Shell 編程
第9章 Shell 編程第9章 Shell 編程
第9章 Shell 編程kidmany2001
 
網路組-Ubuntu介紹
網路組-Ubuntu介紹網路組-Ubuntu介紹
網路組-Ubuntu介紹maryqute520
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplifiedted-xu
 
linux 從無到有
linux 從無到有linux 從無到有
linux 從無到有alex867
 
X64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newX64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newYiwei Ma
 
Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuMarsZ Chen
 
利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版xingsu1021
 
康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)Yiwei Ma
 

Ähnlich wie Zsh (20)

Unix常用命令 1
Unix常用命令 1Unix常用命令 1
Unix常用命令 1
 
4, files & folders
4, files & folders4, files & folders
4, files & folders
 
Unix常用命令
Unix常用命令Unix常用命令
Unix常用命令
 
Linux必学的60个命令
Linux必学的60个命令Linux必学的60个命令
Linux必学的60个命令
 
Linux学习
Linux学习Linux学习
Linux学习
 
1, shell intro
1, shell intro1, shell intro
1, shell intro
 
Overlayfs and VFS
Overlayfs and VFSOverlayfs and VFS
Overlayfs and VFS
 
Linux安全配置终极指南
Linux安全配置终极指南Linux安全配置终极指南
Linux安全配置终极指南
 
Ruby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for MacRuby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for Mac
 
Shell(bash) Scripting
Shell(bash) ScriptingShell(bash) Scripting
Shell(bash) Scripting
 
第9章 Shell 編程
第9章 Shell 編程第9章 Shell 編程
第9章 Shell 編程
 
網路組-Ubuntu介紹
網路組-Ubuntu介紹網路組-Ubuntu介紹
網路組-Ubuntu介紹
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplified
 
linux 從無到有
linux 從無到有linux 從無到有
linux 從無到有
 
X64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 newX64服务器 lamp服务器部署标准 new
X64服务器 lamp服务器部署标准 new
 
Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for Ubuntu
 
利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版
 
gnutool
gnutoolgnutool
gnutool
 
Gnu
GnuGnu
Gnu
 
康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)康盛创想项目部Linux 服务器部署标准(最新版)
康盛创想项目部Linux 服务器部署标准(最新版)
 

Mehr von Rhythm Sun

长连接服务 WebSocket Service
长连接服务 WebSocket Service长连接服务 WebSocket Service
长连接服务 WebSocket ServiceRhythm Sun
 
Trello workflow by @imRhythm
Trello workflow by @imRhythmTrello workflow by @imRhythm
Trello workflow by @imRhythmRhythm Sun
 
Bitcoin and retail
Bitcoin and retailBitcoin and retail
Bitcoin and retailRhythm Sun
 
Garage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_videoGarage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_videoRhythm Sun
 
Doc 2011101412020074
Doc 2011101412020074Doc 2011101412020074
Doc 2011101412020074Rhythm Sun
 
Doc 2011101411284862
Doc 2011101411284862Doc 2011101411284862
Doc 2011101411284862Rhythm Sun
 
Doc 2011101410543721
Doc 2011101410543721Doc 2011101410543721
Doc 2011101410543721Rhythm Sun
 
Doc 2011101404575913
Doc 2011101404575913Doc 2011101404575913
Doc 2011101404575913Rhythm Sun
 
Doc 2010050608572429
Doc 2010050608572429Doc 2010050608572429
Doc 2010050608572429Rhythm Sun
 
火狐2011 sfd讲稿
火狐2011 sfd讲稿火狐2011 sfd讲稿
火狐2011 sfd讲稿Rhythm Sun
 
Sunpinyin and it's future
Sunpinyin and it's futureSunpinyin and it's future
Sunpinyin and it's futureRhythm Sun
 
Customize snipmate
Customize snipmateCustomize snipmate
Customize snipmateRhythm Sun
 
Adorable python
Adorable pythonAdorable python
Adorable pythonRhythm Sun
 
First meetingwithgit
First meetingwithgitFirst meetingwithgit
First meetingwithgitRhythm Sun
 

Mehr von Rhythm Sun (18)

长连接服务 WebSocket Service
长连接服务 WebSocket Service长连接服务 WebSocket Service
长连接服务 WebSocket Service
 
Trello workflow by @imRhythm
Trello workflow by @imRhythmTrello workflow by @imRhythm
Trello workflow by @imRhythm
 
Bitcoin and retail
Bitcoin and retailBitcoin and retail
Bitcoin and retail
 
Garage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_videoGarage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_video
 
Beginning git
Beginning gitBeginning git
Beginning git
 
Doc 2011101412020074
Doc 2011101412020074Doc 2011101412020074
Doc 2011101412020074
 
Doc 2011101411284862
Doc 2011101411284862Doc 2011101411284862
Doc 2011101411284862
 
Doc 2011101410543721
Doc 2011101410543721Doc 2011101410543721
Doc 2011101410543721
 
Doc 2011101404575913
Doc 2011101404575913Doc 2011101404575913
Doc 2011101404575913
 
Doc 2010050608572429
Doc 2010050608572429Doc 2010050608572429
Doc 2010050608572429
 
Outside
OutsideOutside
Outside
 
火狐2011 sfd讲稿
火狐2011 sfd讲稿火狐2011 sfd讲稿
火狐2011 sfd讲稿
 
Linuxdeepin
LinuxdeepinLinuxdeepin
Linuxdeepin
 
Sunpinyin and it's future
Sunpinyin and it's futureSunpinyin and it's future
Sunpinyin and it's future
 
Customize snipmate
Customize snipmateCustomize snipmate
Customize snipmate
 
Adorable python
Adorable pythonAdorable python
Adorable python
 
Using vim
Using vimUsing vim
Using vim
 
First meetingwithgit
First meetingwithgitFirst meetingwithgit
First meetingwithgit
 

Zsh

  • 2.  ID:freetstar  Twitter:freetstar  天津师范大学  www.freetstar.com
  • 3. Zsh 初识 绝大多数 linux 发行版默认的 shell : bash ,而 zsh 可 能是你需要的终极 shell , zsh 不仅仅是一个 shell ,同时还是一个强大的编程语言, 有强大的补全功能,更广泛的文件名匹配,可以重定向多个 I/O 流,丰富的提示符设计,自动更改拼写错误
  • 5. zsh 的诞生 与 debian 的爱情无关,与 linux 的命名不同, zsh 诞生在 一个蛋定的学术环境中,在 1990 年, Princeton Universi- ty 的 Paul Falstad 发布了 zsh 第一版,那时耶鲁教授 Zhong Shao 在 Princeton 做助教, shao 貌似和 shell 比较配套,于是 zsh 这个名字诞生了 * 引自: http://en.wikipedia.org/wiki/Z_shell
  • 6. Zsh 之安装 Deb 系: apt-get install zsh Rpm: yum install zsh Arch: pacman -S zsh  推荐额外安装 zsh-lovers  切换用户的默认 shell:chsh -s /bin/zsh freetstar
  • 7. Zsh 提示符 首先:初始化支持 autoload -U promptinit promptinit prompt random 再次:查看并预览可见提示符主题 : prompt -p 选择一个: prompt elite2 red 随机来一个: prompt -s random
  • 8. Zsh 之 TAB 强大的 TAB 补全: 允许补全各种参数,自动填充 shell 的许多定义和参数类型。 补全文件类型,忽略其他文件类型 . 打开 TAB 补全 : autoload -Uz compinit compinit
  • 9. Zsh 之补全 ssh , scp , ftp 补全: zstyle -e ':conpletion:*:*:*:hosts' hosts 'reply=(${=${$ {(f)”$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) /dev/null)”}%%[# ]*}//,/})' 补全项菜单列出 setopt AUTO_LIST setopt AUTO_MENU
  • 10. Zsh 之补全扩展 当出现一个错误时自动更正 zstyle ':completion:*' completer _complete _math _approximate zstyle ':completion:*:match:*' original only zstyle ':completion:*:approximate:*' max-errors 1 numeric 补全 kiil 命令 zstyle ':completion:*:*:kill:*' menu yes select zstyle ':completion:*:*:pkill:*' menu yes select zstyle ':completion:*:*:*:*:processes' fore-list always
  • 11. Zsh 之历史信息 定制历史信息 HISTFILE=histfile HISTSIZE=1000 SAVEHIST=1000 忽略重复的历史和以空格开头的命令 setopt hist_ignore_alll_dups hist_ignore_space 开启时间的标签戳 export HISTTIMEFORMAT='%F %T '
  • 12. Zsh 之路径 输入路径名回车自动进入 setopt autocd 路径缩写 setopt complete_in_word 快速切换目录 % pwd ~/data/foo/horrible/dir/names % cd foo bar % pwd ~/data/bar/horrible/dir/names
  • 13. Zsh 重定向 重定向 stdout 和 stderr 到一个文件 command |& >file 重定向到多个文件 command > file1 >file2 同时读取多个文件 cat test <file1 <file2 比较两个不同目录的文件列表 diff =(ls new) =(ls old)
  • 14. Zsh 匹配文件之一 / 目录 w 0200 S 2000 F 非空目录 x 0100 **/ 当前目录及子目录 /^F 空目录 A 0040 ***/ 当前目录及子目录 还有符号链接 , 普通文件 I 0020 %b 块设备 @ 软链接文件 E 0010 %c 字符文件 = 套接字 R 0004 * 可执行普通文件 W 0002 % 设备文件 X 0001 r 0400 s 4000
  • 15. Zsh 通配符之二 ls **/*.(sh|py) 显示当前目录下及其子目录下的所有 sh 和 py 文 件 ls **/*test*(.) 当前目录及其子目录下的文件名包含 test 的普通 文件 ls **/*(.x) 文件属主有执行权限的普通文件 ls **/*(.:g-w:) 对组没有写权限得普通文件, g-w 首位加” :” ls **/*(.G) 属于当前用户所在主组的文件 ls **/*(u:freetstar:) 显示 freetstar 用户所拥有的权限 ls *(u0WLk+10m0) root 用户所有,其他用户可写,大于 10k ,在 1h 时内修改过
  • 16. zsh 匹配文件之时间 & 大小 时间: 修改 / 访问时间 m/a, 默认时间单位天,月( M ) , 星期 (w), 小时 (h), 分( m ) , 秒 (s) ls **/*(.aM-1) // 上个月访问过的文件 大小: 标志 L ,默认是字节,也可以是 k , m ls **/*(.Lm+10) // 文件大小 10m
  • 17. zsh 历史 ^old^new 将以前出现过 old 的命令中替换成 new 并执行 语法: event[:word][:mod1:mod2...] event ! 历史扩展 !! 前一个命令 ! n 第 n 个命令 ! -n 当前命令的前几 n 个 ! str 开启最近以 str 开启的命令 !? str[?] 包含 str 的命令
  • 18. Zsh 之 alias Global Alias alias -g H=”| head” alias -g T=”| tail ” alias -g C=”wc -l” alias -g HL=”--help” alias -g NUL=”> /dev/null 2>&1”
  • 19. Zsh 之 alias Normal alias alias ...=”../..” alias ….=”../../..” alias …..=”../../../..” alias ls=”ls –color=auto” Suffix alias # 开启支持 autoload -U zsh-mime-setup: zsh-mime-setup alias -s pdf=evince alias -s txt=vim
  • 20. Zsh 之扩展路径 hash -d www=”/var/www/html” hash -d init=”/etc/init.d” hash -d lib=”/usr/lib”
  • 21. zsh 之 ZLE 编写 ZLE widget # 设置 sudo, 插入光表后 , 光表移动到行末尾 sudo-command-line(){ [[ -z $BUFFER ]] && zle up-history [[ $BUFFER != sudo * ]] && BUFFER="sudo $BUFFER" } zle -N sudo-command-line #Esc Esc 在命令前插入 sudo bindkey "ee" sudo-command-line
  • 22. Zsh 之数组 定义数组 A : set -A buffer a bc B : buffer=(a,b,c) 数组操作 单元素: $buffer[1] $buffer[-1] $buffer[*] $buffer[@] 范围: $buffer[1,3] $buffer[1,-1]
  • 23. Zsh 之 small stricks # 倒转一个字符串 echo "${(j::)${(@Oa)${(s::):-hello}}}" zstyle [tab] 查看可用选项 alt+x 输入 zle 命令 # 内置的 pager: <readme
  • 24. zsh 资料 zshworkshops zsh-lovers zsh offical document zshwiki zshrc: https://github.com/robbyrussell/oh-my-zsh