拿到新版MacBook当然要先折腾一下

169 阅读3分钟

工欲善其事,必先利其器。

余近日在公司申请了一台新款MacBook Pro,16英寸,芯片Apple M1 Pro,内存32GB,HD512G,并回购了原来使用的老款MacBook Pro,15英寸,芯片Intel Core i7,内存16G,HD256G。

工作的快乐这不就又回来了嘛!

最直观的感受是,新款Mac散热效果不错,多半是功耗低,在正常开发使用过程中,触摸电池附近外壳,基本无明显发热感觉,而对比原来老款Mac,运行同样开发软件(Goland、Chrome、飞书),就会有明显发热感觉。另外新款Mac的电池续航时间相应的也有非常明显的延长。

余以为,拿到新电脑,第一件事莫过于配置开发环境,安装效率工具软件。根据之前的办公习惯,安装了如下软件,在此分享给诸位。

1. iTerm2

iterm2.com/,一款可以用来替代Mac自带的终端软件。

2. brew

brew.sh/,一款macOS/Linux包管理软件。

brew [search/info/install] tree 
brew install pstree 
brew install —-cask miniconda    // 带有--cask的,软件包会存储在 /opt/homebrew/Caskroom 目录下 

3. zsh

ohmyz.sh/,一款好用的shell软件,如果在zsh shell中找不到brew,则需要将brew配置到PATH环境变量中。

// 两种方式任选其一
export PATH=/opt/homebrew/bin:$PATH 添加到 ~/.zshrc 文件中
echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.zshrc
cat /etc/shells    // 查看当前系统中可用的shell类型
echo $SHELL    // 查看当前用户默认的shell
echo $0    // 查看当前用户正在使用的shell
cat /etc/passwd | grep `whoami`    // 查看用户配置的默认shell
ps -ef | grep `echo $$` | grep -v ps | grep -v grep    // 查看当前shell进程
ssh-keygen -t rsa   // 使用rsa加密算法,生成公私钥对,一直默认选择即可
cat ~/.ssh/id_rsa.pub    // 输出公钥内容,可以copy到github或用于ssh登录
    ssh-copy-id user@remote [-p port]    // ssh server默认监听22端口,将公钥上传到remote机器user家目录的~/.ssh/authorized_key文件中
    ssh user@remote -p port 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub    // 如果ssh-copy-id不可用,可以执行该指令上传公钥
scp a.txt user@remote:~/    // 将本地的a.txt文件上传到ip=remote的user家目录下
~/.ssh/config 创建文件,写入以下内容,可以实现更方便的登录远程主机
Host host
    HostName example.com或者ip
    User user
    IdentityFile ~/.ssh/id_rsa
    Port 22

zsh插件

// autosuggestions 根据命令使用历史,提供自动补全提示功能
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
// syntax-highlighting 提供语法高亮功能
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

安装完成后执行source ~/.zshrc或者重新打开终端窗口生效

4. Git

brew install git
git pull/push/add/commit/fetch
git commit -am "msg"
git log --pretty=oneline --graph —all
git reset --hard head
git rebase origin/master
git rebase --onto origin/master head~1 head
git stash [pop/apply/drop/list]
git checkout -b branch
git branch -d branch
git push -d origin branch
git remote prune origin
// git config文件中可以添加alias
[alias]
new = "!f() { git fetch && git checkout -b sandbox/`whoami`/`date +%m-%d`/${2:-`date +%H-%M`} origin/${1:-master}; }; f"
cin = commit --amend --no-edit
del = "!f() {git branch | grep sandbox | xargs git branch -D; }; f"
lg = log --pretty=online --graph --all
// 统计个人代码提交行数
git log --author="$(git config --get user.name)" --pretty=tformat: --numstat | grep -v vendor | grep -v output | grep -v script | grep -v conf | grep -E "go$" | awk '{ add += $1 ; subs += $2 ; total += $1 + $2 ; net += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s, net increase lines: %s\n",add,subs,total,net}'
--since=2018-10-01 --until=2018-12-31 添加起止时间
-n 1 指定最近1个commit

