阅读 6591

Mac 终端效率神技

Mac 终端效率神技

增强各种预览的插件

  • 预览查看图片分辨率&大小
  • 代码语法高亮
  • 快速预览zip压缩包内容
  • 快速预览markdown格式内容
brew cask install qlcolorcode betterzipql qlimagesize qlmarkdown
复制代码

iTerm2

具体的配置网上一大堆。贴一个本人亲身操刀操作过的教程

程序员经常与终端操作打交道,所以很多命令便是做成了命令行模式,在自带的 Terminal 命令都保存在 .bash_profile 文件中,使用了 iterm2,命令都保存在 .zshrc 中。

所以我们将很多命令保存且编辑。这里也是分享出我个人常用的配置。不断更新,喜欢的同学可以拿去直接使用

cd ~
vim .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="/Users/liubinpeng/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="agnoster"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

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

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

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

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

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

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# 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.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# 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?
# Standard 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.

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

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

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

source /Users/liubinpeng/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
plugins=(
    git
    zsh-syntax-highlighting
    zsh-autosuggestions
)

# 输入自己常用的命令
# finder 相关指令
alias co='code ./'
alias fo='open ./'

# pod 和 xcode 工程相关指令
alias o='open *.xcodeproj'
alias po='open *.xcworkspace'
alias pru='pod repo update'
alias pi='pod install'
alias pu='pod update'
alias piu='pod install --repo-update'
alias repoanalysis='specbackwarddependency /Users/liubinpeng/.cocoapods/repos/51xianqu-xq_specs'
alias plint='pod lib lint --sources=git@git.caimi-inc.com:client/i-New-All-Specs.git,git@git.caimi-inc.com:client/CocoaPods-Specs.git --allow-warnings --verbose --use-libraries'
alias errorShow=' >1.log 2>&1'
# git 相关指令
alias gck='git checkout'
alias gm='git merge'
alias gb='git branch'
alias gbr='git branch -a'
alias gs='git status'
alias gc='git clone'
alias gl='git log'
alias ga='git add .'
alias gpull='git pull'
alias gpush='git push'
alias gcm='git commit -m'
alias glocalbranchPush='git push --set-upstream origin '
alias glg="git log --graph --pretty=format:'%Cred%h%Crest -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
# npm 相关指令
alias ns='npm start'
alias ni='npm install'
alias nb='npm run build'
alias nig='npm install -g '

# Vue 相关命令
alias vc='vue-init webpack' # (vue-init webpack test1)用法 vc test1

# React 
alias rc='create-react-app' #(create-react-app todolist)用法 rc todolist

# React Native 命令
alias rnc='react-native init' #(react-native init todolist)用法 rnc todolist


# 终端打开应用程序
## 浏览器打开
alias OpenWithSafari='open -a "/Applications/Safari.app" '
alias OpenWithChrome='open -a "/Applications/Google Chrome.app" '
## 用 Typora 打开 markdown 文件预览写作效果。
alias OpenMDPreview='open -a "/Applications/Typora.app" '
## 用 DB Browser for SQLite 打开 db 文件
alias OpenDB='open -a "/Applications/DB Browser for SQLite.app" '
## 用 SourceTree 打开工程
alias openSourceTree='open -a "/Applications/Sourcetree.app/" '
# Flutter 环境变量

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=/Users/liubinpeng/flutter/bin:$PATH


# Android SDK 路径

export ANDROID_HOME=~/Library/Android/sdk
export PATH=${PATH}:${ANDROID_HOME}/emulator
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools


# iOS 模拟器开启
alias iOSSimulator='open -a Simulator'

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion


# Node Version Manager
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"

export N_PREFIX=/usr/local/bin/node #根据你的安装路径而定
export PATH=$N_PREFIX/bin:$PATH
export PATH=/Users/liubinpeng/Desktop/Github/GitWorkflow/bin:$PATH


# chrome 源码探究
# export PATH=/Users/liubinpeng/Desktop/Tech-Research/iOS/depot_tools:$PATH


# 指定 pyhton 版本

alias python="/usr/local/Cellar/python/3.7.4/bin/python3"


# 禁止终端利用 homebrew 安装插件时候的自动更新

alias disableHomebrewUpdate="export HOMEBREW_NO_AUTO_UPDATE=true"


# 终端翻墙相关的设置(开关开启后稍微有点延迟)

function proxy_off(){
    unset ALL_PROXY
    echo -e "已关闭代理"
}

