是时候提高工作效率了:oh-my-zsh与tmux的组合

发表于 2016-12-27
更新于 2024-05-23
分类于 技术专栏
阅读量 3425
字数统计 8073

前言

终于有一天看到了一个好酷的Shell,于是果断放弃了使用N多年的Bash,从此开始了漫漫不归路了呀......

1、好看也好用的Shell:oh-my-zsh

oh-my-zsh的流行程度可以在github的Star数和fork数看出来,原谅以前的无知啊,竟然不知道这么好用的一个东东。

不过现在知道了,为时不晚,于是果断撸起这个玩意。

1.1、安装zsh

因为是基于zsh,所以首先你得确认你的系统是否存在zsh?

zsh --version

如果不存zsh,在Ubuntu下,可以直接使用下面命令安装:apt-get install zsh

接着切换你的默认Shell:chsh -s $(which zsh)

在重新注销登录,可以使用下面命令验证是否更改成功:echo $SHELL

1.2、安装oh-my-zsh

一条命令搞定:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

1.3、定制化

然后喜欢鼓捣的人就有理由来折腾这种主题和插件丰富的软件,而我也就大概玩了一下,满足自己那颗好奇的心就行了。我使用的.zshrc文件如下:

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

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

# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="random"

# 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 node npm bower git-flow nvm github svn svn-fast-info)

source $ZSH/oh-my-zsh.sh

# User configuration
export PATH="/root/.nvm/versions/node/v6.4.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
# export MANPATH="/usr/local/man:$MANPATH"
export MANPATH="/root/.nvm/versions/node/v6.4.0/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man"
export NVM_BIN="/root/.nvm/versions/node/v6.4.0/bin"
export NVM_CD_FLAGS=""
export NVM_DIR="/root/.nvm"
export NVM_IOJS_ORG_MIRROR="https://iojs.org/dist"
export NVM_NODEJS_ORG_MIRROR="https://nodejs.org/dist"
export NVM_PATH="/root/.nvm/versions/node/v6.4.0/lib/node"

# 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"

# -------------------------------------------------------------------
# some alias settings, just for fun
# -------------------------------------------------------------------
# alias 'today=calendar -A 0 -f ~/calendar/calendar.mark | sort'
alias 'today=calendar -A 0 -f /usr/share/calendar/calendar.mark | sort'
alias 'dus=du -sckx * | sort -nr'
alias 'adventure=emacs -batch -l dunnet'
alias 'mailsize=du -hs ~/Library/mail'
alias 'bk=cd $OLDPWD'
alias 'ttop=top -ocpu -R -F -s 2 -n30'
alias lh='ls -a | egrep "^\."'

# -------------------------------------------------------------------
# Git
# -------------------------------------------------------------------
alias ga='git add'
alias gp='git push'
alias gl='git log'
alias gs='git status'
alias gd='git diff'
alias gm='git commit -m'
alias gma='git commit -am'
alias gb='git branch'
alias gc='git checkout'
alias gra='git remote add'
alias grr='git remote rm'
alias gpu='git pull'
alias gcl='git clone'
alias gta='git tag -a -m'
alias gf='git reflog'

#
# Tmux
#
alias tnew='tmux -f ~/.tmux.conf new-session \; split-window -h \; split-window -v \; attach'


# -------------------------------------------------------------------
# OTHER aliases
# -------------------------------------------------------------------

alias cl='clear'



# myIP address
function myip() {
    ifconfig lo0 | grep 'inet ' | sed -e 's/:/ /' | awk '{print "lo0       : " $2}'
    ifconfig en0 | grep 'inet ' | sed -e 's/:/ /' | awk '{print "en0 (IPv4): " $2 " " $3 " " $4 " " $5 " " $6}'
    ifconfig en0 | grep 'inet6 ' | sed -e 's/ / /' | awk '{print "en0 (IPv6): " $2 " " $3 " " $4 " " $5 " " $6}'
    ifconfig en1 | grep 'inet ' | sed -e 's/:/ /' | awk '{print "en1 (IPv4): " $2 " " $3 " " $4 " " $5 " " $6}'
    ifconfig en1 | grep 'inet6 ' | sed -e 's/ / /' | awk '{print "en1 (IPv6): " $2 " " $3 " " $4 " " $5 " " $6}'
}

其中注意几个重点:

  1. 主题的设置,我这里使用了随机主题,每次启动都是不一样的^_^
  2. 环境变量的设置,即export PATH等等,记得将其从之前的Shell中复制过来
  3. alias的设置,非常有用的,将那些常用的命令在这里使用缩写的形式配置出来
  4. tnew命令的设置,用来直接启用tmux并且出现三个窗口(一纵两横)