5. Go

brew install go
// Installed at /opt/homebrew/Cellar/go/1.20.2/bin/go
// Symbolic link /opt/homebrew/bin/go -> ../Cellar/go/1.20.2/bin/go
go get xxx/xxx/xxx   // 更新项目go.mod中的 xxx/xxx/xxx 依赖,并下载依赖对应版本到 /Users/anin/go/pkg/mod
go mod tidy    // 整理go.mod文件中依赖,下载go.mod中指定版本依赖至 /Users/anin/go/pkg/mod 中
go mod why xxx/xxx/xxx    // 检查依赖 xxx/xxx/xxx 的原因,并列出依赖链路
// go代码中格式化可能会出现多个空行,以下指令可以只保留一个空行
go mod tidy | git add . | git diff --name-only head | grep -E "go$" | xargs -I{} sh -c "gsed -i '/import (/, /)$/{/^$/d}' ./{} && goimports -w {}"
    brew install gsed    // installed at /opt/homebrew/bin/gsed
    go install golang.org/x/tools/cmd/goimports    // installed at /Users/anin/go/bin/goimports, 可能需要将 /Users/anin/go/bin 添加到PATH变量中
// 执行git merge或rebase代码时,go.mod与go.sum文件经常发生冲突,删除冲突标识,执行go mod tidy即可保留较新依赖版本,并修复go.sum文件
sh -c "sed '/<<<</d' go.mod | sed '/====/d' | sed '/>>>>/d' > go.mod.bak" && rm go.mod && mv go.mod.bak go.mod && go mod tidy

6. Goland

// 安装完成goland,创建go项目 
// 配置goland中GOROOT,/opt/homebrew/Cellar/go/1.20.2/libexec 
// 配置goland中Go Mudules,开启 Enable Go modules integration 
go mod init [module-path] 

7. Java

brew install openjdk
    brew install openjdk@8    // 如果安装openjdk8,对于M1芯片,无法直接安装,需要使用下面的指令 
    brew tap adoptopenjdk/openjdk
    brew install --cask adoptopenjdk8 // installed at /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home,在idea中配置该路径即可使用 

8. Idea

// 安装完成idea,创建java项目
Project Settings -> Modules 中配置jdk版本
ll /Library/Java/JavaVirtualMachines/
drwxr-xr-x  3 root  wheel    96B  4 21  2021 adoptopenjdk-8.jdk
lrwxr-xr-x@ 1 root  wheel    45B  4 10 12:48 openjdk.jdk -> /opt/homebrew/opt/openjdk/libexec/openjdk.jdk
// 根据需要添加 Module SDK即可

9. conda

brew install --cask miniconda    // conda有anaconda和miniconda两个版本,余倾向使用miniconda
conda init "$(basename "${SHELL}")"    // 执行该指令来setup your shell
conda create -n py37    // 创建一个空环境
conda activate py37    // 激活py37环境,会从默认的base切换到py37环境
conda install python=3.7    // 安装python3.7版本,在Apple Silicon系统上无法使用3.8以下版本,执行下面指令后才能安装python 3.7版本
    conda config --env --set subdir osx-64
conda --version    // 查看版本
conda env list    // 查看全部环境

10. Pycharm

// 安装完成pycharm,创建python项目
// 在project中设置python interpreter,选择conda environment,找到上面创建好的 py37 环境
/opt/homebrew/Caskroom/miniconda/base/envs/py37/bin/python

11. Alfred

www.alfredapp.com/,一款快速检索与自定义控制软件。

12. Spectacle

www.spectacleapp.com/,一款便捷的分屏软件,开源地址 github.com/eczarny/spe…,但是spectacle已经不再维护了,spectacle用户建议使用rectangle作为其开源替代,开源地址 github.com/rxhanson/Re…

至此工具软件安装完毕,开启效率办公模式。快乐如斯,幸甚至哉,文以记之。