function proxy_on(){
    export ALL_PROXY=socks5://127.0.0.1:1081
    echo -e "已开启代理"
}


cd ~
source .zshrc
复制代码

验证:在你的 git 项目所在的目录的终端下输入 glg

Git日志

为你的终端添加常用快捷键

我们经常在终端做着一些纯指令的事情,天天敲、月月敲这个时间的很浪费的,一天节约5分钟,一年节约365*5/60 = 30H。一算吓一跳。我们每年在一些终端的指令上浪费了这么多时间。今天记录下如何给自己的 Mac 终端添加快捷键。

如果是 zsh 的话,可以编辑 .zshrc 文件里面的内容。自带的终端则编辑 bash_profile 脚本具体看上一条。

输出文件目录结构

brew install tree
复制代码

用法:

  1. 我们可以在目录遍历时使用 -L 参数指定遍历层级

    tree -L 2
    复制代码
  2. 如果你想把一个目录的结构树导出到文件 Readme.md ,可以这样操作

    tree -L 2 >README.md //然后我们看下当前目录下的 README.md 文件
    复制代码
  3. 只显示文件夹

    tree -d 
    复制代码
  4. 显示项目的层级,n表示层级数。例:显示项目三层结构,tree -l 3

    tree -L n 
    复制代码
  5. tree -I pattern 用于过滤不想要显示的文件或者文件夹。比如要过滤项目中的node_modules文件夹

    tree -I “node_modules”
    复制代码

浏览器相关

  1. 搜索

在指定的站点下搜索 inurl: jobbole.com intitle:Hybrid

百度云盘破解

  1. 会员体验一般为60秒,通过本代码可以一直以会员的速度下载。
git clone https://github.com/CodeTips/BaiduNetdiskPlugin-macOS.git && ./BaiduNetdiskPlugin-macOS/Other/Install.sh
复制代码
  1. 百度网盘全速下载
  • 先将你需要下载的地址复制进浏览器
  • 然后在域名 baidu 后面拼接 wp
  • 回车。访问页面,选择下载地址1即可全速下载。
// 之前
https://pan.baidu.com/s/1ubcQH34m69hIjYu3CD2S2g
// 之后
https://pan.baiduwp.com/s/1ubcQH34m69hIjYu3CD2S2g
复制代码

「安全与隐私」中系统不显示「任何来源」

在终端执行下面的命令

sudo spctl --master-disable
复制代码

系统错误信息的集中展示

pod spec lint *** 2>&1|tee 1.log
复制代码

经常在终端做操作,有个情况就是在 iOS 的组件库维护的时候去检测合法性。你会发现满屏幕都是信息,甚至好几页,但是事实上错了问题后我们去翻页的时候发现很不方便定位问题,所以想到的就是将该过程产生的任何输出,集中打印到一个地方去查看。代码如上。

几个概念:

  • 0 stdin,1 stdout,2 stderr
  • |:管道。管道的作用是提供一个通道,将上一个程序的标准输出重定向到下一个程序作为下一个程序的标准输入。
  • tee:从标准输入中读取,并将内容写到标准输出以及文件中。

终端查找文件

  1. 终端查找以‘.log’结尾的文件
find . -name '*.log'
复制代码
  1. 安装 ack 包.
brew install ack
复制代码

使用起来很简单,比如 ack + 你要查找的关键词,它可以将查到的结果展示在下面,有完整的文件路径.

效果图

终端每次执行 brew install 都会更新,非常耗时,如何禁止更新。

export HOMEBREW_NO_AUTO_UPDATE=true
复制代码

Mac 翻墙环境和终端翻墙环境

强烈安利一个我用过最快速、最便宜也就是性价比最高的科学上网工具。链接 个人使用的也就是一年120元不到,一个月 60G 流量足够了,打开一些网站秒开。

另外很多开发都需要终端下载一些资源,但是终端走的通道和浏览器不一样,所以浏览器可以翻墙,终端还是不可以,所以可以在 .zshrc 或者 .bash_profile 下加下面的脚本

function proxy_off(){
    unset ALL_PROXY
    echo -e "已关闭代理"
}

function proxy_on(){
    export ALL_PROXY=socks5://127.0.0.1:1081
    echo -e "已开启代理"
}
复制代码

使用 proxy_on 开启终端翻墙模式、proxy_off 关闭终端翻墙模式。

持续更新中...

关注下面的标签,发现更多相似文章
评论