`
天梯梦
  • 浏览: 13612505 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

Ubuntu工具zsh和Byobu

 
阅读更多

 

使用 Zsh 的九个理由

像大部分 *nix 用户,我之前用 bash 很多年,期间也有过小的不爽,但一直都忍过来,或者是说没想过这些不爽的地方能解决,比如 cd 到一个深目录时得哐哐猛敲 <TAB>。这么多年里我也尝试过其他 shell。比如 ksh/tcsh 以及今天要说的 zsh,但最终都没坚持下去,因为心中始终还是认为 bash 是最正统的 shell,不愿意去主动深入学习其他 shell。直到前几天逛 github,发现 排名第 6 的开源项目 oh-my-zsh,下来试用了一把,顿时觉得 bash 各种操作不爽到无法忍受。

 

放弃 bash 的各种内牛满面的理由

这里有个 youtube 上的视频,短短 4 分钟就已经抛出了几十个让 bash 用户切换到 zsh 中的理由。视频链接

 

理由 0:zsh 兼容 bash

兼容 bash 意味着我不需要太多学习成本就可以切换过来,意味着我以前在 bash 下积累的 shell 语法、基本操作都不会荒废。在我心里 bash 还是最通用和标准的 shell 环境,因此兼容 bash 让我切换到 zsh 时没有太多后顾之忧。

 

理由 1:zsh 的补全模式更方便

zsh 中按两下 tab 键可以触发 zsh 的补全,所有待补全项都可以通过键盘方向键或者 <Ctrl-n/p/f/b> 来选择。

 

理由 2:zsh 支持命令选项补全

zsh 除了支持目录的补全,还支持命令选项的补全,例如 ls -<TAB><TAB> 会直接列出所有 ls 的参数,再也不会出现一个命令打到一半,忘记参数导致重开一个 terminal man 一把。

 

理由 3:zsh 支持命令参数补全

以前想 kill 掉一个进程,我的做法是 ps aux | grep "进程名" 然后记下 id,再 kill id。在 zsh 下,只需要 kill 进程名<TAB>zsh 就会自动补全进程的 pid。

其余我常用的补全还有:

  • ssh <TAB><TAB> 时 zsh 会自动列出你访问过的主机和用户名来补全 ssh 的参数。
  • brew install <TAB><TAB> 来补全软件包名,除了 homebrew 以外,同样支持 port/apt-get 等其他包管理器。

 

理由 4:zsh 支持更加聪明的目录补全

以前比如想进入一个比较深的目录,比如 /Users/pw/workspace/project/src/main/webapps/static/js,就得在 bash 下面打半天,不停的 tab 去补全一个正确的路径出来。在 zsh 下,只需要输入每个路径的头字母然后 tab 一下: cd /u/p/w/p/s/m/w/s/j<TAB>

 

理由 5:zsh 强大的快速目录切换

以前最苦逼的事情莫过于频繁在两个工作目录下切换,总要打一长串 cd 路径。也尝试过 popdpushd 来解决这个问题,但往往是目录已经切换了才想起来没用 pushd。而 zsh 会记住你每一次切换的路径,然后通过 1 来切换到你上一次访问的路径,2 切换到上上次……一直到 9,还可以通过 d 查看目录访问历史。

zsh 还可以配合 autojump 一起使用,autojump 会记录下每一个你访问过的目录,然后通过 j 来快速跳转。

 

理由 6:zsh 支持全局 alias 和后缀名 alias

bash 的 alias 只能做命令的缩写,而 zsh 更进一步,使 alias 可以缩写命令的一部分,例如参数或环境变量设置。

$ alias -s log=less
$ ~/package/tomcat/log/catalina.log # 相当于 less ~/package/tomcat/log/catalina.log
$ alias -g PR=http_proxy=127.0.0.1:8087
$ PR curl https://twitter.com # 相当于 http_proxy=127.0.0.1:8087 curl https://twitter.com

 

理由 7:zsh 有着丰富多彩的命令行提示符

bash 下通过设置 $PS1 已经可以实现很丰富的提示符了,而 zsh 更进一步,可以实现诸如多行提示符、提示符右对齐等功能。oh-my-zsh 配置文件中提供了非常丰富的提示符 theme 供选择,我使用的是 gentoo 主题,比较简洁,还可以显示当前 git 仓库的状态。

 

理由 8:zsh 有更多优雅的语法

例如修改 PATH,bash 下设置 $PATH 要求所有路径都要写在一行里,目录多了以后看起来就很难看。zsh 支持更加符合程序员审美观的设置方式。

path=(
    ~/bin
    $path
    ~/package/smartsprites/bin
)

 

安装 zsh

Linux 用户通过各自发行版的包管理器直接安装即可。

Mac 自带一个 4.x.x 版本的 zsh,可以直接使用,也可以通过 homebrew 安装最近刚刚发布的 5.0.0 版本。推荐使用最新的 5.0 版本,对多字节字符提供了完整的支持,这一点对于国内用户来说很重要。详细的 release note

 

设置为默认 shell

通过命令 chsh 修改默认登录 shell,需要注意的是,如果通过 homebrew 安装了最新版本的 zsh,则需要 sudo 编辑 /etc/shells 加入一行 /usr/local/bin/zsh。然后再通过 chsh 来修改默认 shell,否则会提示 /usr/local/bin/zsh 不是合法的 shell。

 

安装 oh-my-zsh 配置

对于每一个像我这样的 zsh 初级用户来说,oh-my-zsh 就是救人于水火中的大杀器,强烈建议使用此配置上手 zsh。

作者提供了傻瓜安装命令:

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

也可以手工安装,具体步骤

 

几个必备的插件

autojump

帮助快速目录跳转的小工具。首先要安装 autojump,然后在 .zshrc 中开启 autojump 插件。它会记录下来每个你进入过的目录,随后通过 j 目录名称的一部分 就可快速跳转到该目录。 Youtube 视频介绍

git

Git 命令补全,除了可以补全 git 的子命令、命令开关等常规补全项以外,还可以补全分支名等内容,用 git 必开的插件。

osx

提供一些与 Mac OSX 系统交互的命令,比如:

  • man-preview 通过 preview 程序查看一个命令的手册,例如 man-preview git
  • quick-look 快速预览文件
  • pfd 返回当前 finder 打开的文件夹的路径
  • cdf 切换到当前 finder 所在的目录
 
来源:http://lostjs.com/2012/09/27/zsh/

 

由于一个莫名其妙的原因,突然想把系统的shell从bash换成终极shell——zsh,于是呢,就寻找了配置文件一份。

 

安装zsh:

sudo apt-get install zsh

 

配置文件(长长长,出自《开源世界旅行指南》,注释良好,茶叶修改过,请自行注释掉你不需要的功能):

#命令提示符 {{{
RPROMPT=$(echo '%{\033[31m%}%D %T%{\033[m%}')
PROMPT=$(echo '%{\033[34m%}%M%{\033[32m%}%/
%{\033[36m%}%n %{\033[01;31m%}>%{\033[33m%}>%{\033[34m%}>%{\033[m%} ')
#}}}

#标题栏、任务栏样式{{{
case $TERM in (*xterm*|*rxvt*|(dt|k|E)term)
  precmd () { print -Pn "\e]0;%n@%M//%/\a" }
  preexec () { print -Pn "\e]0;%n@%M//%/\ $1\a" }
  ;;
esac
#}}}

#关于历史纪录的配置 {{{
#历史纪录条目数量
export HISTSIZE=1000
#注销后保存的历史纪录条目数量
export SAVEHIST=1000
#历史纪录文件
#export HISTFILE=~/.zhistory
#以附加的方式写入历史纪录
setopt INC_APPEND_HISTORY
#如果连续输入的命令相同,历史纪录中只保留一个
#setopt HIST_IGNORE_DUPS      
#为历史纪录中的命令添加时间戳      
setopt EXTENDED_HISTORY      

#启用 cd 命令的历史纪录,cd -[TAB]进入历史路径
setopt AUTO_PUSHD
#相同的历史路径只保留一个
#asetopt PUSHD_IGNORE_DUPS

#在命令前添加空格,不将此命令添加到纪录文件中
#setopt HIST_IGNORE_SPACE      
#}}}

#每个目录使用独立的历史纪录{{{
cd() {
   builtin cd "$@"                             # do actual cd
   fc -W                                       # write current history  file
   local HISTDIR="$HOME/.zsh_history$PWD"      # use nested folders for history
       if  [ ! -d "$HISTDIR" ] ; then          # create folder if needed
           mkdir -p "$HISTDIR"
       fi
       export HISTFILE="$HISTDIR/zhistory"     # set new history file
   touch $HISTFILE
   local ohistsize=$HISTSIZE
       HISTSIZE=0                              # Discard previous dir's history
       HISTSIZE=$ohistsize                     # Prepare for new dir's history
   fc -R                                       #read from current histfile
}
mkdir -p $HOME/.zsh_history$PWD
export HISTFILE="$HOME/.zsh_history$PWD/zhistory"

function allhistory { cat $(find $HOME/.zsh_history -name zhistory) }
function convhistory {
           sort $1 | uniq |
           sed 's/^:\([ 0-9]*\):[0-9]*;\(.*\)/\1::::::\2/' |
           awk -F"::::::" '{ $1=strftime("%Y-%m-%d %T",$1) "|"; print }'  
}
#使用 histall 命令查看全部历史纪录
function histall { convhistory =(allhistory) |
           sed '/^.\{20\} *cd/i\\' }
#使用 hist 查看当前目录历史纪录
function hist { convhistory $HISTFILE }

#全部历史纪录 top44
function top44 { allhistory | awk -F':[ 0-9]*:[0-9]*;' '{ $1="" ; print }' | sed 's/ /\n/g' | sed '/^$/d' | sort | uniq -c | sort -nr | head -n 44 }

#}}}

#a杂项 {{{
#允许在交互模式中使用注释  例如:
#cmd #这是注释
setopt INTERACTIVE_COMMENTS      
     
#启用自动 cd,输入目录名回车进入目录
#稍微有点混乱,不如 cd 补全实用
#setopt AUTO_CD
     
#扩展路径
#/v/c/p/p => /var/cache/pacman/pkg
setopt complete_in_word
     
#禁用 core dumps
limit coredumpsize 0

#Emacs风格 键绑定
bindkey -e
#设置 [DEL]键 为向后删除
bindkey "\e[3~" delete-char

#以下字符视为单词的一部分
#WORDCHARS='*?_-[]~=&;!#$%^(){}<>'
##LEO注释了这部分
#}}}


#自动补全功能 {{{
setopt AUTO_LIST
setopt AUTO_MENU
#开启此选项,补全时会直接选中菜单项
#setopt MENU_COMPLETE

autoload -U compinit
compinit

#自动补全缓存
#zstyle ':completion::complete:*' use-cache on
#zstyle ':completion::complete:*' cache-path .zcache
#zstyle ':completion:*:cd:*' ignore-parents parent pwd

#自动补全选项
zstyle ':completion:*:match:*' original only
zstyle ':completion::prefix-1:*' completer _complete
zstyle ':completion:predict:*' completer _complete
zstyle ':completion:incremental:*' completer _complete _correct
zstyle ':completion:*' completer _complete _prefix _correct _prefix _match _approximate

#路径补全
zstyle ':completion:*' expand 'yes'
zstyle ':completion:*' squeeze-shlashes 'yes'
zstyle ':completion::complete:*' '\\'

zstyle ':completion:*' menu select
zstyle ':completion:*:*:default' force-list always

#彩色补全菜单
eval $(dircolors -b)
export ZLSCOLORS="${LS_COLORS}"
zmodload zsh/complist
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'

#修正大小写
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}'
#错误校正      
zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric

#kill 命令补全      
compdef pkill=kill
compdef pkill=killall
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:*:*:*:processes' force-list always
zstyle ':completion:*:processes' command 'ps -au$USER'

#补全类型提示分组
zstyle ':completion:*:matches' group 'yes'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:options' auto-description '%d'
zstyle ':completion:*:descriptions' format $'\e[01;33m -- %d --\e[0m'
zstyle ':completion:*:messages' format $'\e[01;35m -- %d --\e[0m'
zstyle ':completion:*:warnings' format $'\e[01;31m -- No Matches Found --\e[0m'
#}}}

##行编辑高亮模式 {{{
# Ctrl+@ 设置标记,标记和光标点之间为 region
zle_highlight=(region:bg=magenta #选中区域
              special:bold      #特殊字符
              isearch:underline)#搜索时使用的关键字
#}}}

##空行(光标在行首)补全 "cd " {{{
user-complete(){
   case $BUFFER in
       "" )                       # 空行填入 "cd "
           BUFFER="cd "
           zle end-of-line
           zle expand-or-complete
           ;;
       "cd --" )                  # "cd --" 替换为 "cd +"
           BUFFER="cd +"
           zle end-of-line
           zle expand-or-complete
           ;;
       "cd +-" )                  # "cd +-" 替换为 "cd -"
           BUFFER="cd -"
           zle end-of-line
           zle expand-or-complete
           ;;
       * )
           zle expand-or-complete
           ;;
   esac
}
zle -N user-complete
bindkey "\t" user-complete
#}}}

##在命令前插入 sudo {{{
#定义功能
#sudo-command-line() {
#    [[ -z $BUFFER ]] && zle up-history
#    [[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER"
#    zle end-of-line                 #光标移动到行末
#}
#zle -N sudo-command-line
#定义快捷键为: [Esc] [Esc]
#bindkey "\e\e" sudo-command-line
#}}}
 
#命令别名 {{{
#alias -g cp='cp -i'
#alias -g mv='mv -i'
#alias -g rm='rm -i'
alias -g ls='ls -X -F --color=auto'
alias -g ll='ls -l'
alias -g grep='grep --color=auto'
alias -g la='ls -a'
#alias -g ee='emacsclient -t'
#alias -g tk='tmux kill-server'
#哈哈哈,我现在有了byobu
alias -g bb='byobu-tmux'
alias -g x='exit'
alias -g t='faweibo -t'
alias -g tm='tmux'

alias -g f='cat ~/日程'

#还是算了吧
#alias -g gcc='gcc -Wall'

#alias -g cd....='cd ../..'
#Well this must consult Gods later.__Leo

#[Esc][h] man 当前命令时,显示简短说明
alias run-help >&/dev/null && unalias run-help
autoload run-help

#历史命令 top10
alias top10='print -l  ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
#}}}

#路径别名 {{{
#进入相应的路径时只要 cd ~xxx
#hash -d WWW="/home/lighttpd/html"
#hash -d ARCH="/mnt/arch"
#hash -d PKG="/var/cache/pacman/pkg"
#hash -d E="/etc/env.d"
#hash -d C="/etc/conf.d"
#hash -d I="/etc/rc.d"
#hash -d X="/etc/X11"
#hash -d BK="/home/r00t/config_bak"
hash -d lc="/home/leo/dev/learnC"
hash -d di="/home/leo/文档/日记"
#}}}
   
##afor Emacs {{{
#在 Emacs终端 中使用 Zsh 的一些设置 不推荐在 Emacs 中使用它
if [[ "$TERM" == "dumb" ]]; then
setopt No_zle
PROMPT='%n@%M %/
>>'
alias ls='ls -F'
fi     
#}}}

#{{{自定义补全
#补全 ping
zstyle ':completion:*:ping:*' hosts 192.168.128.1{38,} www.g.cn \
      192.168.{1,0}.1{{7..9},}

#补全 ssh scp sftp 等
#my_accounts=(
#{r00t,root}@{192.168.1.1,192.168.0.1}
#kardinal@linuxtoy.org
#123@211.148.131.7
#)
#zstyle ':completion:*:my-accounts' users-hosts $my_accounts
#}}}

####{{{
function calc { echo $(($@)) }
function timeconv { date -d @$1 +"%Y-%m-%d %T" }

# }}}


## END OF FILE #################################################################
# vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4 

 

下面介绍Byobu

 

安装一个:

sudo apt-get install byobu

 

简单配置下:

~/.byobu/keybindings.tmux

#绑定一个F12切换
set -g prefix F12
unbind-key -n C-a
bind a send-prefix

unbind '"'
bind - splitw -v
unbind %
#bind | splitw -h
bind \ splitw -h

bind k selectp -U
bind j selectp -D
bind h selectp -L
bind l selectp -R

 

按下F12,然后再按下hjkl就可以切换格子了。

 

按下F2打开新窗口,F3、F4负责左右切换窗口。

我就用到这些功能了。

 

来源:http://leosong.diandian.com/post/2012-07-13/40030151442

 

在 Ubuntu Linux 中安裝 Zsh 及 Oh-my-zsh

直接用 apt-get 安裝 zsh 套件

$ apt-get install zsh
$ zsh --verison
zsh 4.3.17

 

從 GitHub 下載 oh-my-zsh 套件

$ git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

 

如果本來沒有安裝 zsh 可以直接使用 oh-my-zsh 的範例 zshrc

$ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

 

看看有什麼 Theme 可以用

$ ls ~/.oh-my-zsh/themes

 

編輯 ~/.zshrc 更換 zsh 的 theme 我自己喜歡用 candy

ZSH_THEME="candy"

 

看看有什麼 Plugin 可以用

$ ls ~/.oh-my-zsh/plugins

 

編輯 ~/.zshrc 啟用 Plugin

plugins=(git git-flow debian grails rvm history-substring-search github gradle svn node npm zsh-syntax-highlighting sublime)

 

下載 zsh-syntax-highlighting plugin

$ cd ~/.oh-my-zsh/custom/plugins
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git

 

新增自訂 zsh 設定,我會把 alias 和 PATH 的設定放在這邊

$ cat ~/.oh-my-zsh/custom/xxx.zsh
alias df='df -h'
alias h='htop'

PATH=$PATH:/opt/app/bin/

 

来源:http://shyuan.github.io/blog/2012/07/10/install-zsh-and-oh-my-zsh-in-ubuntu-linux/

 

Make ZSH default

chsh -s /bin/zsh

 

Restart your system.

 

关联: https://github.com/robbyrussell/oh-my-zsh

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics