zsh安装和配置

zsh安装和配置

Zsh是一个Linux用户很少使用的power-shell,这是由于大多数Linux产品安装,以及默认使用bash shell。几乎每一款Linux产品都包含有zsh,通常可以用apt-get、urpmi或yum进行安装。zsh是兼容bash的,但是又比bash好用,有什么理由不用zsh呢?

zsh的安装

在终端中输入下面命令进行安装

sudo apt-get install zsh

安装完毕以后,你需要将zsh替换为你的默认shell,之前默认的shell为Bourne Again Shell (bash),输入下面命令进行替换

chsh -s /bin/zsh

ok,现在关闭你的终端,重新打开你的终端就可以啦

zsh的配置

一开始安装zsh时,zsh界面个人感觉是比较丑的,还好zsh有强大的配置功能,你可以任意配置你的zsh成你想要的样子,不过这里我们为了方便配置,我们采用oh-my-zsh来进行配置,oh-my-zsh是为了简化zsh的配置而提供的一个配置模板,可以更好的管理zsh的各项配置。下面我们就来安装oh-my-zsh

首先你需要安装curl或者是wget

常规安装oh-my-zsh

通过curl安装

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

通过wget安装

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

手动安装oh-my-zsh

当然你也一颗手动安装,oh-my-zsh的源码是托管于github上的

克隆仓库里面的代码

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

备份你的zsh配置

cp ~/.zshrc ~/.zshrc.orig

创建一个新的zsh配置文件

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

改变默认的shell

chsh -s /bin/zsh

修改配置文件~/.zshrc

在你安装好oh-my-zsh以后,你的主目录下面将会有一个.oh-my-zsh目录和.zshrc配置文件。.oh-my-zsh里面存放着zsh的各种配置模板和插件,.zshrc就是在每次打开zsh的时候都会读取的配置文件啦。

首次打开你的终端时你会看到这样的zsh界面:

是不是和bash的感觉完全不一样了,个人感觉是更好看啦

我们现在需要修改的就是配置文件啦,打开主目录下的.zshrc

你会看到下面的配置

找到ZSH_THEME="robbyrussell"这一行,这一行主要是配置你的zsh的提示信息的样式的,现在我们来替换成其他的主题样式

我们将主题替换成了ZSH_THEME="xiong-chiamiov-plus",退出终端,重新打开一个终端,你会发现变成其他样式啦

哈哈,是不是变得更加漂亮啦?如果你想要更多的主题样式,你可以到这里去找你喜欢的主题:https://github.com/robbyrussell/oh-my-zsh/wiki/themes

那要是上面的主题你都不喜欢,没关系,自己制作一个主题,打开~/.oh-my-zsh/themes目录下面的任意一个文件作为你修改的模板,博主在这里打开的是

xiong-chiamiov-plus.zsh-theme这个文件

打开文件以后,你会发现,这都是些啥啊???

仔细一看,其实很简单,大家可以参照这篇博文对这个配置文件进行修改《linux环境变量设置(PS1,PS2)》http://blog.csdn.net/lushujun2011/article/details/7351926

下面是博主的设置文件,下载地址:《zsh与oh-my-zsh终极配置文件》http://download.csdn.net/detail/ii1245712564/8713231

# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh

# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it‘ll load a random theme each
# time that oh-my-zsh is loaded.
ZSH_THEME="xiong-chiamiov-plus"

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

# User configuration

export PATH=$HOME/bin:/usr/local/bin:$PATH
# export MANPATH="/usr/local/man:$MANPATH"

source $ZSH/oh-my-zsh.sh

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR=‘vim‘
# else
#   export EDITOR=‘mvim‘
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# ssh
# export SSH_KEY_PATH="~/.ssh/dsa_id"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

#color{{{
autoload colors
colors

for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
eval _$color=‘%{$terminfo[bold]$fg[${(L)color}]%}‘
eval $color=‘%{$fg[${(L)color}]%}‘
(( count = $count + 1 ))
done
FINISH="%{$terminfo[sgr0]%}"
#}}}

#命令提示符
#RPROMPT=$(echo "$RED%D %T$FINISH")
#PROMPT=$(echo "$CYAN%[email protected]$YELLOW%M:$GREEN%/$_YELLOW>$FINISH ")

#PROMPT=$(echo "$BLUE%M$GREEN%/
#$CYAN%[email protected]$BLUE%M:$GREEN%/$_YELLOW>>>$FINISH ")
#标题栏、任务栏样式{{{
case $TERM in (*xterm*|*rxvt*|(dt|k|E)term)
precmd () { print -Pn "\e]0;%[email protected]%M//%/\a" }
preexec () { print -Pn "\e]0;%[email protected]%M//%/\ $1\a" }
;;
esac
#}}}

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

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

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

