0%

Mac

阅读更多

1 快捷键

1.1 清屏

command + k

1.2 关闭显示器

  • ⌃ + ⇧ + 电源键(貌似只能锁屏,并未关闭显示器)
  • 系统偏好设置->调度中心->触发角(亲测可用)

1.3 锁定屏幕

⌃ + ⌘ + Q

1.4 显示当前应用的多个窗口

^ + ↓

1.5 显示正在执行的任务

^ + ↑

1.6 启动台

F10

1.7 露出桌面

F11

1.8 显示隐藏文件夹

⌘ + ⇧ + .

也有如下非快捷键的方法

1
2
3
4
5
6
7
//显示
defaults write com.apple.finder AppleShowAllFiles -boolean true
killall Finder//重启Finder

//隐藏
defaults write com.apple.finder AppleShowAllFiles -boolean false
killall Finder//重启Finder

1.9 微粒度音量调节

⌥ + ⇧ + 音量调节按键

1.10 应用图标抖动

在launchpad界面中,⌃ + ⌥ + ⌘ + B

1.11 emoji

⌃ + ⌘ + space

1.12 Page Up/Down

Home键:Fn+←
End键:Fn+→
Page UP:Fn+↑
Page DOWN:Fn+↓
向前Delete:Fn+delete

1.13 缩放窗口

Maximize window shortcut

窗口缩放功能的配置路径是:System Preferences->Dock->Double-click a window's title bar to zoom

默认情况下,该功能是没有对应的快捷键的,但是我们可以手动设置

  1. System Preferences->Keyboard->Shortcuts->App Shortcuts
  2. 新增快捷键
    • Application:所有类型
    • Menu TitleZoom
    • Keyboard Shortcut:自定义,我设置的是⌃ + ⌘ + M

1.14 修改大小写切换方式

  1. System Preferences->Keyboard->Text Input Edit->Press and hold to enable typing in all uppercase
    • 14.x 没有这个配置选项了

1.15 如何修改用户名

Change the name of your macOS user account and home folder

1.16 如何修改HostName/ComputerName

1
2
3
4
5
6
7
scutil --get HostName
scutil --get LocalHostName
scutil --get ComputerName

sudo scutil --set HostName xxx
sudo scutil --set LocalHostName xxx
sudo scutil --set ComputerName xxx

1.17 禁用密码校验规则

1
2
3
4
5
# back up all account policies
pwpolicy getaccountpolicies > back_policies.xml

# clean account policies
pwpolicy -clearaccountpolicies

2 Homebrew

2.1 常用操作

1
2
3
4
5
6
7
8
9
10
11
# 安装软件
brew install <software>

# 卸载软件
brew uninstall <software>

# 软件安装路径
brew list <software>

# 查看软件信息
brew info <software>

2.2 国内安装Homebrew

参考国内安装homebrew

常规安装脚本(推荐/完全体/几分钟安装完成):

1
/bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

极速安装脚本(精简版/几秒钟安装完成):

1
/bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" speed

卸载脚本:

1
/bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"

FAQ

2.3 更换Homebrew镜像源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# step 1: 替换brew.git
cd "$(brew --repo)"
# 中国科大:
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 清华大学:
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# step 2: 替换homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
# 中国科大:
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 清华大学:
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# step 3: 替换homebrew-bottles
# 中国科大:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
# 清华大学:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

# step 4: 应用生效
brew update

2.4 常用软件下载

1
2
3
brew install openjdk@11
brew install maven
brew install protobuf

2.4.1 解压缩

1
2
3
4
5
file xxx.zip

unzip xxx.zip
brew install unar
unar xxx.zip

3 常用功能

3.1 升级bash

1
2
3
brew install bash
sudo mv /bin/bash /bin/bash.origin
sudo ln -s /usr/local/bin/bash /bin/bash

注意,sudo mv /bin/bash /bin/bash.origin可能因为权限的问题,无法成功执行,这时,我们需要关闭Mac的SIP机制

