跳转至

MacOS Guide Chapter 4

IDE and text editor

Vim

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems and with Apple macOS.

Installation

To install the latest version, use homebrew:

Text Only
1
brew install vim

The Ultimate vimrc

The Ultimate vimrc is a collection of vimrc configurations to make easy the usage of vim.

To download the The Ultimate vimrc, you need to install the git client. If you need install it, use home brew:

Text Only
1
brew install git

Now, download the vimrc files:

Text Only
1
git clone https://github.com/amix/vimrc.git ~/.vim_runtime

To install the complete version, run:

Text Only
1
sh ~/.vim_runtime/install_awesome_vimrc.sh

To install the basic version, run:

Text Only
1
sh ~/.vim_runtime/install_basic_vimrc.sh

Update

To update the vimrc scripts, run:

Text Only
1
cd ~/.vim_runtime && git pull --rebase && cd -

Maximum Awesome

Maximum Awesome is a collection of vim configuration and plugins, like a configuration manager for the vim environment.

Installation

To install it, just make a clone of the repository with the git client:

Text Only
1
git clone https://github.com/square/maximum-awesome.git

Then install it:

Text Only
1
2
cd maximum-awesome
rake

NOTE: the rake command will install all dependencies needed

My Configuration of Vim

You can gain an insight into Vim Configuration in Vim-Conf @bxhu

NeoVim

Emacs

Emacs is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as the extensible, customizable, self-documenting, real-time display editor.

Development of the first Emacs began in the mid-1970s, and work on its direct descendant, GNU Emacs, continues actively as of 2017.

Installation

There are many Emacs clients on macOS. The recommended version on macOS is Emacs Mac Port, but others are good as well.

Many useful features are built with Emacs Mac Port, e.g. environment variables, full screen, visual enhancements and so on.

Link the Homebrew tap first.

Text Only
1
brew tap railwaycat/emacsmacport
  • Method 1: Install with brew cask.
Text Only
1
brew install --cask emacs-mac

There are three available versions, emacs-macemacs-mac-official-iconemacs-mac-spacemacs-icon.

  • Method 2: Install using brew.
Text Only
1
brew install emacs-mac [options]

Emacs plus

Start off by tapping the official emacs-plus cask.

Text Only
1
brew tap d12frosted/emacs-plus

Emacs Plus contains separate formulas for different Emacs versions:

  • emacs-plus - installs Emacs 26, current release version.
Text Only
1
brew install emacs-plus [options]
  • emacs-plus@27 - installs Emacs 27, next release version.
Text Only
1
brew install emacs-plus@27 [options]
  • emacs-plus@28 - installs Emacs 28, development version.
Text Only
1
brew install emacs-plus@28 [options]

Click here to see available options:

Note: 1) You might want to install exec-path-from-shell if you are using Emacs plus. It takes care of your environment variables. 2) To have the title bar match your theme background color, consider using instead: brew install emacs-plus --HEAD --with-natural-title-bars

Spacemacs

Spacemacs is a new way to experience Emacs -- a sophisticated and polished set-up focused on ergonomics, mnemonics and consistency.

Spacemacs can be used naturally by both Emacs and Vim users -- you can even mix the two editing styles. Switching easily between input styles makes Spacemacs a great tool for pair-programming.

Installation

  1. If you have an existing Emacs configuration, back it up first:

    Text Only
    1
    2
    3
    cd ~
    mv .emacs.d .emacs.d.bak
    mv .emacs .emacs.bak
    

    Don't forget to backup and remove ~/.emacs file otherwise Spacemacs WILL NOT load since that file prevents Emacs from loading the proper initialization file.

  2. Clone the repository:

    Text Only
    1
    git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d
    

    master is the stable branch and it is immutableDO NOT make any modification to it or you will break the update mechanism. If you want to fork Spacemacs safely use the develop branch where you handle the update manually.

  3. (Optional) Install the Source Code Pro font.

    If you are running in terminal you'll also need to change font settings of your terminal.

  4. Launch Emacs. Spacemacs will automatically install the packages it requires. If you get an error regarding package downloads then you may try to disable the HTTPS protocol by starting Emacs with

    Text Only
    1
    emacs --insecure
    

    Or you can set the dotspacemacs-elpa-https to nil in your dotfile to remove the need to start Emacs with --insecure argument. You may wish to clear out your .emacs.d/elpa directory before doing this, so that any corrupted packages you may have downloaded will be re-installed.

  5. Restart Emacs to complete the installation.

You can learn more on Spacemacs Tutorial Video

Visual Studio Code

Visual Studio Code is a lightweight code editor with support for many programming languages through extensions

Installation

To install the latest version, use Homebrew:

Text Only
1
brew install --cask visual-studio-code

macOS integration

Launch VS Code from the command line.

After that, you can launch VS Code from your terminal:

  • code . will open VS Code in the current directory
  • code myfile.txt will open myfile.txt in VS Code

Useful Extensions

Python

  • Python - Python code highlighting

    To enable auto-formatting on "Save", i.e. ⌘ + S, configure the following:

    1. Change the default formatter to Black instead of Autopep8. Critical to avoid large diffs. Go to Preferences -> User Settings and update the setting python.formatter.provider to Black

    2. Enable Format on Save Setting: Editor: Format On Save setting on Code -> Preferences -> Settings

JavaScript

  • ESLint - Useful to check JavaScript errors and helps in auto-formatting the code
  • Prettier - JavaScript code formatter

SQL

Markdown

GitLens

  • GitLens - Supercharge the Git capabilities built into VSCode

Docker

  • Docker - Create, manage, and debug images from within VSCode

