Ultimate Shell: Zsh (CentOS7 installation zsh + Oh my zsh + tmux)

CentOS7 installation zsh and configuration Oh my zsh

When we operate the Linux terminal through the Shell, most of the configuration, color differentiation, and command prompts cannot achieve the results we expected or the operations are relatively cumbersome.

Today I will introduce the ultimate and easy-to-use terminal software for Linux-like systems. It is called the sports car among carriages, the flying car among sports cars, and the “Ultimate Shell” in history.

1 Install zsh and change the default terminal

① Install the software package

yum -y install zsh git

②Change the default terminal

chsh -s /bin/zsh

When you open the terminal software at this time, you should enter zsh by default.

2 Configure oh-my-zsh

① Pull oh-my-zsh from the igt warehouse

git clone https://gitee.com/mirrors/oh-my-zsh.git ~/.oh-my-zsh

②Default configuration

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

3 Install highlighting and auto-completion plug-ins

①Install the highlighting plug-in: zsh-syntax-highlighting

git clone https://gitee.com/dawnwords/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

②Install automatic completion: zsh-autosuggestions

git clone https://gitee.com/lhaisu/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

③Install autojump directory jump

git clone https://gitee.com/gentlecp/autojump.git
cd autojump
./install.py

4 plugin configuration

Also open the .zshrc file, find plugins=(git), and add the plugins you want here. Use spaces or newlines to separate multiple plugin names (commas are not allowed)

vim ~/.zshrc
plugins=(
        git
        sudo
        zsh-autosuggestions
        zsh-syntax-highlighting
        autojump
)

Every time we modify .zshrc we need to:

source ~./zshrc

If the prompt is the command you want, press the right arrow (→) to complete the command.

5 effects

If you are not used to this zsh theme, you can modify it

vim ~/.zshrc

Find ZSH_THEME and change it to your favorite theme, such as maran

#Refresh zsh configuration file
source ~/.zshrc

Effect:

6 tmux: Configure terminal management

Sometimes when we run script commands through the terminal, we don’t want the script program to exit when we exit the terminal.

  • At this time, it is necessary to use tmux, this terminal management artifact.

6.1 Concepts: workspace, window, pane

  1. Workspace: The workspace is the highest level of tmux and can contain multiple windows. Each workspace has a unique name, and you can create and manage multiple workspaces as needed.
  2. Window: A window is a child of the workspace and can contain one or more panes. Each window has a number and an optional name. You can switch between windows, and each window can run a different application or command.
  3. Pane: A pane is a child of the window, which is the visible area in tmux. Each pane can display a different command line interface or terminal session. Panes can be split vertically or horizontally, and can be navigated and resized between panes.

6.2 Installation and use

The v3.3 version is used here

  1. Update packages and download tmux
# If you have installed tmux and it is not version 3.3, you can uninstall it with the following command
sudo yum remove tmux
# Update packages and install
sudo yum install http://galaxy4.net/repo/galaxy4-release-7-current.noarch.rpm
sudo yum install -y tmux

# View version
tmux -V
  1. Edit the configuration file ~/.tmux.conf
vim ~/.tmux.conf
#Input: set paste to enter paste mode
:set paste
# Enter i to enter insert (paste) mode
i

Copy the following content to tmux.conf:

The default command prefix of tmux is control + B. We can unbind it according to unbind-key C-b and bind-key C-a send-prefix to rebind the keys we are used to.

set-option -g prefix C-b
# unbind-key C-b
bind-key C-b send-prefix
bind-key s setw synchronize-panes

# Use Alt-arrow keys to switch panes
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

# Shift arrow to switch windows
bind -n S-Left previous-window
bind -n S-Right next-window

# Mouse mode
set-option -g mouse on

# # Set easier window split keys
bind-key v split-window -h
bind-key h split-window -v
#
# # Easy config reload
bind-key r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"

set -g default-terminal "screen-256colour"
set -g status-style fg=white # Foreground color
set -g status-style bg=black # Background color
setw -g window-status-style fg=cyan # Active window color is blue-green
setw -g window-status-current-style fg=white
setw -g window-status-current-style bg=red
set -g message-style fg=white
set -g message-style bg=black
set -g message-style bright
set -g pane-border-style fg=white
set -g pane-border-style bg=green

set -g status-left "#[fg=green]#S#[fg=yellow]-#I#[fg=cyan]-#P"
set -g status-utf8 on
set-g status-interval 60s
setw -g monitor-activity on
set -g visual-activity on
  1. Configure environment variables
# Make the tmux.conf file effective
tmux source-file ~/.tmux.conf
# Configure zsh environment variables
vim ~/.zshrc
#Add the following configuration at the bottom of the .zshrc file
export TERM=xterm

6.3 Set up your own workspace

  1. Create a workspace named workspace
tmux new-session -s workspace-demo -d
# If you want to delete a namespace, enter the space through the following command
# tmux attach-session -t workspace-demo
# After entering the space, enter exit to delete, or directly execute kill-session
# tmux kill-session -t workspace-demo
  1. tmux ls to check whether the creation is successful
# View all namespaces
tmux ls

  1. Enter the workspace and create your own window
# Enter the workspace-demo workspace
tmux attach-session -t workspace-demo
# Create a new window-demo-1 window
tmux new-window -n window-demo-1

  1. Under the window-demo-1 window, create two panes (two ssh sessions)
# Log in to the machine 145
ssh [email protected]

# Create a new ssh session (command shortcut key prefix I use C-B as an example here)
control + B, after pressing and releasing, press H again (create a new horizontal window)
# I connect to my 146 machine in a new window
ssh [email protected]

Of course we can continue to open more shell windows

Attention:
① How to ensure that the current session state is retained when logging in next time?

# Pay attention to press control + B first, release it, and then press D (not all three buttons together)
control + B, then press D (detach)


②Configure alias, so that when you log in to the terminal next time, you can directly enter our window

For example, we want to set alias to dev on the machine 192.168.16.145. Next time we log in to the machine 192.168.145 (tmux environment is required), we can enter dev directly. workspace-demo workspace in tmux

# Modify ~/.bashrc or ~/.bash_profile file
vim ~/.bashrc
# Add the following commands in the alias section (without spaces between =)
alias dev='tmux attach-session -t workspace-demo'

# Input: wq save and exit
:wq
# Refresh the configuration file
source ~/.bashrc

test:

After ssh logging into the machine 145, enter dev

6.4 Common commands

- use control + b then press [ select text with mouse to copy
- Use control + b then press ] to paste
- `Control + B`, then press `Spacebar`: Change pane layout.
- `Control + B`, then `Z`: Toggle full screen mode for the current pane.
- `Control + B`, then press `&`: Close the current window.
- `Control + B` then `L`: Switch between windows, return to the previous window.
- `Control + B`, then press `$`: Rename the current window.
- `Control + B`, then press `?`: Show tmux shortcut help.
- `Control + B`, then `C`: Create a new window.
- `Control + B`, then `N`: Switch to the next window.
- `Control + B`, then press `P`: Switch to the previous window.
`Control + B`, then press `0-9`: switch to the window with the specified number.

- `Control + B`, then press `%` or V (vertical): Create a new vertically split pane in the current window.
- `Control + B`, then press `"` or H: Create a new horizontally split pane in the current window.
- `Control + B`, then press `arrow keys`: Navigate between panes. (option + arrow keys can also achieve the effect)
- `Control + B`, then press `x`: Close the current pane.