zsh教程

来源:互联网 发布:数组作为形参 编辑:程序博客网 时间:2024/06/02 06:15

目录

  • 1. 本文参考链接
  • 2. 显示系统中存在的shell
  • 3. zsh环境
    • 3.1. 第一步 安装zsh
      • 3.1.1. mac
      • 3.1.2. linux
    • 3.2. 第二步 安装oh my zsh
      • 3.2.1. 手动安装
      • 3.2.2. 自动安装
  • 4. 设置zsh为系统默认shell
  • 5. 配置zsh
    • 5.1. 别名
    • 5.2. 主题
  • 6. zsh插件
    • 6.1. 插件使用方式和插件位置
    • 6.2. 插件打开方式
    • 6.3. 主要插件
      • 6.3.1. autojump
      • 6.3.2. incr显示补全提示
  • 7. zsh使用
    • 7.1. 兼容 bash,原来使用 bash 的兄弟切换过来毫无压力,该咋用咋用
    • 7.2. 智能补全
    • 7.3. 智能跳转
  • 8. 遇到的问题
    • 8.1. zsh下输入vi,补全不起作用
    • 8.2. zsh下补全命令乱码

本文参考链接

http://macshuo.com/?p=676

显示系统中存在的shell

cat /etc/shells

zsh环境

第一步 安装zsh

mac

不用安装,系统自带

linux

  1. ubuntu

    sudo apt-get install zsh

  2. centos

    sudo yum install zsh

第二步 安装oh my zsh

手动安装

git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

自动安装

wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

设置zsh为系统默认shell

chsh -s /bin/zsh

配置zsh

别名

zsh的配置主要集中在用户当前目录的.zshrc里
alias cls=’clear’
alias ll=’ls -l’
alias la=’ls -a’
alias vi=’vim’
alias javac=”javac -J-Dfile.encoding=utf8”
alias grep=”grep –color=auto”
alias -s html=mate # 在命令行直接输入后缀为 html 的文件名,会在 TextMate 中打开
alias -s rb=mate # 在命令行直接输入 ruby 文件,会在 TextMate 中打开
alias -s py=vi # 在命令行直接输入 python 文件,会用 vim 中打开,以下类似
alias -s js=vi
alias -s c=vi
alias -s java=vi
alias -s txt=vi
alias -s gz=’tar -xzvf’
alias -s tgz=’tar -xzvf’
alias -s zip=’unzip’
alias -s bz2=’tar -xjvf’
alias -s html=mate,意思就是你在命令行输入 hello.html,zsh会为你自动打开 TextMat 并读取 hello.html; alias -s gz=’tar -xzvf’,表示自动解压后缀为 gz 的压缩包。

主题

.zshrc 里找到ZSH_THEME,就可以设置主题了,默认主题是:ZSH_THEME=”robbyrussell”。oh my zsh 提供了数十种主题,相关文件在~/.oh-my-zsh/themes目录下。
PROMPT=’%{fg_bold[red]fg\_bold[green]%}%p%{fg[cyan]fg\_bold[blue]%}% %{resetcolor#PROMPT=fg\_bold[red]%}➜ %{fg_bold[green]fg[cyan]%}%c %{\(fg_bold[blue]%}\)(git\_prompt\_info)%{fg_bold[blue]reset_color%}’
对照原来的版本,我把 c 改为 d,c 表示当前目录,d 表示绝对路径,另外在末尾增加了一个「 > 」,改完之后的效果是这样的:

zsh插件

插件使用方式和插件位置

~/.oh-my-zsh/plugins
插件的使用方式在插件相应的文件中

插件打开方式

.zshrc中变量plugins=(git textmate ruby autojump osx mvn gradle)增加要使用的插件

主要插件

autojump

  1. 安装

    • mac

    brew install autojump

    • linux

    git clone git://github.com/joelthelion/autojump.git
    ./install.py ;;其会在~/目录下建立.autojump文件夹

    .zshrc中增加
    如果你是zsh,添加如下到 ~/.zshrc
    [ [ -s ~/.autojump/etc/profile.d/autojump.zsh ]] && . ~/.autojump/etc/profile.d/autojump.zsh

    如果你是bash,则添加如下到 ~/.bashrc

    [ [ -s ~/.autojump/etc/profile.d/autojump.bash ] ] && . ~/.autojump/etc/profile.d/autojump.bash

  2. 使用方法(j –help查看详细使用方法)

    1. j+目录名

    2. j -s

incr显示补全提示

参考链接:http://my.oschina.net/u/923974/blog/500086
http://zhan.renren.com/imacsoft?gid=3602888498048767708&checked=true

  1. 在~/.zshrc中找到plugins=(git incr) 添加incr,ohmyzsh会自动引用plugins中的安装目录

  2. 新建文件.oh-my-zsh/plugins/incr/incr.plugin.zsh ;;注意必须是该路径,不然找不到文件,该路径能够被子模块识别到,文件夹custom加入到了子模块的忽略文件夹里了。

    incr.plugin内容为地址 http://mimosa-pudica.net/src/incr-0.2.zsh

  3. source ~/.zshrc

zsh使用

兼容 bash,原来使用 bash 的兄弟切换过来毫无压力,该咋用咋用

智能补全

  • tab
  • c-p/n/f/b
  • /Users/pw/workspace/project/src/main/webapps/static/js,就得在 bash 下面打半天,不停的 tab 去补全一个正确的路径出来。在 zsh 下,只需要输入每个路径的头字母然后 tab 一下: cd /u/p/w/p/s/m/w/s/j
  • 命令+方向键

智能跳转

  • 见autojump使用。
  • zsh下输入d显示跳转的目录。输入前面的序号即可进行跳转。
  • 直接输入文件名即可跳转,需要cd前缀。甚至是.和..

遇到的问题

zsh下输入vi,补全不起作用

_arguments:450: _vim_files: function definition file not found

解决:

  1. rm ~/.zcompdump*
  2. restarting your session.

zsh下补全命令乱码

ffmpeg -i tesperl: warning: Setting locale failed.null
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = “UTF-8”,
LANG = “en_US.UTF-8”
are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).

解决: How can I fix a locale warning from Perl? - Stack Overflow
If you are using zsh, edit zshrc:

LC_CTYPE=en_US.UTF-8
LC_ALL=en_US.UTF-8

原创粉丝点击