Content Table

Mac Tips

Mac 使用中的一些常用操作和命令。

隐藏 Dock Icon

隐藏程序在 Dock 上的 Icon,以隐藏 QQ 的 图标为例:

1
2
defaults write /Applications/QQ.app/Contents/Info.plist LSUIElement 1
sudo codesign -f -s - /Applications/QQ.app

Vim 基础

下面列出一些 Vi 常用的命令和设置:

  • 搜索高亮: :set hlsearch
  • 显示行号: :set number
  • 可使用鼠标点击: :set mouse=a
  • 搜索: /+keyword
  • 大小写不敏感搜索:
    • /+keyword\c
    • :set ic,然后 /+keyword
  • 删除当前行: dd
  • 删除光标后的字符: x
  • 复制当前行: yy
  • 粘贴复制的行: p
  • 回到第一行: gg
  • 跳到最后行: GG
  • 回到行首: 0
  • 跳到行尾: $
  • 光标处插入: i
  • 光标后插入: a
  • 插入新一行: o
  • 撤销操作: u
  • 光标移到屏幕顶部: H
  • 光标移到屏幕中部: M
  • 光标移到屏幕底部: L
  • 单词出现的次数: :%s/pattern//gn
  • 上一页: ctrl+b
  • 下一页: ctrl+f

Vi 内全局替换

1
2
3
4
5
6
# 使用 str2 替换 str1
:%s/str1/str2/g

# 多个匹配,例如把所有的 '\t' 替换为 ','
# 一个或多个用 '\+' 而不是 '+'
:%s/\t\+/,/g

如果要长期有效,可以在 ~/.vimrc 里加上对应的设置,例如

1
2
3
4
5
set hlsearch
set number
colorscheme torte
set mouse=a
syntax on

Bat

Bat 是语法高亮的 cat (Clone of cat with syntax highlighting and Git integration), 主页为 https://github.com/sharkdp/bat,可以使用 brew 安装: brew install bat,可以在 .bash_profile 里给其设置别名为 cat 用于替代 cat: alias cat="bat":

查看端口占用

使用 lsof -i:port 查看端口是否被某个进程占用了,例如 lsof -i:8080

查看使用端口的 PID

lsof -n -P | grep :80

使用文件名查找

find dir -name filename,例如 find . -name mvc.xml

使用文件大小查找

find . -size +200M

使用文件修改时间查找

find . -ctime -10s

查找后使用 grep

find . -name "*.java" | xargs grep -n --color "topic"

查找后打开文件

1
find . -name mac-tips.md | xargs open

统计多个文件的行数

1
find . -name "*.java" | xargs wc -l

awk 输出列

1
2
3
4
5
6
7
8
// 输出第一列
echo 1 2 3 4 5 | awk '{ print $1 }'

// 输出第三列到最后一列
echo 1 2 3 4 5 | awk '{ for (i=3; i<=NF; i++) print $i }'

// 列求和
cat m.txt | awk '{a+=$1} END {print a}'

sort & uniq

1
2
3
4
5
// 按行排序,并去掉重复行
sort a.txt | uniq

// 按行排序,并去掉重复行,统计最后的行数
sort a.txt | uniq -c

zip

1
2
// 把文件夹 H5 和文件 x.html 压缩成 result.zip
zip -r result.zip H5 x.html

unzip

1
2
3
// -d 指定输出目录
// 如果没有 -d + targetDir,则解压到当前目录
unzip signup.zip [-d targetDir]

tar

1
2
3
4
5
6
7
8
9
10
11
// 压缩为 archive.tar
tar -cf archive.tar file1 file2 file3

// 解压 archive.tar
tar -xf archive.tar

# Create compressed gzip archive.
tar -czf file.tar.gz file1 file2

# Extract .gz archive.
tar -xzf file.tar.gz

查看不同状态的链接数量

1
2
3
4
5
6
netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t",state[key]}'

输出:
SYN_SENT 1
CLOSE_WAIT 4
ESTABLISHED 7

