../

Emacs Notes

2024-08-03

I recently switched my main editor from VSCode to Emacs, so I am recording the configuration process here.

Basic Settings

Force Emacs to put temporary files in /tmp to avoid polluting the current directory:

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

Set fonts:

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

Enable line number display:

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

The menu bar and tool bar are useless in most cases and just take up screen space, so I hide them here. If needed, they can be recalled using F10.

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

Package Management

Add Melpa to Emacs:

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

Enable the package manager:

(require 'package)
(package-initialize)

Then restart Emacs and update the package list: M-x package-refresh-contents.

Use M-x package-install to install the following feature packages one by one:

To update all installed packages: M-x list-package RET S-u x

Treesit

Enable the built-in treesit and then use treesit-auto for automatic configuration:

(require 'treesit)

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

Then install the grammar libraries: M-x treesit-auto-install-all

Typst

The typst-mode package is not in Melpa and needs to be installed from git: M-x package-vc-install. The URL is:

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

Then compile and install the treesit library for typst: first enter

M-: (treesit-install-language-grammar 'typst) RET

Then enter

https://github.com/uben0/tree-sitter-typst`。

Configuration:

(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 is a minibuffer completion tool. Some people also use helm, but I feel ivy is lighter and cleaner.

(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

Here I will only install the LSP functionality for Rust for now:

(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)

File Browser Sidebar

Use 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))

You can then use C-x C-n to toggle the sidebar.

Projectile

Use C-c p to quickly jump to files within a project, similar to CtrlP in Vim:

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

Snippet

Use the yasnippet plugin:

(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))

Snippets are saved in the ~/.emacs.d/snippets/{mode-name}/ directory.

Ripgrep

Use the rg plugin to quickly search text content within the project directory:

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

The most commonly used shortcut is C-c p f, which allows searching within a Projectile project.

Magit

The functions of the Magit plugin are quite complex, so I won’t go into detail here; you can check the tutorials on the Magit website.

Enable:

(require 'magit)

Markdown

Simply load the Markdown mode:

(use-package markdown-mode)

Mistivia - https://mistivia.com