2、tmux的使用

因为我一直使用XShell来远程登录我的Ubuntu,有的时候不想要多开Xshell窗口,所以就得使用tmux(terminal multiplexer)。关于它的介绍网上很多,在使用过程中主要遇到了两个问题:

  1. tmux的复制粘贴模式
  2. tmux的滚轮模式

对应的问题解决方案可以参考下面的链接6和7。唯一没能解决的是,目前无法做到在tmux中复制的信息让windows系统获取到,只能在tmux内部使用。这个比较纠结!

在tmux的复制可以使用下面的过程:(基于下面的.tmux.conf)

  1. Ctrl+a+[ 进入复制模式
  2. 使用上下键转到你想复制的地方
  3. 敲空格键表示开始复制
  4. 使用上下左右键来选择你想要复制的文本
  5. y键表示复制结束
  6. Ctrl+a+v粘贴你刚才选中的文本

对应的配置文件:(tmux默认在/etc/.tmux.conf下查找,没有找到的话到~/.tmux.conf下查找,你也可以在启动tmux的时候指定配置文件使用-f参数)

This requires tmux 2.1. a lot of these settings will error on anything earlier.

# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# Act like Vim; use h,j,k,l to select panes and move the cursor
set-window-option -g mode-keys vi
bind-key h select-pane -L
bind-key j select-pane -D
bind-key k select-pane -U
bind-key l select-pane -R


# switch panes using Alt-arrow without prefix
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D


# Look good
set-option -g default-terminal "screen-256color"


# Enable mouse support (works in iTerm)
set -g -q mouse on

bind -t vi-copy y copy-pipe "xclip -sel clip -i"

# scrollback buffer size increase (this eats up memory, sometimes)
set -g history-limit 500000

# C-b C-b will swap to last used window
bind-key C-b last-window

# Start tab numbering at 1
set -g base-index 1
set-window-option -g pane-base-index 1

# Allows for faster key repetition
set -s escape-time 0

# Highlight active window in tab-bar at bottom in red
set-window-option -g window-status-current-bg red

# use different keys to split vertical and horizonal
# ^B | for vertical split
# ^B - for horizontal split
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# Change cursor in vim to distinguish between insert and command mode
# Use in conjunction with tmux-cursors.vim
set-option -g terminal-overrides '*88col*:colors=88,*256col*:colors=256,xterm*:XT:Ms=\E]52;%p1%s;%p2%s\007:Cc=\E]12;%p1%s\007:Cr=\E]112\007:Cs=\E]50;CursorShape=%?%p1%{3}%<%t%{0}%e%p1%{2}%-%;%d\007'


# use C-b v to paste the tmux buffer
bind v paste-buffer

# reload config file (change file location to your the tmux.conf you want to use)
bind r source-file ~/.tmux.conf

# split the screen into a main screen and an 80-width pane on the right
bind -n C-g split-window -h -c "#{pane_current_path}" \; resize-pane -x 80 \; select-pane -t 0

# split the screen into main screen with 16-tall pane on the bottom
bind g split-window -v -c "#{pane_current_path}"\; resize-pane -y 16 \; select-pane -t 0

# move current window left and right in the tab list, respectively.
bind '<' swap-window -t -1
bind '>' swap-window -t +1

# ^B \ : resize the currently selected pane to be 80 columns
bind '\' resize-pane -x 80

# ^B ] : resize the currently selected pane to 16 rows high
bind ']' resize-pane -y 16

注意:有的时候你修改了配置,想要让其生效,可以使用下面两种方法:

  1. 关闭tmux服务器:
    tmux ls 
    killall tmux
    
  2. tmux source ~/.tmux.conf

最后

工具的配置依托于个人的使用情况,仅供参考。后续会不断更新这种配置,保证自己使用的时候可以达到比较完美的状态。

参考:

  1. https://github.com/robbyrussell/oh-my-zsh/wiki/Installing-ZSH
  2. https://github.com/robbyrussell/oh-my-zsh/wiki
  3. https://github.com/robbyrussell/oh-my-zsh
  4. https://gist.github.com/spikegrobstein/5724689
  5. https://github.com/tmux/tmux
  6. https://coderwall.com/p/4b0d0a/how-to-copy-and-paste-with-tmux-on-ubuntu
  7. http://www.rushiagr.com/blog/2016/06/16/everything-you-need-to-know-about-tmux-copy-pasting-ubuntu/

公众号关注一波~

微信公众号

关于评论和留言

如果对本文 是时候提高工作效率了:oh-my-zsh与tmux的组合 的内容有疑问,请在下面的评论系统中留言,谢谢。

网站源码:linxiaowu66 · 豆米的博客

Follow:linxiaowu66 · Github