转换文件编码

1
2
3
4
5
// 单个文件转码
iconv -f GB18030 -t UTF8 201607-data.txt > 201607.txt

// 查找文件并转码
find *.txt -exec sh -c "iconv -f GB18030 -t UTF8 {} > {}.txt" \;

显示目录树

  1. 使用 brew 安装 tree: brew install tree
  2. 执行命令 tree 就能显示当前目录的树形结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    .
    ├── LICENSE.txt
    ├── build.gradle
    └── src
    ├── main
    │   ├── java
    │   ├── resources
    │   └── webapp
    │   └── WEB-INF
    │   └── web.xml
    └── test
    ├── java
    └── resources

    9 directories, 3 files

计算文件的 MD5

1
md5 fileName

计算字符串 MD5

1
2
3
4
5
6
7
8
# 下面 3 种方式都可以
md5 -s Welcome
md5 -s 'Welcome'
md5 -s "Welcome"

echo -n Welcome | md5
echo -n 'Welcome' | md5
echo -n "Welcome" | md5

输出文件的十六进制

1
hexdump -C Main.java | head -n 20

重新加载声卡驱动

合上盖子后再打开有时候可能会没声,注销再登陆后就有声音了,或者用命令重新加载声卡驱动也可以解决

1
2
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext

递归创建目录

1
2
# p 是 plus 之意
mkdir -p a/b/c

Go2Shell

Opens a terminal window to the current directory in Finder. 支持 iTerm2 和 Terminal.

可以在系统快捷键中设置 Finder 的 Finder->Services->New iTerm2 Tab Here 的快捷键为 ALT+CMD+T 按下快捷键打开.

Chrome 清除缓存插件

Chrome 应用商店里搜索 clear Cache,找到 clear Cache, clean cache, 清理缓存 并安装

查看文件夹大小

1
du -sh

cp 复制文件夹

1
2
3
4
5
# 把文件夹 srcDir 复制到 destDir, srcDir 为 destDir 的子文件夹
cp -r srcDir destDir

# 把文件夹 srcDir 下的文件及子文件夹复制到 destDir
cp -r srcDir/ destDir

scp 上传和下载文件

scp src dst:

  • 上传文件: scp /Users/Biao/Desktop/a.zip root@192.168.82.130:/root
  • 下载文件: scp root@192.168.82.130:/root/a.zip /Users/Biao/Desktop
  • 指定端口使用 -P port: scp -P 29443 root@192.168.82.130:/root/a.zip .

mv 移动目录

1
2
# 移动目录和重命名都是使用命令 mv,区别就是后面的 /
mv srcDir destDir/

Quick Look plugins

可以使用 brew cask 安装 Quick Look 插件,具体请参考 https://github.com/sindresorhus/quick-look-plugins

1
brew cask install qlcolorcode qlstephen qlmarkdown quicklook-json qlimagesize webpquicklook suspicious-package quicklookase qlvideo

列出 Node 安装的软件

1
ls $(npm root -g)

Node 使用淘宝镜像

使用淘宝 NPM 镜像,安装时使用 cnpm 代替 npm:

1
npm install -g cnpm --registry=https://registry.npm.taobao.org

删除所有 Vmware 的进程

1
kill -9 $(ps aux | grep -i vmware | grep -v grep | awk '{print $2}')

去掉截图阴影

1
defaults write com.apple.screencapture disable-shadow -bool true ; killall SystemUIServer

修改截图名称

1
2
3
defaults write com.apple.screencapture name "shot"
defaults write com.apple.screencapture "include-date" 0
killall SystemUIServer

更新文件图标