#每个目录使用独立的历史纪录{{{
cd() {
builtin cd "[email protected]"                             # 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 }

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

#}}}

#杂项 {{{
#允许在交互模式中使用注释  例如:
#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
#bindkey -v
#设置 [DEL]键 为向后删除
#bindkey "\e[3~" delete-char

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

#自动补全功能 {{{
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:*‘ verbose yes
zstyle ‘:completion:*‘ menu select
zstyle ‘:completion:*:*:default‘ force-list always
zstyle ‘:completion:*‘ select-prompt ‘%SSelect:  lines: %L  matches: %M  [%p]‘

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:*‘ ‘\\‘

#彩色补全菜单
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‘
zstyle ‘:completion:*:corrections‘ format $‘\e[01;32m -- %d (errors: %e) --\e[0m‘

# cd ~ 补全顺序
zstyle ‘:completion:*:-tilde-:*‘ group-order ‘named-directories‘ ‘path-directories‘ ‘users‘ ‘expand‘
#}}}

##行编辑高亮模式 {{{
# [email protected] 设置标记,标记和光标点之间为 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 cp=‘cp -i‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias ls=‘ls -F --color=auto‘
alias ll=‘ls -al‘
alias grep=‘grep --color=auto‘
alias la=‘ls -a‘
alias pacman=‘sudo pacman-color‘
alias p=‘sudo pacman-color‘
alias y=‘yaourt‘
alias h=‘htop‘
alias vim=‘vim‘

#[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 A="/media/ayu/dearest"
hash -d H="/media/data/backup/ayu"
hash -d E="/etc/"
hash -d D="/home/ayumi/Documents"
#}}}

##for Emacs {{{
#在 Emacs终端 中使用 Zsh 的一些设置 不推荐在 Emacs 中使用它
#if [[ "$TERM" == "dumb" ]]; then
#setopt No_zle
#PROMPT=‘%[email protected]%M %/
#>>‘
#alias ls=‘ls -F‘
#fi
#}}}

#{{{自定义补全
#补全 ping
zstyle ‘:completion:*:ping:*‘ hosts 192.168.1.{1,50,51,100,101} www.google.com

#补全 ssh scp sftp 等
#zstyle -e ‘:completion::*:*:*:hosts‘ hosts ‘reply=(${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) /dev/null)"}%%[# ]*}//,/ })‘
#}}}

#{{{ F1 计算器
arith-eval-echo() {
LBUFFER="${LBUFFER}echo \$(( "
RBUFFER=" ))$RBUFFER"
}
zle -N arith-eval-echo
bindkey "^[[11~" arith-eval-echo
#}}}

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

# }}}

zmodload zsh/mathfunc
autoload -U zsh-mime-setup
zsh-mime-setup
setopt EXTENDED_GLOB
#autoload -U promptinit
#promptinit
#prompt redhat

setopt correctall
autoload compinstall

#漂亮又实用的命令高亮界面
setopt extended_glob
 TOKENS_FOLLOWED_BY_COMMANDS=(‘|‘ ‘||‘ ‘;‘ ‘&‘ ‘&&‘ ‘sudo‘ ‘do‘ ‘time‘ ‘strace‘)

 recolor-cmd() {
     region_highlight=()
     colorize=true
     start_pos=0
     for arg in ${(z)BUFFER}; do
         ((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]## #}}))
         ((end_pos=$start_pos+${#arg}))
         if $colorize; then
             colorize=false
             res=$(LC_ALL=C builtin type $arg 2>/dev/null)
             case $res in
                 *‘reserved word‘*)   style="fg=magenta,bold";;
                 *‘alias for‘*)       style="fg=cyan,bold";;
                 *‘shell builtin‘*)   style="fg=yellow,bold";;
                 *‘shell function‘*)  style=‘fg=green,bold‘;;
                 *"$arg is"*)
                     [[ $arg = ‘sudo‘ ]] && style="fg=red,bold" || style="fg=blue,bold";;
                 *)                   style=‘none,bold‘;;
             esac
             region_highlight+=("$start_pos $end_pos $style")
         fi
         [[ ${${TOKENS_FOLLOWED_BY_COMMANDS[(r)${arg//|/\|}]}:+yes} = ‘yes‘ ]] && colorize=true
         start_pos=$end_pos
     done
 }
check-cmd-self-insert() { zle .self-insert && recolor-cmd }
 check-cmd-backward-delete-char() { zle .backward-delete-char && recolor-cmd }

 zle -N self-insert check-cmd-self-insert
 zle -N backward-delete-char check-cmd-backward-delete-char
时间: 2024-08-09 00:24:59

zsh安装和配置的相关文章

Z shell(zsh)安装-更新-配置-使用

有人演示了一把zsh,看着是挺爽的.这个shell一定有独到之处,于是就捣鼓一把. zsh是同bash一样,也是一中shell,只不过它有自己的不同之处,或优或劣.开始写这篇记录的时候,只是安装配置好它了,还没有来得及尝试体验其“优劣”. 1.安装.zsh在一般的linux发行版本中都默认包含了.看一下你的机器上都有什么shell呢?下面是我的机器上的shell $cat /etc/shells/bin/sh/bin/bash/sbin/nologin/bin/tcsh/bin/csh/bin/

ubuntu 安装并配置zsh

参考: 1.https://www.jianshu.com/p/4fde9ae77922 2.https://www.cnblogs.com/dhcn/p/11666845.html 一.安装zsh 1.源码安装 (1)安装: wget https://sourceforge.net/projects/zsh/files/latest/download tar -xvJf zsh-5.7.1.tar.xz cd zsh-5.7.1 ./configure make sudo make insta

Zsh安装CMake补全脚本进行CMake命令补全

最近在尝试使用Zsh,发现其补全命令的功能相当厉害.但对CMake命令的补全在默认的5.0.5中好像没有看到,网上找了下关于配置Zsh补全的文章也没有多少.     于是自己动手,发现在Zsh安装目录下有个functions的文件夹,里面都有以_开头的各种补全提示文件,比如_sed,_awk等,于是尝试在网上 下到zsh_cmake补全文件之后,重命名为_cmake放置该文件夹中(注意Windows用户的换行符,在这个上浪费了很多时间,使用 dos2unix可以解决)重新启动Zsh,享受CMak

iTerm的安装以及配置

iTerm是类似于DOS命令行的一个工具,和苹果终端类似,但是可以自定义一些配置,本文就是简单的说一下安装和简单的配置过程. 首先就是下载iTerm可以从官网上下载也可以从我的云盘上下载,云盘上的有配置文件,建议新手直接用,官网的下载下来界面不是很好看,高手可以自己调,怎么调今天就先不说了,网上有很多的资料. 官网的地址: http://www.iterm2.com/ 官网下载地址: https://iterm2.com/downloads/stable/iTerm2-3_0_14.zip(这个

Arch Linux 安装、配置、美化和优化

国庆假期玩了下Arch Linux,发现这货跟Ubuntu之流相差甚远,甚难调教,而且安裝过程全命令行,会有各种问题,各种知识... --- 安装引导器--- --------------------------- GRUB BIOS: # pacman -S grub os-prober # grub-install --recheck /dev/<目标磁盘> # grub-mkconfig -o /boot/grub/grub.cfg UEFI:---如果BIOS是UEFI的,就要用下面的

oh-my-zsh安装与配置

1.安装与配置 Setup oh-my-zshshould work with any recent release of Zsh. The minimum recommended version is 4.3.9. If not already installed, you can install Zsh using the command-line. The automatic installer- do you trust me? You can install this via the

ubuntu16安装与配置

1. ubuntu安装与配置 1.1. 换源 1.2. 卸载libreoffice sudo apt remove libreoffice-common 1.3. 安装git sudo apt install git 1.4. 安装与配置zsh 安装zsh sudo apt install zsh 安装oh-my-zsh sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -

Windows下MySQL下载安装、配置与使用

用过MySQL之后,不论容量的话,发现比其他两个(sql server .oracle)好用的多,一下子就喜欢上了.下面给那些还不知道怎么弄的童鞋们写下具体的方法步骤. (我这个写得有点太详细了,甚至有些繁琐,有很多步骤在其他的教程文档里都是省略掉的,但我还是要写出来,因为我当时走了很多弯路,我希望你们能够避免我走的这些弯路.当然,知道同学的可以略过,选择你想知道的地方看) 第一大步:下载.(不需要注册也可以下载,直接点下面的no thanks) a.俗话说:“巧妇难为无米之炊”嘛!我这里用的是

Centos 7.3下 Linux For SQL Server安装及配置介绍

Centos 7.3下Linux For SQL Server安装及配置介绍 说到SQL Server服务,我们大家都知道是Microsoft公司的数据库服务,当然说到数据库,现在主要分为三大商:1:Oracle.2:Msql Server.3:Mysql:三种数据库在当下环境受到不了不同程度的关注:比如oracle主要应用到大型的商业比较多,比如银行:SQL Server主要在常见的互联网公司使用:mysql主要应用于小型的企业或者服务商使用:当然从费用上来说,Oracle是最贵的,也是最为稳