3.2 开启关闭SIP

  1. 进入恢复模式:
    • Intel:重启,然后按住⌘ + R,直到看到logo后松开
    • Arm:关机,按住开机键10s以上,直至进入恢复模式
  2. 出现界面之后,上面菜单栏 -> 实用工具 -> 终端
  3. 在Terminal中输入csrutil disable关闭SIP(csrutil enable打开SIP)
  4. 重启

3.3 开启关闭任何来源

1
2
3
4
5
# 开启
sudo spctl --master-disable

# 关闭
sudo spctl --master-enable

3.4 刻录iso文件

1
2
3
4
5
6
7
8
9
10
11
# 先列出所有设备
diskutil list

# 找到u盘对应的设备,比如这里是 /dev/disk6,卸载它
diskutil unmountDisk /dev/disk6

# 烧制ISO文件到u盘
sudo dd if=<iso文件路径> of=/dev/disk6 bs=1m

# 弹出磁盘
diskutil eject /dev/disk6

3.5 打开/禁止产生.DS_Store文件

1
2
3
4
5
# 禁止
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE

# 打开
defaults delete com.apple.desktopservices DSDontWriteNetworkStores

3.6 开启HiDPI

macOS开启HiDPI

3.7 清理磁盘

macOS 系统占用储存空间太大怎么办?

最近我的磁盘容量快被系统吃满了,排查了一下,发现这几个路径

  1. /Library/Application Support
    • /Library/Application Support/Symantec/AntiVirusSymantec这个软件一直在做备份
  2. /Library/Caches
  3. ~/Library/Caches
    • ~/Library/Caches/IntelliJIdea2018.1IntelliJIdea的一些缓存数据

3.8 卸载itunes

为什么要卸载,升级完mac之后,发现某些应用的f8快捷键失效了,一按f8就会自动打开itunes

我们是无法通过正常方式卸载itunes的,sudo rm -rf /System/Applications/Music.app会提示Operation not permitted,即便切到root账号也无法执行,这是因为mac对此类行为做了安全防护

我们可以通过csrutil disable解除这个限制。但是该命令需要到恢复模式才能用

如何进入恢复模式:重启电脑,按COMMAND+R组合键进入恢复模式

进入恢复模式后,在屏幕上方点击实用工具->终端,然后再执行csrutil disable即可

当关闭mac的System Integrity Protection功能之后,再次尝试删除itunues,发现还是删除不了,这次提示的是Read-only file system,无语

后来在Stop F8 key from launching iTunes?找到了解决方案

  • System Preferences -> Keyboard -> Keyboard
  • 取消Use all F1,F2,etc. keys as standard function keys选项的勾选

4 Iterm2

智能选中

  1. 双击选中
  2. 三击选中整行
  3. 四击智能选中
  4. 选中即复制

按住Command

  1. 拖拽选中字符串
  2. 点击url,访问网页
  3. 点击文件,用默认程序打开此文件
  4. 点击文件夹,在Finder中打开
  5. 同时按住option键,可以以矩形选中,类似于vim中的ctrl v操作

常用快捷键

  1. 切换窗口:⌘+←⌘+→⌘+数字
  2. 新建窗口:⌘+t
  3. 垂直切分当前窗口:⌘+d
  4. 水平切分当前窗口:⌘+⇧+d
  5. 智能查找:⌘+f
  6. 历史记录窗口:⌘+⇧+h
  7. 全屏所有tab:⌘+⌥+e
  8. 锁定鼠标位置:⌘+/

设置⌥+←⌥+→以单词为单位移动光标

  1. 首先打开iTerm2的preferences–>profile–>Keys,将常用的左Alt键设置为换码符(escape character)。如下图所示
    • fig1
  2. 接下来在Key mappings中找到已经存在的⌥←⌥→,如果没有的话,就新建这两个快捷键
  3. ⌥←的设置修改为如下内容
    • Keyboard Shortcut: ⌥←
    • Action: Send Escape Sequence
    • Esc+: b
  4. ⌥→的设置修改为如下内容
    • Keyboard Shortcut: ⌥→
    • Action: Send Escape Sequence
    • Esc+: f