1
sudo rm -rfv /Library/Caches/com.apple.iconservices.store; sudo find /private/var/folders/ \( -name com.apple.dock.iconcache -or -name com.apple.iconservices \) -exec rm -rfv {} \; ; sleep 3;sudo touch /Applications/* ; killall Dock; killall Finder

隐藏显示文件

1
2
alias hide="chflags hidden *"
alias show="chflags nohidden *"

显示后台进程

使用 jobs -l 查看当前终端(标签页)启动的后台进程。关闭终端后,在另一个终端 jobs 无法看到后台跑得程序了,此时可利用 ps 命令。

终端不显示上次登录时间

If you don’t want to see that login message or MOTD again, you can get rid of that ‘Last login’ message at the top of a new terminal by entering the following command to create a ‘hushlogin’ file:

1
touch /Users/$(whoami)/.hushlogin

终端不显示邮件提示

终端打开标签页有时会提示有新邮件,一般都是由于 crontab 任务导致的,如果不想看到,删除文件 /var/mail/$(whoami) 即可。

Zsh 配置 .zshrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
export GRADLE_HOME="/usr/local/gradle"
export MYSQL_HOME="/usr/local/Cellar/mysql/5.7.21"
export NGINX_HOME="/usr/local/Cellar/openresty/1.11.2.5/nginx/sbin"
export ES_HOME="/usr/local/elasticsearch"
export FLUTTER_HOME="/Users/Biao/Downloads/flutter"
export PATH="$PATH:$GRADLE_HOME/bin:$MYSQL_HOME/bin:$NGINX_HOME:$ES_HOME/bin:$FLUTTER_HOME/bin"
export SERVER_ID="1"

alias ls="ls -G"
alias ll="ls -Glh"
alias lla="ls -Glha"
alias tcpCount="netstat -n | awk '/^tcp/ {++state[\$NF]} END {for(key in state) print key,\"\t\",state[key]}'"
# alias rm="rmtrash"

# Hexo
alias hs="hexo server -g"
alias hd="hexo clean; hexo generate; hexo deploy"

alias cddesk="cd ~/Desktop"
alias cdblog="cd /Users/Biao/Documents/workspace/Blog"
alias cdjava="cd /Users/Biao/Documents/workspace/Java"
alias cdnote="cd /Users/Biao/Documents/workspace/Notable"
alias cdtemp="cd /Users/Biao/Temp"
alias cdebag="cd /Users/Biao/Documents/workspace/new-ebag-web"
alias cdbrew="cd /usr/local/Cellar"
alias cdworkspace="cd /Users/Biao/Documents/workspace"

alias ngrokStart="/Applications/ngrok clientid 497cb09d080c5594"
alias hide="chflags hidden *"
alias show="chflags nohidden *"
alias cal="cal 2019"
alias cat="bat"

alias ebagApps="mysqld & mongod --auth --config /usr/local/etc/mongod.conf & redis-server & elasticsearch -d & activemq start"
alias ebagRun="gradle clean appStartDebug -Denv=mac"
alias ebagImRun="gradle clean run -Denv=macOfficeDev"
alias ebagTest1="gradle clean deploy -Denv=test1"
alias ebagTest2="gradle clean deploy -Denv=test2"

alias code="/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron"
alias ohtml="code ~/Temp/test.html"
alias otemp="code ~/Temp/temp.txt"

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_77`
# export JAVA_HOME=`/usr/libexec/java_home -v 9`

export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

# 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/Biao/.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="powerlevel10k/powerlevel10k"
# ZSH_THEME="powerlevel9k/powerlevel9k"
# ZSH_THEME="pi"

# 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 change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# 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.
plugins=(
# git
zsh-autosuggestions
zsh-syntax-highlighting
)

ZSH_DISABLE_COMPFIX=true
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"

POWERLEVEL9K_MODE='nerdfont-complete'
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(os_icon ssh dir status vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=()
POWERLEVEL9K_HOME_SUB_ICON='\uF015'
POWERLEVEL9K_FOLDER_ICON='\uF109'
POWERLEVEL9K_STATUS_CROSS=true
POWERLEVEL9K_PROMPT_ADD_NEWLINE=true
# POWERLEVEL9K_DIR_PATH_SEPARATOR="%F{black} "$'\uE0B1'" %F{black}"