时间轴

时间轴

2025-01-12

init

2025-10-17

add clang-format

2025-12-03

modify indent style

2025-12-15

add desktop-save-mode, add ibuffer, add magit


环境

使用 Windows Terminal + WSL2

WSL2 Arch Linux

配置

Windows Terminal

Windows Terminal 中打开设置,选择打开 JSON 文件,在 schemes 中添加配色方案

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
"schemes":
[
{
"background": "#0D1117",
"black": "#484F58",
"blue": "#58A6FF",
"brightBlack": "#6E7681",
"brightBlue": "#79C0FF",
"brightCyan": "#56D4DD",
"brightGreen": "#56D364",
"brightPurple": "#D2A8FF",
"brightRed": "#FFA198",
"brightWhite": "#FFFFFF",
"brightYellow": "#E3B341",
"cursorColor": "#2F81F7",
"cyan": "#39C5CF",
"foreground": "#E6EDF3",
"green": "#3FB950",
"name": "GitHub-Dark-Default",
"purple": "#BC8CFF",
"red": "#FF7B72",
"selectionBackground": "#E6EDF3",
"white": "#B1BAC4",
"yellow": "#D29922"
}
],

  • 在 archlinux 的配置中选择 外观 选项卡
    • 配色方案选择 GitHub-Dark-Default
    • 字体选择 FiraCode Nerd Font
    • 背景不透明度 86%,选择启用亚克力材料
  • 在 archlinux 的配置中选择 高级 选项卡
    • 铃声通知样式取消勾选声音,选择闪烁窗口和闪烁任务栏
  • 安装 win32yank.exe 加入到环境变量中,使得在命令行可以直接访问到

如果亚克力失效,关闭Windows节能模式即可

Arch Linux

archlinux 需要下载一些软件包

下载 yay 参考

注意下载后最好对 pacman 和 yay 进行换源,否则速度巨慢

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
# 设置locale
sudo nano /etc/locale.gen
# 找到#en_US.UTF-8 UTF-8解除注释
sudo locale-gen

sudo pacman -Sy emacs-nox
sudo pacman -Sy clang nvm ripgrep bear pandoc

# python/bash development
source /usr/share/nvm/init-nvm.sh
nvm list-remote
nvm install v20.19.4
nvm use v20.19.4

npm i -g pyright bash-language-server devicetree-language-server

# rust development
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup component add rust-analyzer

# 替换系统ls命令为exa命令
sudo pacman -S exa
# 如果是zsh,则写入zshrc
echo 'alias ll="ls -alF"' >> ~/.bashrc
echo 'alias ls="exa --icons"' >> ~/.bashrc
source ~/.bashrc

mkdir -p ~/.emacs.d/data/backup
mkdir -p ~/.emacs.d/data/autosave

Ubuntu20.04

参考:

Emacs

安装

archlinux可直接通过pacman安装,如果是Ubuntu考虑自己编译,如下:

1
2
3
4
5
6
7
8
9
10
# Optional: 卸载snapd
sudo apt-get purge snapd

# 下载emacs源码
wget https://mirrors.ustc.edu.cn/gnu/emacs/emacs-30.2.tar.xz
cd emacs
sudo apt install zlib1g-dev libgccjit-9-dev pkg-config libgnutls28-dev libtree-sitter-dev
./configure --without-x --without-ns --without-pgtk --with-native-compilation --with-json --with-threads --prefix=/usr/local
make -j$(nproc)
sudo make install
  • --without-x emacs-nox,不使用 X11(Xorg),只能用 emacs -nw(终端)
  • --without-ns 禁用 macOS 的 Cocoa / NS 窗口系统,只是防止 configure 自动探测到 macOS GUI
  • --without-pgtk 禁用 Pure GTK(Wayland 下的新 GUI 后端)
  • --with-native-compilation 启用 native-comp(.eln)将Elisp 编译成机器码
  • --with-json启用 原生 JSON 支持(libjansson)
  • --with-threads启用 多线程支持
  • --prefix=/usr/local安装到 /usr/local

配置