设置滚动行数

  • Preferences->Profiles->Terminal

设置语言

  • export LANG=en_US.UTF-8
  • export LANG=zh_CN.UTF-8

4.1 更换主题

本小节转载摘录自iTerm 2 && Oh My Zsh【DIY教程——亲身体验过程】

先上效果

iterm2-theme

步骤1:安装iterm2,不赘述

步骤2:安装ohmyzsh,不赘述。有时国内下载不下来,可以参考下面的步骤安装

1
2
3
git clone https://github.com/ohmyzsh/ohmyzsh.git --depth 1
cd ohmyzsh/tools
./install.sh

步骤3:安装Powerline

1
2
sudo brew install pip
sudo pip install powerline-status

步骤4:安装Powerline的字体库

1
2
3
4
5
git clone https://github.com/powerline/fonts.git --depth 1
cd fonts
./install.sh
cd ..
rm -rf fonts

安装时,会提示所有字体均已下载到/Users/<user name>/Library/Fonts

步骤5:将iterm2的字体设置为Powerline的字体

step5

在iterm2中使用Powerline字体:Preferences -> Profiles -> Text

步骤6:安装配色方案solarized

1
2
git clone https://github.com/altercation/solarized.git --depth 1
open solarized/iterm2-colors-solarized

上面的open命令会弹出finder,然后在弹出的finder中,双击Solarized Dark.itermcolors以及Solarized Light.itermcolors便可将配色方案安装到iterm2中

然后在iterm2中选择该配色方案即可:Preferences -> Profiles -> Colors

step6

步骤7:安装agnoster主题

1
2
3
4
5
git clone  https://github.com/fcamblor/oh-my-zsh-agnoster-fcamblor.git --depth 1
cd oh-my-zsh-agnoster-fcamblor
./install
cd ..
rm -rf oh-my-zsh-agnoster-fcamblor

这些主题会被安装到~/.oh-my-zsh/themes目录下,然后修改~/.zshrc文件,将ZSH_THEME配置项的值改成agnoster

