Emacs手札

2024-08-03

最近把主力編輯器從VSCode切換到了Emacs,這裏給配置過程做一下記錄。

基本設置

讓Emacs把臨時文件都放到/tmp中,以免污染當前目錄:

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))

設置字體:

(set-frame-font "monospace 13" nil t)
(set-fontset-font t 'han "Source Han Sans CN")

開啓行號顯示:

(global-display-line-numbers-mode 1)

標題欄和工具欄大多數情況下沒有用,反而會佔屏幕空間,因此這裏把它們隱藏,如果需要用到的話可以用F10呼出。

(menu-bar-mode -1) 
(toggle-scroll-bar -1) 
(tool-bar-mode -1)

包管理

把Melpa加入Emacs:

(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)

啓用包管理器:

(require 'package)
(package-initialize)

隨後重啓Emacs,更新包目錄:M-x package-refresh-contents

使用M-x package-install依次安裝下列功能包:

  • magit
  • lsp-mode
  • rust-mode
  • dired-sidebar
  • treesit-auto
  • use-package
  • yasnippet
  • rg
  • counsel
  • ivy
  • lsp-ivy
  • projectile
  • company

如果要更新所有安裝的包:M-x list-package RET S-u x

Treesit

啓用內置的treesit然後利用treesit-auto自動配置:

(require 'treesit)

(use-package treesit-auto
  :config
  (global-treesit-auto-mode))

然後安裝語法庫:M-x treesit-auto-install-all

Typst

typst模式這個包不在Melpa中,需要從git安裝:M-x package-vc-install。地址爲:

https://git.sr.ht/~meow_king/typst-ts-mode

然後編譯安裝typst的treesit庫:先輸入M-: (treesit-install-language-grammar 'typst) RET,然後輸入https://github.com/uben0/tree-sitter-typst

配置:

(use-package typst-ts-mode
  :custom
  (typst-ts-mode-grammar-location (expand-file-name "tree-sitter/libtree-sitter-typst.so" user-emacs-directory)))

Ivy

Ivy是一個minibuffer的補全工具,也有人用helm,不過我覺得ivy更輕更簡潔一點。

(require 'ivy)
(require 'counsel)
(ivy-mode)
(setq ivy-use-virtual-buffers t)
(setq enable-recursive-minibuffers t)
(global-set-key "\C-s" 'swiper)
(global-set-key (kbd "C-c C-r") 'ivy-resume)
(global-set-key (kbd "<f6>") 'ivy-resume)
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "<f1> f") 'counsel-describe-function)
(global-set-key (kbd "<f1> v") 'counsel-describe-variable)
(global-set-key (kbd "<f1> o") 'counsel-describe-symbol)
(global-set-key (kbd "<f1> l") 'counsel-find-library)
(global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
(global-set-key (kbd "<f2> u") 'counsel-unicode-char)
(global-set-key (kbd "C-c g") 'counsel-git)
(global-set-key (kbd "C-c j") 'counsel-git-grep)
(global-set-key (kbd "C-c k") 'counsel-ag)
(global-set-key (kbd "C-x l") 'counsel-locate)
(global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
(define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history)

LSP

這裏先只安裝Rust的lsp功能:

(use-package company)
(use-package rust-mode)
(use-package lsp-mode
  :init
  ;; set prefix for lsp-command-keymap (few alternatives - "C-l", "C-c l")
  (setq lsp-keymap-prefix "C-c l")
  :hook (
         (rust-mode . lsp)
         ;; if you want which-key integration
         (lsp-mode . lsp-enable-which-key-integration))
  :commands lsp)
(use-package lsp-ivy :commands lsp-ivy-workspace-symbol)

文件瀏覽器側欄

使用dired-sidebar

(use-package dired-sidebar
  :bind (("C-x C-n" . dired-sidebar-toggle-sidebar))
  :ensure t
  :commands (dired-sidebar-toggle-sidebar)
  :init
  (add-hook 'dired-sidebar-mode-hook
            (lambda ()
              (unless (file-remote-p default-directory)
                (auto-revert-mode))))
  :config
  (push 'toggle-window-split dired-sidebar-toggle-hidden-commands)
  (push 'rotate-windows dired-sidebar-toggle-hidden-commands)

  (setq dired-sidebar-subtree-line-prefix "__")
  (setq dired-sidebar-theme 'vscode)
  (setq dired-sidebar-use-term-integration t)
  (setq dired-sidebar-use-custom-font t))

然後使用C-x C-n可以呼出側欄。

Projectile

使用C-c p快速在項目中跳轉到文件,類似於Vim的CtrlP:

(projectile-mode +1)
;; Recommended keymap prefix on Windows/Linux
(define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)

Snippet

使用yasnippet插件:

(use-package yasnippet
  :bind
  (("C-c n i" . yas-insert-snippet))  
  :config
  (add-to-list 'yas-snippet-dirs "~/.emacs.d/snippets")
  (yas-global-mode 1))

Snippet保存在~/.emacs.d/snippets/{模式名}/目錄下。

Ripgrep

使用rg插件可以快速在項目目錄中搜索文本內容:

(require 'rg)
(rg-enable-default-bindings)

最常用的快捷鍵是C-c p f,可以在Projectile項目中搜索。

Magit

Magit插件功能比較繁雜,這裏不再贅述了,可以去看magit網站上的教程。

啓用:

(require 'magit)

Markdown

直接加載Markdown模式即可:

(use-package markdown-mode)


Email: i (at) mistivia (dot) com