Skip to content

macOS 常用笔记

偏爱 Linux 的我居然还是投向了 macOS

MacBook Pro 双显卡切换命令

bash
sudo pmset -a GPUSwitch 0

## 0 - 强制使用核显
## 1 - 强制使用独显(相当于在偏好设置-效能 里去掉自动切换显卡这个选项)
## 2 - 自动切换显卡
sudo pmset -a GPUSwitch 0

## 0 - 强制使用核显
## 1 - 强制使用独显(相当于在偏好设置-效能 里去掉自动切换显卡这个选项)
## 2 - 自动切换显卡

设置允许所有来源安装

bash
sudo spctl --master-disable
sudo spctl --master-disable

如已经开启任何来源,但依旧打不开,提示已损坏(macOS Catalina 10.15 以上会遇到)

bash
sudo xattr -d com.apple.quarantine /Applications/xxxx.app
sudo xattr -d com.apple.quarantine /Applications/xxxx.app

安装 command line tools

bash
xcode-select --install
xcode-select --install

另外,如果想知道自己的机器是否已经安装了「CommandLine Tools」,检查一下/Library/Developer/CommandLineTools文件夹是否存在。

安装 homebrew 包管理工具

官网的安装方式如下:

bash
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

但是在国内下载过慢,解决方案如下:

先下载安装脚本

bash
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install

编辑安装脚本,把这两句用#注释掉

bash
BREW_REPO = "https://github.com/Homebrew/brew".freeze
CORE_TAP_REPO = "https://github.com/Homebrew/homebrew-core".freeze
BREW_REPO = "https://github.com/Homebrew/brew".freeze
CORE_TAP_REPO = "https://github.com/Homebrew/homebrew-core".freeze

修改为这两句

bash
BREW_REPO = "git://mirrors.ustc.edu.cn/brew.git".freeze
CORE_TAP_REPO = "git://mirrors.ustc.edu.cn/homebrew-core.git".freeze
BREW_REPO = "git://mirrors.ustc.edu.cn/brew.git".freeze
CORE_TAP_REPO = "git://mirrors.ustc.edu.cn/homebrew-core.git".freeze

运行修改了的 brew_install

bash
/usr/bin/ruby brew_install
/usr/bin/ruby brew_install

执行之后你会看到如下界面

UTOOLS1559294005064.png

出现这个因为源不通,代码无法下载到本地,解决方法是更换成国内镜像源,执行如下命令,更换到中科院的镜像:

bash
git clone git://mirrors.ustc.edu.cn/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1
git clone git://mirrors.ustc.edu.cn/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1

然后把 Homebrew-core 的镜像地址也设置为中科院的国内镜像

bash
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

执行更新 brew 命令:

bash
brew update
brew update

接着执行 brew 检测命令:

bash
brew doctor
brew doctor

homebrew 镜像[清华]

Homebrew 镜像使用帮助

homebrew-bottles 镜像[清华]

Homebrew-bottles 镜像使用帮助

Homebrew 基本用法:

操作命令
更新 Homebrewbrew update
更新所有安装过的软件包brew upgrade
更新指定的软件包brew upgrade wget
查找软件包brew search wget
安装软件包brew install wget
卸载软件包brew remove wget
列出安装的软件包brew list
查看软件包信息brew info wget
查看软件包依赖关系brew deps wget
列出可以更新的软件包brew outdated

对当前命令行设置网络代理

Termianl 中输入

bash
export http_proxy="http://127.0.0.1:1087"
export https_proxy="http://127.0.0.1:1087"
export http_proxy="http://127.0.0.1:1087"
export https_proxy="http://127.0.0.1:1087"

注意一点在 Terminal 的生命周期中这个全局代理是有用的,一旦关闭了当前的 Terminal 或者重新开一个需要重新运行上面的命令。

如果需要频繁切换,可以在 .bashrc 中定义函数

bash
function proxy_off(){
        unset http_proxy
        unset https_proxy
        unset ftp_proxy
        unset rsync_proxy
        echo -e "已关闭代理"
}