如果你选择了白色背景的话,agnoster也需要进行一些调整

  1. ~/.zshrc增加配置项SOLARIZED_THEME="light"
  2. ~/.oh-my-zsh/themes/agnoster.zsh-theme修改背景
    • 找到关键词build_prompt,这就是命令提示符的全部构成,每一个配置项的颜色都可以单独调整
    • prompt_contextprompt_status为例,将prompt_segment后面接的black改为white
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    prompt_context() {
    if [[ "$USERNAME" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment white default "%(!.%{%F{yellow}%}.)%n@%m"
    fi
    }

    prompt_status() {
    local -a symbols

    [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
    [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
    [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"

    [[ -n "$symbols" ]] && prompt_segment white default "$symbols"
    }

步骤8:安装zsh-syntax-highlighting用于高亮指令

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

然后修改~/.zshrc文件,修改配置项plugins,添加zsh-syntax-highlighting

1
plugins=(<原有插件> zsh-syntax-highlighting)

步骤9:安装zsh-autosuggestions用于指令提示

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

然后修改~/.zshrc文件,修改配置项plugins,添加zsh-autosuggestions

1
2
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=red,bold'
plugins=(<原有插件> zsh-autosuggestions)

如果在iterm2中通过ssh访问远程主机,也想获得上述效果,那么需要在远程主机上执行如下几个步骤:

  • 步骤2
  • 步骤7
  • 步骤8
  • 步骤9

4.1.1 Tips

  1. 登录时,命令行提示符显式的是~,但是执行cd后,显示的目录是用户目录的绝对路径,比如/home/test。这是因为HOME变量设置有问题,该变量最后不能有/符号,否则在将主目录替换成~的时候就会替换失败

4.2 Alt + f/b在ssh场景下失效

bash默认使用emacs模式,在该模式下,光标按单词移动的快捷键是Alt + b以及Alt + f,但是mac是没有这两个快捷键的,可以通过设置profile来解决这个问题,步骤如下:

  1. Preferences -> Profiles -> Keys -> Key Mappings -> +:新建快捷键
    1. Alt + b的替代快捷键
      • Shortcut⌥←
      • Action:选择Send Escape Sequence,填b
    2. Alt + f的替代快捷键
      • Shortcut⌥→
      • Action:选择Send Escape Sequence,填f

4.3 常用配置

  1. 光标形状
    • Preferences->Profile->Text->Cursor

5 Karabiner-elements

外接如Filco的键盘,需要将win以及alt这两个键位进行交换。其中win对应command键,alt对应option

  • Keys in pc keyboards - application -> Modifier keys - fn
  • Modifier keys left_command -> Modifier keys left_option
  • Modifier keys left_option -> Modifier keys left_command

6 常见问题

6.1 VirtualBox(rc=-1908)

解决方式如下(请挨个尝试):

方法1:

1
sudo "/Library/Application Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh" restart

方法2:

1
2
3
4
sudo kextload -b org.virtualbox.kext.VBoxDrv
sudo kextload -b org.virtualbox.kext.VBoxNetFlt
sudo kextload -b org.virtualbox.kext.VBoxNetAdp
sudo kextload -b org.virtualbox.kext.VBoxUSB

方法3:

1
2
3
4
5
6
7
1. 进入恢复模式:重启,然后按住 ⌘ + R
2. 恢复模式中,上面菜单栏->实用工具->启动安全性实用工具,选择无安全性
3. 恢复模式中,上面菜单栏->实用工具->终端,在terminal中输入 csrutil disable
4. 恢复模式中,上面菜单栏->实用工具->终端,在terminal中输入 spctl kext-consent add VB5E2TV963,其中 VB5E2TV963 是 Oracle 的 Developer ID
5. 重启
6. 在系统偏好设置->安全与隐私->允许Virtual Box
7. 重启

6.2 VirtualBox cannot enable nested VT-x/AMD-V

nested VT-x/AMD-V这个特性不开的话,如果在虚拟机里面安装了VirtualBox,那么这个VirtualBox只能安装32位的系统

Virtualbox enable nested vtx/amd-v greyed out

1
VBoxManage modifyvm <vm-name> --nested-hw-virt on

6.3 您没有权限来打开应用程序

1
sudo xattr -r -d com.apple.quarantine <app path>

6.4 中文输入法卡顿

以下步骤可以解决Chrome中的卡顿问题(通用问题)

  1. 系统偏好设置->键盘
    • 键盘:按键重复调到最快,重复前延迟调到最短
    • 文本:所有的功能都关了,什么联想、提示之类的功能

以下步骤可以解决Chrome中的卡顿问题,参考How To Fix Input Lag And Slow Performance In Google Chrome

  • Chrome -> Settings -> Advanced -> System -> Disable Use hardware acceleration when available
    • 开启或关闭可能都会有问题,重新切换一下开关状态可以恢复

以下步骤可以解决VSCode中的卡顿问题,参考Lagging/freezing using VSCode Insiders in Big Sur

1
codesign --remove-signature /Applications/Visual\ Studio\ Code.app/Contents/Frameworks/Code\ Helper\ \(Renderer\).app

以下步骤可以解决中文输入法卡顿的问题,参考程序开久了之后中文输入法卡顿,不知道怎么解决-Page2

  1. 系统偏好设置->调度中心
    • 显示器具有单独的空间(取消该选项,取消后会导致其他问题,比如Dock无法跟随鼠标在两个屏幕之间切换)

6.5 滚动条总是自动隐藏

系统偏好设置 -> 通用 -> 选择始终显示滚动条

6.6 登录酒店 WIFI 无法弹出登录页面

如果使用了 Proxy SwitchyOmega 配置代理,那么默认新连接的 WIFI 也会带上相关的配置,需要通过如下方式手动删除掉:

System Settings -> Network -> Details -> Proxies

6.7 配置开机自启动

  • 14.x及以上版本:搜索Login Items

7 参考