More elegant use of the command line

Time:2020-10-17

zsh

If a worker wants to do a good job, he must sharpen his tools first. By arming his command-line tools, he can use the command line more elegantly, which can make the work more efficient and interesting. This article will introduce the following aspects of command line skills and tools

CLI one click call in and out

Iterm2 is a completely free terminal tool for MacOS, which can open the hotkey window to achieve the effect of one click call in and out

The results are as follows:

iterm2

The detailed settings are as follows:

1. First, make the following settings

preferences > Keys > HotKey > Create a Dedicated Hotkey Window...

iterm2_set01

2. Next, set the hotkey and selectAnimate showing and hidingandFloating windowThese two options

iterm2_set02

zsh

Bash is the default shell for Linux and OS X systems.oh my zshIt’s an enhanced shell

If it’s Mac OS, you should bring it by defaultzshYes, it can be confirmed before installation

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

You can view the current environment’sshell

echo $SHELL

You can use the following commandshellSwitch. Especially, switchshellAfter that, restart the CLI to take effect

Chsh - S / bin / bash ා switch Bash
Chsh - S / bin / Zsh ා switch Zsh

Next, start the installationoh-my-zshPlease pay special attention to that you cannot use the address of the official website for installation, otherwise you will be prompted

Failed to connect to raw.github.com port 443: Connection refused

Instead, install it at the following address

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

After the installation is completed, you will be prompted whether tozshSet as default shell, selectY

Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] y
Changing the shell...
Changing shell for root.
Shell successfully changed to '/bin/zsh'.

         __                                     __
  ____  / /_     ____ ___  __  __   ____  _____/ /_
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / /
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/
                        /____/                       ....is now installed!


Please look over the ~/.zshrc file to select plugins, themes, and options.

p.s. Follow us on https://twitter.com/ohmyzsh

p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh

Here is a brief introductionoh-my-zshThe advantages of

1. The topic prompt changes from user name and host name to the name of the current directory

2. Press tab to complete, you can not only complete the command, but also complete options, parameters, files, etc

3. The jump path can omit the CD command and match the first character of the path

c/k/k/t/c

After pressing the tab key, the following path will be automatically completed

code/ktsg/ktsg_new/trunk/config

4. Enter D directly under the current directory to display the list of historical access directories (the last 20), and add a numerical index on the left

$ d
0   ~/Desktop/md/blog
1   ~/Desktop/md
2   ~/Desktop
3   ~

Alias configuration

Using git alias configuration can make git experience easier

It can be done throughgit configCommand for commandgit branchSet an alias

$ git config --global alias.b branch

This means that when you want to inputgit branchOnly input is requiredgit bJust fine

The simpler way is to edit directly~/.gitconfigFile, can achieve the same effect

[alias]
b = branch

But if you just want to inputgbYou want to achieve itgit branchFor the same effect, you need to use the alias function of Linux

actually,zshThe GIT plug-in has been set by default. The file path is as follows

.oh-my-zsh/plugins/git/git.plugin.zsh

Here are some common configurations

alias g='git'
alias ga='git add'
alias gb='git branch'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gcam='git commit -a -m'
alias gcb='git checkout -b'
alias gcmsg='git commit -m'
alias gco='git checkout'
alias gd='git diff'
alias gl='git pull'
alias glog='git log --oneline --decorate --graph'
alias gloga='git log --oneline --decorate --graph --all'
alias gp='git push'
alias gsb='git status -sb'
alias gst='git status'

usegstThe effect is as follows

$ gst
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

    new file:   html_backup.md
    new file:   t.html

homebrew

Brew is also calledhomebrew, is a software package management tool on MAC, which can easily install or uninstall software in MAC

Here ishomebrewCommon commands for

Brew install Git
Brew uninstall WGet
Brew list ා lists the installed software

Plug in Recommendation

Here are some plug-in recommendations. After the plug-in is installed, it needs to be opened~/.zshrc, foundplugins=Then write the name of the plug-in you need. As long as you modify this file, use thesource ~/.zshrcTo update the configuration

Quick jump

autojumpThe plug-in realizes the quick jump between directories. Which directory do you want to go to directlyJ + directory nameNo more frequentcd

useautojumpCommand, or use a short commandjTo jump to the specified directory. Note that only opened directory plug-ins will record. Therefore, the longer it is used, the more intelligent the plug-in will be

j directoryName

zsh_02

The installation is as follows:

brew install autojump

And then in the.zshrcAdd the following statement to the file

[[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh

Command prompt

usezsh-autosuggestionsThe plug-in can prompt automatic completion (gray part) when entering command, and then press the right key of keyboard direction to complete

zsh_01

The installation is as follows:

cd ~/.oh-my-zsh/custom/plugins/
sudo git clone https://github.com/zsh-users/zsh-autosuggestions

Syntax highlighting

usezsh-syntax-highlightingPlug in, the daily command will be highlighted, and the command error will be displayed in red

zsh_03

The installation is as follows:

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

Order correction

usethefuckPlug in, can be used for command correction, inputfuckAfter that, you can correct the previous command

the_fuck

The installation is as follows:

brew install thefuck

And then in the.zshrcAdd the following statement to the file

eval $(thefuck --alias)

Search keywords

useweb-searchPlug ins can search using search engines, such as usinggoogleorstackoverflow

$Google oh my Zsh
$stack overflow oh my Zsh

zsh_web_search

The plug-in does not need to be installed directly in thezshrcIn the filepluginsAdd in

Open remote warehouse

usegit-openPlug in, inputgit openYou can open a warehouse in the browsergithubpage

git_open

The installation is as follows:

cd ~/.oh-my-zsh/custom/plugins/
sudo git clone https://github.com/paulirish/git-open.git $ZSH_CUSTOM/plugins/git-open

Quick search

fzfPlug in is a general command line fuzzy search tool, depending on Fuzzy keywords, can quickly locate files

adoptcode $(fzf)Command to search for files

fzf

The installation is as follows:

brew install fzf

translate

translate shellIs a default Google translation to translate the command line translator

usetransCommand can be translated, plus-spOptions (short for speak) can also be pronounced

trans

The installation is as follows:

brew install translate-shell

Plug in configuration

After the above plug-ins are installed,.zshrcThe configuration of the plug-in section of the file is as follows

plugins=(
    git
    web-search
    autojump
    zsh-syntax-highlighting
    zsh-autosuggestions
    git-open
    fzf
)

# autojump
[[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh

# thefuck
eval $(thefuck --alias)