Emacs 入门和命令查询:Emacs 入门教程
熟悉了Emacs的基本使用之后,我们现在来尝试使用 Emacs 配置一个比较现代、实用的C++开发环境(包括代码补全、跳转、语法检查、调试等)
Emacs 的配置文件为:~/.emacs.d/init.el
将下列的配置信息保存到 Emacs 的配置文件中,保存并重启打开 Emacs 就可以生效。
安装必要的包管理器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| (require 'package)
(setq package-archives '(("gnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/") ("melpa" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/"))) (package-initialize)
(unless (package-installed-p 'use-package) (package-refresh-contents) (package-install 'use-package))
(unless (package-installed-p 'use-package) (package-refresh-contents) (package-install 'use-package))
|
基本的 C++ 模式支持
Emacs 自带 cc-mode
主模式,支持C、C++、Java、Objective-C、AWK 等一系列在语法和风格上类似的“类C”语言的语法高亮和智能缩进。
安装 clang-format
针对Ubuntu:
1
| sudo apt install clang-format
|
Mac: brew install clang-format
Windows: 用 LLVM 安装包
我们可以安装 clang-format
实现代码自动排版:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| (use-package clang-format :ensure t :commands (clang-format-buffer clang-format-region) :init (setq clang-format-style "file") (setq clang-format-fallback-style "llvm") :bind (("C-c f" . clang-format-buffer) ("C-c r" . clang-format-region)))
|
我们可以在 Emacs 配置一个钩子函数,每次保存时都对代码自动自动排版
1 2 3 4
| (add-hook 'c-mode-common-hook (lambda () (add-hook 'before-save-hook 'clang-format-buffer nil t)))
|
代码补全:LSP + clangd
最主流的方法是用 LSP(Language Server Protocol) 与 clangd 配合。LSP (Language Server Protocol) 是一种开放标准协议,它允许编辑器或 IDE 与语言服务器进行通信,从而提供智能代码功能。在 Emacs 中,LSP 通过特定的包实现,将编辑器转变为功能强大的现代开发环境。
Emacs 配置
推荐 lsp-mode
+ lsp-ui
:
1 2 3 4 5 6 7 8 9 10 11
| (use-package lsp-mode :hook ((c++-mode . lsp) (c-mode . lsp)) :init (setq lsp-clients-clangd-executable "clangd") :commands lsp)
(use-package lsp-ui :commands lsp-ui-mode)
|
这样就有:智能补全、跳转、悬停文档、诊断信息等。关于 LSP 的详细使用,包括快捷键绑定等,后面写文章总结。
自动补全:company-mode
1 2 3 4 5
| (use-package company :hook (after-init . global-company-mode) :config (setq company-minimum-prefix-length 1 company-idle-delay 0.0))
|
例如:
当输入 myvec.
的时候,它会弹出一个框,选择相应的成员函数等。
语法检查:flycheck
1 2 3
| (use-package flycheck :hook (prog-mode . flycheck-mode))
|
调试:GDB/ dap-mode
Emacs 自带 M-x gdb
,也可以用 dap-mode
(VSCode 风格的调试器前端):
1 2 3 4 5
| (use-package dap-mode :after lsp-mode :config (dap-auto-configure-mode))
|
项目管理:project.el / projectile
1 2 3 4
| (use-package projectile :init (projectile-mode +1) :bind-keymap ("C-c p" . projectile-command-map))
|
projectile包是一个强大的项目管理和导航工具,详细用法后面写专题文章总结。
模板与 Snippets
这个配置用于设置 Emacs 的 YASnippet 系统,这是一个强大的代码片段管理工具,可以显著提高编码效率。
1 2 3 4
| (use-package yasnippet :hook (prog-mode . yas-minor-mode))
(use-package yasnippet-snippets) ;; 常用代码片段集合
|
完整配置文件
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
(require 'package)
(menu-bar-mode -1) (tool-bar-mode -1)
(setq package-archives '(("gnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/") ("melpa" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/"))) (package-initialize)
(unless (package-installed-p 'use-package) (package-refresh-contents) (package-install 'use-package))
(require 'use-package) (setq use-package-always-ensure t)
(use-package clang-format :ensure t :commands (clang-format-buffer clang-format-region) :init (setq clang-format-style "file") (setq clang-format-fallback-style "llvm") :bind (("C-c f" . clang-format-buffer) ("C-c r" . clang-format-region)))
(add-hook 'c-mode-common-hook (lambda () (add-hook 'before-save-hook 'clang-format-buffer nil t)))
(use-package lsp-mode :ensure t :hook ((c++-mode . lsp) (c-mode . lsp)) :init (setq lsp-clients-clangd-executable "clangd") :commands lsp)
(use-package lsp-ui :commands lsp-ui-mode)
(use-package company :hook (after-init . global-company-mode) :config (setq company-minimum-prefix-length 1 company-idle-delay 0.0))
(use-package flycheck :hook (prog-mode . flycheck-mode))
(use-package dap-mode :after lsp-mode :config (dap-auto-configure-mode))
(use-package projectile :init (projectile-mode +1) :bind-keymap ("C-c p" . projectile-command-map))
|