切换到 zsh —— 新手必备操作

从 macOS Catalina 开始,系统默认使用 zsh 作为 Shell。如果你还停留在 Bash,不妨尝试切换到更现代的 zsh:

  1. 点击苹果菜单  > 系统偏好设置 > 用户与群组。
  2. 按住 Control 键点击你的用户名,选择“高级选项”。
  3. 在“登录 Shell”菜单中,选择 zsh,然后点击“好”。

简单吧?一键切换到 zsh,你会发现新世界!

轻松配置 zsh

zsh 的配置文件是 ~/.zshrc,它可以让你的终端变得更好用。以下是一些推荐的设置:

常用别名

1
2
3
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

自定义终端显示

用下面的代码让你的终端看起来更酷:

1
export PS1="%10F%m%f:%11F%1~%f \$ "

修改后别忘了执行 source ~/.zshrc 来加载配置。

让补全更智能

默认情况下,zsh 在补全路径时区分大小写。如果你希望忽略大小写并开启智能补全,可以试试这个方法:

1
2
autoload -Uz compinit && compinit
zstyle ':completion:*' matcher-list 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}'

有时你可能会遇到权限问题,例如:

1
2
zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]?

解决方法如下:

1
2
chmod g-w /usr/local/share/zsh
chmod g-w /usr/local/share/zsh/site-functions

搞定后,再次执行 source ~/.zshrc,就可以愉快地用上智能补全了!

优化 GitHub 访问速度

如果你觉得 GitHub 加载慢,可以试试手动修改 Hosts 文件:

  1. 打开 Hosts 文件:

    1
    sudo code /etc/hosts

    如果你没有安装 VSCode,也可以用其他编辑器。

  2. 添加以下内容:

    1
    2
    3
    140.82.113.4 github.com
    140.82.114.5 api.github.com
    185.199.108.133 raw.githubusercontent.com
  3. 保存后刷新网络缓存:

    1
    sudo killall -HUP mDNSResponder

完成后,你会发现访问速度快了不少!

给 Git 添加一点小魔法

Git 提供了很多方便的配置项,比如:

1
2
3
4
5
6
git config --global core.quotepath false
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --date=format:'%Y-%m-%d %H:%M:%S' --abbrev-commit"

试试这些别名配置,让你的 Git 操作更高效!

用 Vim 让代码编辑更顺滑

如果你习惯用 Vim 写代码,可以用以下方法优化它的显示效果:

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
colorscheme default     " 设置颜色主题

syntax on " 语法高亮

filetype on " 检测文件的类型

set number " 显示行号
set cursorline " 用浅色高亮当前行
"autocmd InsertLeave * se nocul
"autocmd InsertEnter * se cul

set ruler " 在编辑过程中,在右下角显示光标位置的状态行
set laststatus=2 " 显示状态栏 (默认值为 1, 无法显示状态栏)
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 设置在状态行显示的信息

set tabstop=4 " Tab键的宽度
set softtabstop=4
set shiftwidth=4 " 统一缩进为4

set autoindent " vim使用自动对齐,也就是把当前行的对齐格式应用到下一行(自动缩进)
set cindent " (cindent是特别针对 C语言语法自动缩进)
set smartindent " 依据上面的对齐格式,智能的选择对齐方式,对于类似C语言编写上有用

set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离

set incsearch " 输入搜索内容时就显示搜索结果
set hlsearch " 搜索时高亮显示被找到的文本

set foldmethod=indent " 设置缩进折叠
set foldlevel=99 " 设置折叠层数
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" 用空格键来开关折叠

" 自动跳转到上次退出的位置
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

~/.vimrc 文件中添加这些配置,你的 Vim 使用体验将大幅提升。

安装 Brew,玩转 macOS 软件管理

Homebrew 是 macOS 上不可或缺的软件包管理工具。安装方法如下:

1
2
3
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
# 卸载脚本:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"

安装完成后,你可以用它轻松安装各种软件,比如 NVM、MySQL 等。

结语

看到这里,你已经掌握了 macOS 的基础配置和优化技巧。不管是日常使用还是开发工作,这些设置都能让你的 Mac 用起来更顺手、更高效。赶紧试试吧,享受一个更强大的 macOS!