JSON

  • Paste JSON as Code - Infers types from sample JSON data, then outputs strongly typed models and serializers for working with that data in your desired programming language

Live Server

  • Live Server - Launches a local development server with live reloading for both static and dynamic

VS Code Icons

  • vscode-icons - Adds unique icons to distinguish different file extensions (easier to glance through your directories)

Configuration of C++ and some commonly used language

reference - mainly copied from Yunfi's Blog

本文主要应用场景是单文件的编译。如果使用 CMake 等工具,则本文中的一部分配置并不一定兼容。

主要分为两步操作,使用 clang/lldb 编译和调试,以及使用 clangd 进行补全和检查,这两者应该是互相独立的,可以只使用其中一个,但是找到的大多数教程好像都是两个混在一起搞得。

本文的配置不使用 C/C++ 插件 和 Code Runner 插件!!请禁用或者卸载它们。由于 VSCode 和 C/C++ 插件的一些 bug,我选择绕开它们,使用 CodeLLDB 和 Clangd 来替代。

我的环境:MacOS Sonoma 14.3

使用 clang/lldb 进行单文件编译和调试

  1. 确保 clang++ 已经正确安装(通过 clang++ -v 可以验证)

    • 对于 macOS,运行 xcode-select --install 可以安装好本文用到的所有包
    • 对于 Linux,下载 llvm 包,大概率包含了本文用到的所有包
    • vscode 已启用 CodeLLDB 插件(报错无法下载可以先按报错给的 url 用浏览器下载,然后手动安装)
  2. 卸载微软提供的 C/C++ 插件!!!也不要使用 Code Runner 插件。

  3. tasks.json,放入.vscode 文件夹中

Bash
 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
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++", // `which clang++` may help you find this path
      "args": [
        "--std=c++17",
        "-fcolor-diagnostics",
        "-fansi-escape-codes",
        "-g",
        "${file}",
        "-o",
        "${workspaceFolder}/.build/${fileBasenameNoExtension}"
        "-fstandalone-debug", // to enable viewing std::string etc. when using lldb on Windows or Linux 
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ]
}

我的习惯是把所有的可执行文件放到 ./build/ 文件夹下,如果不这么做的话,改变 3、4 步中的文件路径,以及忽略第五步

  1. launch.json,放入.vscode 文件夹中
Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C/C++: clang++ build and debug active file customize",
      "type": "lldb",
      "request": "launch",
      "program": "${workspaceFolder}/.build/${fileBasenameNoExtension}",
      "args": [],
      "cwd": "${workspaceFolder}",
      "preLaunchTask": "C/C++: clang++ build active file"
    }
  ]
}
  1. 在文件夹里新建一个 .build 文件夹( macOS / Linux 必做)

  2. 按 F5,就可以编译调试了( fn + F5 )

这样的配置下,右上角不会有运行的那个小按钮。由于未知问题,这个又 C/C++ 插件提供的按钮不会同步 tasks.json 和 launch.json 中的配置,所以放弃使用了那个插件。

使用 clangd 自动补全、代码检查

  1. 确保已安装 clangd(应该和 clang++ 在一个包里的,通过 clangd --version 检查)

  2. 安装 VScode 插件 clangd

  3. 在工作区根目录下新建一个 compile_flags.txt,这是用来为 clangd 指定参数的,比如使用的标准或是标准库路径之类。内容就是编译选项,一行一个。这里只写了一个标准作为例子

    Text Only
    1
    --std=c++17
    

一般来说 clangd 的参数是由 compile_commands.json 指定,由 CMake 等构建工具自动生成。但是由于在我的需求中对每一个文件都是相同的编译参数,所以可以手写 compile_flags.txt 统一管理。 具体查看 JSON Compilation Database Format Specification

关于 .clang-format 文件

我的习惯是直接放在 ~ 下(如果你的代码都放在你的~和其子文件夹里的话)

生成的话,官方文档的那个网页实在是太丑了,我直接选择去 CLion 里配好,然后导出为 .clang-format,既可视化又方便

贴一下我的.clang-format

Text Only
 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
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
  AfterCaseLabel: false
  AfterClass: false
  AfterControlStatement: Never
  AfterEnum: false
  AfterFunction: false
  AfterNamespace: false
  AfterUnion: false
  BeforeCatch: false
  BeforeElse: false
  IndentBraces: false
  SplitEmptyFunction: false
  SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 0
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never

故障排除

macOS 无法使用集成终端调试

  • 具体表现为在 enternalConsole 为 false 的时候并不会像在 Windows 中一样出现一个新的集成终端用来输入,导致没有地方输入从而无法调试。
  • 这种情况出现于使用了微软提供的 C/C++ 插件,并且 launch.json 中 type 为 “cppdbg” 时。这是 VSCode 的 bug,微软文档提供了一种解决方案,但是实测没效果()
  • 解决方案一是将 enternalConsole 设为 true,如果接受使用外部终端进行调试的话;
  • 解决方案二是不用微软提供的插件,而是使用 CodeLLDB 插件提供的调试选项。也就是按照本文进行操作

吐槽:VSCode 的 bug 还真不少,在 Windows 上外部终端用不了,Mac 上却又只能用外部终端()

VSCode 在 WSL 中无法正确联网

  • 主要原因是 vscode 会在 wsl 中使用在 Windows 下的代理 ip+ 端口(一般是 127.0.0.1:端口 ),
  • 解决方案一:给 WSL 设置 http_proxy 和 https_proxy,把 127.0.0.1 改成局域网下 windows 的 ip
  • 解决方案二:关闭系统代理,直接使用 TUN /增强模式

懒人直通车(一键配置)

reference

两个脚本,分别VSCode在 Linux 和 Windows 下的一键脚本,轻松配出环境