function proxy_on() {
        export no_proxy="localhost,127.0.0.1"
        export http_proxy="http://127.0.0.1:1087" ## 注意,根据自己的配置设置有可能会是1080或1086
        export https_proxy=$http_proxy
        export ftp_proxy=$http_proxy
        export rsync_proxy=$http_proxy
        echo -e "已开启代理"
}
function proxy_off(){
        unset http_proxy
        unset https_proxy
        unset ftp_proxy
        unset rsync_proxy
        echo -e "已关闭代理"
}

function proxy_on() {
        export no_proxy="localhost,127.0.0.1"
        export http_proxy="http://127.0.0.1:1087" ## 注意,根据自己的配置设置有可能会是1080或1086
        export https_proxy=$http_proxy
        export ftp_proxy=$http_proxy
        export rsync_proxy=$http_proxy
        echo -e "已开启代理"
}

设置 git 代理

打开 ~/.gitconfig 添加

ini
[http]
	proxy = socks5://127.0.0.1:1080
[https]
	proxy = socks5://127.0.0.1:1080
[http]
	proxy = socks5://127.0.0.1:1080
[https]
	proxy = socks5://127.0.0.1:1080

如何显示隐藏文件(快捷键)

老版本的系统需要通过命令开启,新版本支持快捷键:

Command+Shift+. 可以显示隐藏文件、文件夹,再按一次,恢复隐藏; finder 下使用 Command+Shift+G 可以前往任何文件夹,包括隐藏文件夹

修改主机名和共享名

bash
sudo scutil --set HostName wolfx-mbp
sudo scutil --set ComputerName wolfx-mbp
sudo scutil --set HostName wolfx-mbp
sudo scutil --set ComputerName wolfx-mbp

修改 NPM 全局安装时的路径

bash
## 创建新目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
## 修改环境变量
export PATH=~/.npm-global/bin:$PATH
## 创建新目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
## 修改环境变量
export PATH=~/.npm-global/bin:$PATH

Android SDK 环境变量配置

建议安装 oh-my-zsh,然后修改用户目录下的 .zshrc 即可:

bash
export ANDROID_HOME=~/Library/Android/sdk
export PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH
export ANDROID_HOME=~/Library/Android/sdk
export PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH

Android 一键启动 AVD

bash
cd /Users/wolfx/Library/Android/sdk/emulator/
emulator -list-avds
cd /Users/wolfx/Library/Android/sdk/emulator/
emulator -list-avds

正常情况下就会显示刚刚创建好的 AVD 名称(例如 Pixel_2_API_27)

通过如下命令启动 AVD

bash
emulator -netdelay none -netspeed full -avd Pixel_2_API_27
emulator -netdelay none -netspeed full -avd Pixel_2_API_27

iTerm2 配置一键 SSH

编写脚本 ~/Work/SSH/test.sh

bash
#!/usr/bin/expect -f
set user root
set host 192.168.1.101
set password 123456
set timeout 30

spawn ssh $user@$host
expect {
    "password:"
    {send "$password\r"}
}
interact
#!/usr/bin/expect -f
set user root
set host 192.168.1.101
set password 123456
set timeout 30

spawn ssh $user@$host
expect {
    "password:"
    {send "$password\r"}
}
interact

在 iTerm2 的 Profile 中增加方案,设置登录脚本为 expect ~/Work/SSH/test.sh

spotlight 显示不出内容

重新开启索引功能

bash
sudo mdutil -a -i off
sudo mdutil -a -i on
sudo mdutil -a -i off
sudo mdutil -a -i on

加速时间机器的速度

bash
## 解除限速
sudo sysctl debug.lowpri_throttle_enabled=0
## 开启限速
sudo sysctl debug.lowpri_throttle_enabled=1
## 开始备份
tmutil startbackup
## 解除限速
sudo sysctl debug.lowpri_throttle_enabled=0
## 开启限速
sudo sysctl debug.lowpri_throttle_enabled=1
## 开始备份
tmutil startbackup

最后编辑时间:

Version 4.0 (framework-1.0.0-rc.20)