ZSH
Summary: Author: 张亚飞 | Read Time: 2 minute read | Published: 2017-08-12
Filed under
—
Categories:
Linux
—
Tags:
Note,
ZSH
brew install zsh
sudo apt install zsh
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
sudo ~/rs/ConSendSync.sh mac
chsh -s /bin/zsh
安装 ZSH
- 以mac 为例,我们看下系统内置了几种shell
coam@MacPro:~$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
Mac 自带 zsh,不过我们当然要用最新版啦,其他发行版请参照各自的软件安装方法.安装完毕后,重新打开 iTerm2.
brew install zsh
chsh -s `which zsh`
Mac 默认已安装 zsh , Linux 默认没有安装,需要手动安装
//sudo yum install zsh
sudo apt install zsh
shell 切换
echo $SHELL
cat /etc/shell
chsh -s /bin/bash
chsh -s /bin/zsh
注 具体路径使用 cat /etc/shell 查看,不用 which zsh 命令.chsh -s其实修改的就是/etc/passwd文件里和你的用户名相对应的那一行
大多数系统默认的shell是bash,但是可以修改为其他shell.比如最近比较火的 oh-my-zsh 就是 zsh 的一种配置.安装好新的shell,使用 chsh 命令(即 change shell)即可设置为默认shell.
但是今天用 brew 安装最新的 zsh 后,使用
chsh -s /usr/local/bin/zsh
修改默认shell时,报错
chsh: /usr/local/bin/zsh: non-standard shell
但是直接运行 /usr/local/bin/zsh 却是正常的.
经查, /etc/shells 文件中,包含了 login shell 的列表,应用程序通过该文件来判断一个shell是不是合法.我的系统里该文件内容如下:
List of acceptable shells for chpass(1).
Ftpd will not allow users to connect who are not using
one of these shells.
/bin/bash /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh
因此,必须先将 /usr/local/bin/zsh 加入到此文件中,然后才能使用 chsh 命令将其设置为默认shell.
安装
mac 预装了zsh ,但是很少有人直接切换过来使用此shell ,因为 zsh 的默认配置及其复杂繁琐,让人望而却步,直到有了oh-my-zsh这个开源项目,让zsh配置降到0门槛.而且它完全兼容 bash .
通过 curl 安装
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
- 配置 .zshrc
每次开启新终端,配置随机主题
vi .zshrc
ZSH_THEME="random" # (...please let it be pie... please be some pie..)
记录不错的主题
- 插件
plugins=(git autojump osx brew node npm) //zsh 使用的插件 默认配置 git
incr.zsh 补全插件 让你在zsh 模式下全自动补全指令或目录
mkdir -p .oh-my-zsh/plugins/incr
cd .oh-my-zsh/plugins/incr
wget http://mimosa-pudica.net/src/incr-0.2.zsh
- 添加到 .zshrc 最后一行
vi .zshrc
source ~/.oh-my-zsh/plugins/incr/incr*.zsh
source .zshrc
常见用法
常见问题
安装完 zsh 和 oh-my-zsh 并设置为 zsh 后,在 Mac 笔记本登录没有显示对应的服务器 IP
之前在 bash 环境下是在 ~/.bashrc 添加如下行
vi ~/.bashrc
# Auto add env parameter $PROMPT_COMMAND when use non-Linux tty login by ssh.
# 让 putty 登陆的标题栏显示对应服务器 IP
if [ "$SSH_CONNECTION" != '' -a "$TERM" != 'linux' ]; then
declare -a HOSTIP
HOSTIP=`echo $SSH_CONNECTION |awk '{print $3}'`
export PROMPT_COMMAND='echo -ne "\033]0;${USER}@$HOSTIP:[${HOSTNAME%%.*}]:${PWD/#$HOME/~} \007"'
fi
切换成 zsh 环境后没有使用 .bashrc 的配置,需要修改 .oh-my-zsh/lib/termsupport.zsh 并添加两处 HOSTIP 字段
vi .oh-my-zsh/lib/termsupport.zsh
function title {
emulate -L zsh
setopt prompt_subst
[[ "$EMACS" == *term* ]] && return
# if $2 is unset use $1 as default
# if it is set and empty, leave it as is
: ${2=$1}
HOSTIP=`echo $SSH_CONNECTION |awk '{print $3}'`
case "$TERM" in
cygwin|xterm*|putty*|rxvt*|ansi)
print -Pn "\e]2;$HOSTIP || $2:q\a" # set window name
print -Pn "\e]1;$1:q\a" # set tab name
;;
screen*)
print -Pn "\ek$1:q\e\\" # set screen hardstatus
;;
*)
if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
#print -Pn "\e]2;$2:q\a" # set window name
print -Pn "\e]2;$2:q\a" # set window name
print -Pn "\e]1;$1:q\a" # set tab name
else
# Try to use terminfo to set the title
# If the feature is available set title
if [[ -n "$terminfo[fsl]" ]] && [[ -n "$terminfo[tsl]" ]]; then
echoti tsl
print -Pn "$1"
echoti fsl
fi
fi
;;
esac
}
Comments