.emacs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
;;不显示menu-bar
(menu-bar-mode -1)
;;显示行号
(global-display-line-numbers-mode t)
;;在ModeLine显示列号
(column-number-mode 1)
;;代码的折叠
(add-hook 'prog-mode-hook 'hs-minor-mode)
;; 自动重载更改的文件
(global-auto-revert-mode 1)
;;开启可视化换行,自动折行
(global-visual-line-mode 1)
;; 括号匹配
(electric-pair-mode 1)
;; 自动缩进
(electric-indent-mode 1)

;; 鼠标模式
(xterm-mouse-mode 1)

;; 中文显示
(set-language-environment "utf-8")
(set-buffer-file-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-clipboard-coding-system 'utf-8)
(modify-coding-system-alist 'process "*" 'utf-8)
(prefer-coding-system 'utf-8)
(setq-default pathname-coding-system 'utf-8)
(setq default-process-coding-system '(utf-8 . utf-8)
locale-coding-system 'utf-8
file-name-coding-system 'utf-8
default-buffer-file-coding-system 'utf-8
slime-net-coding-system 'utf-8-unix
)

(setq find-file-encoding 'auto)
(setq-default coding-system-for-read 'utf-8)
(setq-default coding-system-for-write 'utf-8)


;; 基本设置
(setq-default window-combination-resize t ;; 新窗口平均其他左右窗口
x-stretch-cursor t ;; 将光标拉伸到字形宽度
indent-tabs-mode nil
tab-width 8
)

(setq ring-bell-function 'ignore
initial-scratch-message nil
inhibit-startup-message t
auto-save-file-name-transforms '((".*" "~/.emacs.d/data/autosave/" t)) ;; Write auto-save files to a separate directory
backup-directory-alist '(("." . "~/.emacs.d/data/backup/")) ;; Write backup files to a separate directory
create-lockfiles nil ;; Disable lockfiles
scroll-conservatively 10000 ;;防止页面滚动时跳动
scroll-step 1 ;; 当光标移动到边缘时,窗口每次只滚动一行
undo-limit 80000 ;; 提升撤销限制
display-time-default-load-average nil ;; 不显示系统负载
use-short-answers t
comp-deferred-compilation t ;; Emacs 28+
native-comp-async-report-warnings-errors nil
)



(defun my/kill-unwanted-buffers ()
"在 Emacs 启动时关闭一些不想要的 buffer."
(dolist (buf-name '("*scratch*"
;;"*Async-native-compile-log*" ;; can't be killed
;;"*Messages*"
))
(when (get-buffer buf-name)
(kill-buffer buf-name))))

(add-hook 'emacs-startup-hook #'my/kill-unwanted-buffers)

;; 启动速度统计
(add-hook 'emacs-startup-hook
(lambda ()
(message "Emacs ready in %s with %d garbage collections."
(format "%.2f seconds"
(float-time
(time-subtract after-init-time before-init-time)))
gcs-done)))

;; 如果用emacs打开文件则只打开文件,如果直接打开emacs则开启上次退出的非空界面
;; (add-hook 'after-init-hook
;; (defun if-desktop-read ()
;; (setq desktop-dirname "~/.emacs.d/desktop/"
;; desktop-base-file-name "emacs.desktop"
;; desktop-base-lock-name "lock"
;; desktop-path (list desktop-dirname)
;; desktop-save t
;; desktop-files-not-to-save "^$" ;reload tramp paths
;; desktop-load-locked-desktop nil
;; desktop-auto-save-timeout 10)
;; (desktop-save-mode 1)
;; (if (< (length command-line-args) 2)
;; (desktop-read)))
;; 'if-desktop-read)

;; 使用 Fira Code Nerd Font 字体
(when (display-graphic-p)
(set-face-attribute 'default nil
:font "FiraCode Nerd Font"
:height 120))


;; wsl与windos共享剪切版
(defun my/clipboard-to-windows (text &optional push)
(let ((process-connection-type nil))
(with-temp-buffer
(insert text)
(call-process-region (point-min) (point-max) "win32yank.exe" nil nil nil "-i"))))
(defun my/clipboard-from-windows ()
(string-trim-right
(with-output-to-string
(call-process "win32yank.exe" nil standard-output nil "-o"))))

(setq interprogram-cut-function 'my/clipboard-to-windows)
(setq interprogram-paste-function 'my/clipboard-from-windows)



;;设置镜像源
;; (setq package-archives
;; '(("melpa" . "https://melpa.org/packages/")
;; ("gnu" . "https://elpa.gnu.org/packages/")))
(setq package-archives '(("gnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
("nongnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/nongnu/")
("melpa" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))



(use-package package
:config
(unless (bound-and-true-p package--initialized)
(package-initialize)))

(setq use-package-verbose t
use-package-always-ensure t
use-package-minimum-reported-time 0.01)


;;通过github下载包
(unless (package-installed-p 'quelpa)
(with-temp-buffer
(url-insert-file-contents "https://raw.githubusercontent.com/quelpa/quelpa/master/quelpa.el")
(eval-buffer)
(quelpa-self-upgrade)))

;; 关闭自动升级和 verbose(减少启动开销)
(setq quelpa-upgrade-p nil
quelpa-verbose nil
quelpa-checkout-melpa-p nil)

(quelpa
'(quelpa-use-package
:fetcher git
:url "https://github.com/quelpa/quelpa-use-package.git"))

(require 'quelpa-use-package)

;; 防止 GC 触发太频繁
(use-package benchmark-init
:ensure t
:config
;; To disable collection of benchmark data after init is done.
(add-hook 'after-init-hook 'benchmark-init/deactivate))

(setq gc-cons-threshold (* 128 1024 1024 ))
(add-hook 'after-init-hook (lambda () (setq gc-cons-threshold (* 5 1024 1024))))

;; 最近打开的文件
(use-package recentf
:ensure t
;; Loads after 2 second of idle time.
:defer 2)

;;doom主题
(use-package doom-themes
:ensure t
:init
:config
(setq doom-themes-enable-bold t ; if nil, bold is universally disabled
doom-themes-enable-italic nil ; if nil, italics is universally disable
)
;; 加载主题(例如 doom-one)
(load-theme 'doom-tomorrow-night t)
(global-hl-line-mode 1)
(doom-themes-visual-bell-config) ;; 视觉提示“响铃”(visual bell)
)

;; dire 高亮
(use-package diredfl
:ensure t
:defer t
:hook (dired-mode . diredfl-mode))

(use-package nerd-icons-dired
:defer t
:quelpa (nerd-icons-dired :fetcher github :repo "rainstormstudio/nerd-icons-dired")
:after nerd-icons
:hook (dired-mode . nerd-icons-dired-mode))

(add-hook 'dired-mode-hook 'nerd-icons-dired-mode)

;;tab栏显示buffer
;; (use-package centaur-tabs
;; :ensure t
;; :demand
;; :init
;; :config
;; (defun centaur-tabs-hide-tab (x)
;; "隐藏特殊 buffer,保留 GUD/GDB buffer."
;; (let ((name (format "%s" x)))
;; (cond
;; ;; 保留所有 GDB/GUD 相关 buffer
;; ((string-match-p
;; "^\\*\\(gud\\|gdb\\)" name)
;; nil)
;; ;; 隐藏 *Messages*、*Async-native-compile-log* 等内部 buffer
;; ((string-prefix-p "*" name) t)
;; ;; Magit buffer
;; ((and (string-prefix-p "magit" name)
;; (not (file-name-extension name))) t)
;; ;;非文件不显示
;; ((not (buffer-file-name x)) t)
;; ;; 其他 buffer 显示
;; (t nil))))

;; (defun centaur-tabs-buffer-groups ()
;; "`centaur-tabs-buffer-groups' control buffers' group rules."
;; (list
;; (cond
;; ((or (string-equal "*" (substring (buffer-name) 0 1))
;; (memq major-mode '(magit-process-mode
;; magit-status-mode
;; magit-diff-mode
;; magit-log-mode
;; magit-file-mode
;; magit-blob-mode
;; magit-blame-mode
;; ))) "Emacs")
;; ((memq major-mode '(helpful-mode
;; help-mode)) "Help")
;; (t
;; (centaur-tabs-get-group-name (current-buffer))))))

;; (setq centaur-tabs-style "bar"
;; centaur-tabs-set-bar 'over
;; centaur-tabs-enable-key-bindings nil
;; centaur-tabs-set-icons t
;; centaur-tabs-icon-type 'nerd-icons
;; centaur-tabs-set-close-button nil
;; centaur-tabs-show-new-tab-button nil
;; centaur-tabs-set-modified-marker t
;; centaur-tabs-modified-marker "M"
;; centaur-tabs-cycle-scope 'tabs)

;; (centaur-tabs-mode t)
;; :bind
;; ("C-<left>" . centaur-tabs-backward)
;; ("C-<right>" . centaur-tabs-forward)
;; :hook
;; (dired-mode . centaur-tabs-local-mode)
;; )


;; modeline主题
(use-package doom-modeline
:ensure t
:custom
(doom-modeline-buffer-file-name-style 'truncate-except-project) ; 更智能的文件名截断
(doom-modeline-env-version t) ; 显示 Python/Rust 版本等
(doom-modeline-major-mode-icon t)
:init (doom-modeline-mode 1))



;;文件导航栏
;; (use-package neotree
;; :defer t
;; :ensure t
;; :bind (("<f8>" . neotree-toggle))
;; :config
;; (setq neo-theme 'nerd-icons)
;; (setq neo-smart-open t) ;; 打开时自动跳到当前文件路径
;; (setq neo-show-hidden-files t);; 显示隐藏文件
;; )
;; ;; neotree buffer 中强制截断长行
;; (add-hook 'neo-after-create-hook
;; (lambda (_)
;; ;; 不换行,禁用 visual-line(你全局开了的话)
;; (visual-line-mode -1)
;; (setq-local truncate-lines t)

;; ;; 用 “…” 代替右侧的 $ 截断标记
;; (let ((tbl (or buffer-display-table (make-display-table))))
;; (set-display-table-slot tbl 'truncation (make-glyph-code ?…))
;; (setq-local buffer-display-table tbl))))
;; ;; 允许 neotree 窗口宽度可调整
;; (with-eval-after-load 'neotree
;; (setq neo-window-fixed-size nil))


(use-package nerd-icons
:defer t
:custom
(nerd-icons-font-family "FiraCode Nerd Font")
)


(use-package undo-tree
:ensure t
:init (global-undo-tree-mode)
:config
;;将undo-tree生成的文件,都写入到~/.emacs.d/.cache,方便管理.
(setq undo-tree-history-directory-alist `(("." . "~/.emacs.d/.cache")))
)


;; enhancement for amx
;; 命令按历史排序
(use-package amx
:ensure t
:defer t
:init (amx-mode))


(use-package exec-path-from-shell
:ensure t
:init
:config
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize)))

;; Git工具
(use-package magit
:ensure t
:defer t
:bind (("C-x g" . magit-status))
)

(use-package markdown-mode
:ensure t
:defer t
:mode ("\\.md\\'" . markdown-mode)
:init (setq markdown-command "pandoc"))

(use-package dts-mode
:ensure t
:defer t
)

;; 语法解析
(use-package tree-sitter
:ensure t
;; :when (and (fboundp 'treesit-available-p)
;; (treesit-available-p))
:config
(setq treesit-font-lock-level 4)
;;:init
(setq treesit-language-source-alist
'((bash . ("https://github.com/tree-sitter/tree-sitter-bash" "v0.20.0"))
(c . ("https://github.com/tree-sitter/tree-sitter-c" "v0.21.3"))
(cpp . ("https://github.com/tree-sitter/tree-sitter-cpp"))
(python . ("https://github.com/tree-sitter/tree-sitter-python"))
(rust . ("https://github.com/tree-sitter/tree-sitter-rust"))
(toml . ("https://github.com/tree-sitter/tree-sitter-toml"))
(elisp . ("https://github.com/Wilfred/tree-sitter-elisp"))
(cmake . ("https://github.com/uyha/tree-sitter-cmake"))
(dockerfile . ("https://github.com/camdencheek/tree-sitter-dockerfile"))
(make . ("https://github.com/alemuller/tree-sitter-make"))
(yaml . ("https://github.com/ikatyang/tree-sitter-yaml"))
(json . ("https://github.com/tree-sitter/tree-sitter-json"))
(java . ("https://github.com/tree-sitter/tree-sitter-java"))
(html . ("https://github.com/tree-sitter/tree-sitter-html"))
(javascript . ("https://github.com/tree-sitter/tree-sitter-javascript"))
(css . ("https://github.com/tree-sitter/tree-sitter-css"))
(go . ("https://github.com/tree-sitter/tree-sitter-go"))
))

;;没有内置<languate>-mode的需要添加
(add-to-list 'auto-mode-alist '("\\.c\\.[^/]*\\'" . c-ts-mode))
(add-to-list 'auto-mode-alist '("\\.include\\.[^/]*\\'" . c-ts-mode))
(add-to-list 'auto-mode-alist '("\\.y[a]?ml\\'" . yaml-ts-mode))
(add-to-list 'auto-mode-alist '("\\(?:Dockerfile\\(?:\\..*\\)?\\|\\.[Dd]ockerfile\\)\\'" . dockerfile-ts-mode))
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-ts-mode))
(add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode))
(add-to-list 'auto-mode-alist '("\\.js\\'" . js-ts-mode))

)

;; 安装 tree-sitter 语法解析器(仅 Emacs 29+)
;; (run-with-idle-timer 3 nil ; 启动后3秒空闲时执行
;; (lambda ()
;; (dolist (lang '(bash c cpp python rust toml elisp cmake dockerfile make yaml json java html javascript css go))
;; (unless (treesit-language-available-p lang)
;; (treesit-install-language-grammar lang)))))

;; 优先使用 Tree-sitter mode
(setq major-mode-remap-alist
'((bash-mode . bash-ts-mode)
(c-mode . c-ts-mode)
(c++-mode . c++-ts-mode)
(python-mode . python-ts-mode)
;;(rust-mode . rust-ts-mode)
(conf-toml-mode . toml-ts-mode)
(elisp-mode . elisp-ts-mode)
(cmake-mode . cmake-ts-mode)
;;(dockerfile-mode . dockerfile-ts-mode)
(json-mode . json-ts-mode)
(java-mode . java-ts-mode)
;;(yaml-mode . yaml-ts-mode)
(makefile-mode . makefile-ts-mode)

(mhtml-mode . html-ts-mode)
(html-mode . html-ts-mode)
;;(js-mode . js-ts-mode)
(css-mode . css-ts-mode)
;;(go-mode . go-ts-mode)
))

;; 设置 C/C++ 模式的缩进为 8 个空格
(defun my-c-cpp-indent-setup ()
"Customize C/C++ mode."
(setq c-ts-mode-indent-offset 8) ;; 缩进单位为 8
(setq c-ts-mode-indent-style 'linux))

(dolist (hook '(c-mode-hook
c++-mode-hook
c-ts-mode-hook
c++-ts-mode-hook))
(add-hook hook #'my-c-cpp-indent-setup))



;; lsp语言服务器配置
(use-package eglot
:ensure t
:defer 1
:config
(setq eglot-server-programs
'(((c-mode-hook c++-mode-hook c++-ts-mode c-ts-mode)
. ("clangd"
;; "-j=4"
"--enable-config"
"--background-index"
;; "--background-index-priority=low"
"--clang-tidy"
"--completion-style=detailed"
"--header-insertion=never"
;; "--all-scopes-completion"
"--pretty"
;; "--fallback-style=none"
;;"--query-driver=aarch64-linux-gnu-*"
;;"--query-driver=gcc,g++"
))
((dts-mode) . ("devicetree-language-server" "--stdio"))
((rust-mode rust-ts-mode) . ("rust-analyzer" :initializationOptions
(:cargo (:buildScripts (:enable t))
:procMacro (:enable t))))

((python-mode python-ts-mode) . ("pyright-langserver" "--stdio"))

;;((bash-mode bash-ts-mode sh-mode sh-ts-mode) . ("bash-language-server" "start"))
;; ((toml-mode toml-ts-mode) . ("taplo" "lsp" "stdio"))
;; ((yaml-mode yaml-ts-mode) . ("yaml-language-server" "--stdio"))
;; ((markdown-mode markdown-ts-mode) . ("marksman"))

))

;; 关闭lsp自动格式化
(setq eglot-ignored-server-capabilities '(:documentOnTypeFormattingProvider))
)

;;自动打开eglot
(dolist (hook '(c-mode-hook c-ts-mode-hook c++-mode-hook c++-ts-mode-hook
dts-mode-hook
rust-mode-hook rust-ts-mode-hook
python-mode-hook python-ts-mode-hook
;;bash-mode-hook bash-ts-mode-hook
;;sh-mode-hook sh-ts-mode-hook
;; markdown-mode-hook markdown-ts-mode-hook
;; toml-mode-hook toml-ts-mode-hook
;; yaml-mode-hook yaml-ts-mode-hook
))
(add-hook hook 'eglot-ensure))


;; eglot快捷键
(global-set-key (kbd "M-,") 'xref-find-definitions) ;; 跳转到定义
(global-set-key (kbd "M-.") 'xref-pop-marker-stack) ;; 跳回
(global-set-key (kbd "M-?") 'xref-find-references) ;; 查找引用

;; comment
(defun my-cpp-line-comments ()
"Use // comments instead of /* */ in C/C++."
(setq-local comment-start "// "
comment-end ""
comment-start-skip "//+\\s-*"))

(dolist (hook '(c-mode-hook c++-mode-hook c-ts-mode-hook c++-ts-mode-hook
rust-mode-hook rust-ts-mode-hook python-mode-hook python-ts-mode-hook java-mode-hook java-ts-mode-hook))
(add-hook hook #'my-cpp-line-comments))

(use-package yasnippet
:ensure t
:hook (after-init . yas-global-mode)
:config
;; 可选:设置 snippet 目录(默认已包含 ~/.emacs.d/snippets)
;; (setq yas-snippet-dirs '("~/.emacs.d/snippets")))
)
;; 自动补全
(use-package company
:defer 1
:ensure t
:hook (after-init . global-company-mode)
:config
;; 光标停留 0 秒立即弹出补全
(setq company-idle-delay 0)
;; 后端
(setq company-backends '((company-capf :with company-yasnippet)
company-keywords
company-files))
;; 最少 1 个字符触发补全,0 表示空白处也可以触发
(setq company-minimum-prefix-length 1)
;; 补全候选最多显示 30 个
(setq company-tooltip-limit 30)
(setq eldoc-idle-delay 0.3)
)


;; GDB配置
(add-hook 'gud-mode-hook (lambda () (company-mode -1))) ;; gud模式下禁用 company

;; 格式化
(use-package format-all
:ensure t
:defer t
:commands format-all-mode
;; :hook (prog-mode . format-all-mode) ;; 禁用自动保存格式化
:config
(setq-default format-all-formatters
'(("C" (clang-format))
("C++" (clang-format))
;; ("Java" (clang-format))
("Rust" (rustfmt))
("JavaScript" (prettier))
("Markdown" (prettier))
("YAML" (prettier))
("TOML" (taplo-fmt))
("Devicetree" (dtsfmt))
)))

(global-set-key (kbd "C-x f") 'format-all-buffer)

;; which-key 显示键绑定提示
(use-package which-key
:ensure t
:defer t
:config
(which-key-mode))

;; 窗格切换
(use-package ace-window
:ensure t
:defer t
:bind (("M-o" . ace-window)))

;; 窗格移动
(use-package buffer-move
:ensure t
:defer t
:bind (("C-c <up>" . buf-move-up)
("C-c <down>" . buf-move-down)
("C-c <left>" . buf-move-left)
("C-c <right>" . buf-move-right)))

;; 智能按键C-a C-c k
(use-package crux
:ensure t
:defer t
:bind (("C-a" . 'crux-move-beginning-of-line)
("C-c k" . 'crux-smart-kill-line)))

;; 预览要跳转的行
(use-package goto-line-preview
:ensure t
:defer t
:bind (("M-g g" . goto-line-preview)))


;;代码整行上移下移
(use-package move-dup
:ensure t
:defer t
:bind (("C-M-p" . move-dup-move-lines-up)
;; ("C-M-p" . move-dup-duplicate-up)
("C-M-n" . move-dup-move-lines-down)
;; ("C-M-n" . move-dup-duplicate-down)
))

;; 项目识别,通过创建.projectile文件识别为一个项目
(setq project-find-functions nil)

(use-package projectile
:ensure t
;;:init
;; :bind-keymap
;; ("C-c p" . projectile-command-map)
:config
(projectile-mode +1)
(setq projectile-enable-caching t)
(setq projectile-project-search-path '("~/repository"))
(setq projectile-completion-system 'auto) ;; 或 ivy / helm / default

(use-package rg
:defer t
:ensure t))

(use-package multiple-cursors
:ensure t
:config
(global-set-key (kbd "C-c m") 'mc/mark-all-dwim))

;; 快捷键定义必须放在后面否则容易被其他插件覆盖

;; next error
(global-set-key (kbd "M-n") #'flymake-goto-next-error)
(global-set-key (kbd "M-p") #'flymake-goto-prev-error)

;; 快捷键绑定:使用调整窗口大小
(global-set-key (kbd "M-<up>") 'enlarge-window)
(global-set-key (kbd "M-<down>") 'shrink-window)
(global-set-key (kbd "M-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "M-<right>") 'enlarge-window-horizontally)

;;撤销
(global-set-key (kbd "C-/") 'undo-only)

;;全局搜索
(global-set-key (kbd "C-c C-s") 'projectile-ripgrep)

(defun k-insert-timestamp ()
(interactive)
(insert (format-time-string "%Y-%m-%d %H:%M")))

(defun k-insert-date ()
(interactive)
(insert (format-time-string "%Y-%m-%d")))

(global-set-key (kbd "C-c t") 'k-insert-timestamp)
(global-set-key (kbd "C-c d") 'k-insert-date)

(global-unset-key (kbd "C-x C-b"))
(global-set-key (kbd "C-x C-b") 'ibuffer)

(global-set-key (kbd "TAB") 'tab-to-tab-stop)

(global-set-key (kbd "C-x k")
(lambda ()
(interactive)
(kill-this-buffer)))

(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages nil))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)

.clang-format 配置

直接用的 Linux Kernel 的配置,但没有使用TAB,而是空格,ColumnLimit调大为100

.clang-format
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# SPDX-License-Identifier: GPL-2.0
#
# clang-format configuration file. Intended for clang-format >= 11.
#
# For more information, see:
#
# Documentation/dev-tools/clang-format.rst
# https://clang.llvm.org/docs/ClangFormat.html
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 100
CommentPragmas: "^ IWYU pragma:"
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
IncludeBlocks: Preserve
IncludeCategories:
- Regex: ".*"
Priority: 1
IncludeIsMainRegex: "(Test)?$"
IndentCaseLabels: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 8
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ""
MacroBlockEnd: ""
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true

# Taken from git's rules
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60

PointerAlignment: Right
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatementsExceptForEachMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 8
UseTab: false

快捷键配置总览

“C” 在 Emacs 中表示Ctrl

“M” 在 Emacs 中表示Alt

快捷键 功能描述 来源配置
C-/ 撤销(undo-only) 手动绑定
C-? 重做(undo-redo) 手动绑定
M-<up> 增大当前窗口高度 手动绑定
M-<down> 减小当前窗口高度 手动绑定
M-<left> 减小当前窗口宽度 手动绑定
M-<right> 增大当前窗口宽度 手动绑定
C-<left> 切换到前一个 Tab(Centaur Tabs) centaur-tabs
C-<right> 切换到下一个 Tab centaur-tabs
M-g g 预览跳转到指定行(goto-line-preview) goto-line-preview
M-C-p 将当前行上移 move-dup
M-C-n 将当前行下移 move-dup
M-p 下一个错误 flymake
M-n 上一个错误 flymake
f8 打开/关闭文件树侧边栏(neotree) neotree
C-x g magit status magit
C-x f 格式化当前 buffer(clang-format / rustfmt) format-all
M-, 跳转到定义(xref-find-definitions,需 LSP 支持) 手动绑定
M-. 跳回上一个位置(xref-pop-marker-stack) 手动绑定
M-? 查找引用(xref-find-references,需 LSP 支持) 手动绑定
M-o 在多个窗口间切换焦点 ace-window
C-c <up> 当前 buffer 上移到另一个窗口 buffer-move
C-c <down> 当前 buffer 下移到另一个窗口 buffer-move
C-c <left> 当前 buffer 左移到另一个窗口 buffer-move
C-c <right> 当前 buffer 右移到另一个窗口 buffer-move
C-a 智能移动光标到行首(再次按可回到代码开始处) crux
C-c k 智能删除一行(不会破坏缩进) crux
C-c p Projectile 项目命令前缀 projectile
C-x SPC 矩形选择模式,可以选择某些列 内置
M-< 移到缓冲区开头 内置
M-> 移到缓冲区末尾 内置
C-c t 插入时间戳 手动绑定
C-c d 插入日期 手动绑定
C-x u 打开undo-tree undo-tree
  • M-X projectile-ripgrep ripgrep全局查找

  • C-c C-s 项目里全局查找

  • neotree

    • M-x neotree-create-node 创建文件,也可以用C-c n
    • M-x neotree-delete-node 删除文件
    • M-x neotree-rename-node 重命名文件
  • M-x recenter 把当前行放在窗口中间

  • M-x eglot-code-actions对于 lsp 提示 fix available 的可以让 lsp 修改

  • M-x man, 然后输入要查找的函数比如shmget(2),这里2表示查找syscall,然后在 *Man* 缓冲区中用:

    • M-n → 下一个手册页
    • M-p → 上一个手册页

buffer-menu

你可以用以下按键操作:

按键 功能
Enterf 切换到光标所在行的缓冲区
o 切换到该缓冲区,并保留 *Buffer List*
d 标记该缓冲区为“待删除”(不会立即删)
x 真正删除所有被标记的缓冲区
s 保存该缓冲区(如果已修改)
u 取消标记(如果之前按了 d
q 关闭 *Buffer List* 窗口
g 刷新列表(重新加载最新状态)

按d标记删除后,按x才是真正删除

magit

Magit 的设计哲学是:一个按键 = 一个操作

按键 功能
s Stage 当前光标下的文件/区域(相当于 git add
u Unstage(取消暂存)
c Commit(提交) → 再按 c 确认
P Push 推送到远程
F Fetch 从远程拉取更新
p Pull(拉取并合并)
b 分支操作(创建/切换/合并)
l 查看 log(提交历史)
L 查看带图形的 log(类似 git log --graph
d Diff 查看变更内容
= 在 commit 上按 = 查看该次提交的 diff
q Quit 关闭当前 Magit 缓冲区

⚠️ 注意:这些命令只在 Magit 状态缓冲区(*magit: xxx*)中有效

使用一个按键后,按ctlr-g取消

编程设置

对于有 makefile 的 project 可以用 bear 生成 compile_command.json 供 clangd 索引

1
2
3
4
5
6
# 在项目根目录下创建,标识这是一个项目
touch .projectile

# 生成compile_command.json
make clean
bear -- make

对于linux内核相关设置,参考

以守护进程形式运行Emacs

systemd

需要systemd

1
2
systemctl --user enable --now emacs
emacsclient file.txt

Shell

1
2
emacs --daemon
emacsclient file.txt

完成编辑后,按 Ctrl+x,然后 #退出,Emacs 会将该缓冲区标记为已完成,然后emacsclient 会退出。如果没有其他缓冲区在使用,因该buffer启动LSP(例如 clangd)也会随之退出。

而如果按Ctrl+x退出,然后 c,Emacs会认为这个buffer暂时还不能关闭