LoginSignup
7
5

備忘録 (init.el -> org-babel)

Last updated at Posted at 2022-09-04

はじめに

こんにちは。Emacsを使い続けて3年ほどになる初心者です。
これまで備忘録(init.el)のように early-init.elinit.el だけで管理していましたが、 こちらの記事を読んで、org-modeで管理したいと思い一新しました。org-modeで管理したい方の参考になれば幸いです。

設定の方針

org-mode で管理する基本的な流れは下記のようになります。

  • xxx.org のorg-babelに設定を記述
  • org-babel-tangle-file または org-babel-load-filexxx.el の生成
  • 生成した xxx.el を読み込む

私は .emacs.d 直下に config.orgearly-init.org を作成し、設定を記述し、 init.el のコンパイル時に org-babel-tangle-file を実行&コンパイルし、 xxx.elc を読み込むようにしています。
ただ、 .emacs.d 直下だと警告が出るので、気になる方は .emacs.d/lispに作成してください。

以降に実際の init.el の内容と、 early-init.org config.org をMarkdownで出力したものを載せています。

init.el

※最新の org を利用していますが、うまく行かない場合は init.el(straight-use-package 'org) を削除して、Emacs標準の org を利用してください。

init.el
;;; package --- Summary
;;; Commentary:
;;; Code:
    
;; パッケージマネージャーのインストール
(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

;; 最新のorgをインストール
(straight-use-package 'org)

;; ファイルの生成・読み込み(elcでは実行されない)
(eval-when-compile
  (require 'org)
  (org-babel-tangle-file (expand-file-name "early-init.org" user-emacs-directory))
  (org-babel-tangle-file (expand-file-name "config.org" user-emacs-directory))
  (byte-compile-file (expand-file-name "early-init.el" user-emacs-directory))
  (byte-compile-file (expand-file-name "config.el" user-emacs-directory)))

;; すぐバイトコンパイルできるように定義
(defun byte-compile-init-file ()
  (interactive)
  (byte-compile-file (expand-file-name "init.el" user-emacs-directory)))

;; 設定の読み込み
(add-to-list 'load-path (expand-file-name user-emacs-directory))
(require 'config)

(provide 'init)
;;; init.el ends here

(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)

(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.
 '(warning-suppress-types '((initialization) (comp))))
(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.
 )

orgファイルの書き方

Emacsの設定はbabelのコードブロック内に記述し、その前後に説明やリンクを記述していきます。
babelの記述はスニペットを利用すると快適に記述でき、companycompany-tabnineyasnippetyasnippet-snippetsのように設定することで利用できます。
使用方法は org上で、 e を入力して elisp スニペットを選択すると展開できます。

※ コードブロックで :tangle no を指定するとファイルに出力されず、設定に反映されません。

(例)

config.org
#+TITLE: config.el
#+AUTHOR: hoge hoge

* config.el
** header

#+begin_src emacs-lisp :tangle yes
  
  ;;; package --- Summary
  ;;; Commentary:
  ;;; Code:

#+end_src

** OS判定用定数

#+begin_src emacs-lisp :tangle yes
  
(defconst IS-MAC (eq system-type 'darwin))
(defconst IS-LINUX (memq system-type '(gnu gnu/linux gnu/kfreebsd berkeley-unix)))
(defconst IS-WINDOWS (memq system-type '(cygwin windows-nt ms-dos)))

#+end_src

** straight

Emacs標準の ~package.el+quelpa~ や ~el-get~ 等を色々試した結果、
たどり着いたパッケージマネージャー。
設定は ~init.el~ に記述。

| コマンド                  | 動作                                                                  |
|--------------------------+----------------------------------------------------------------------|
| straight-use-package     | パッケージのインストール                                                |
| straight-pull-all        | 全てのパッケージを最新の状態に更新                                        |
| straight-freeze-versions | ロックファイルを作成                                                    |
| straight-thaw-versions   | ロックファイルのバージョンでインストールックファイルのバージョンでインストール |

** use-package

#+begin_src emacs-lisp :tangle yes
  
(straight-use-package 'use-package)

#+end_src

** bind-key

#+begin_src emacs-lisp :tangle yes
  
(use-package bind-key
  :straight t)

#+end_src

** IME

Emacsは~C-\~で日本語入力を切り替えることができますが、
デフォルトだとあまり補完が賢くないのでOSに合わせて導入します。

*** [[https://github.com/trueroad/tr-emacs-ime-module][tr-ime]]

#+begin_src emacs-lisp :tangle yes
  
(use-package tr-ime
  :straight t
  :if IS-WINDOWS
  :custom (default-input-method "W32-IME")
  :init
  (tr-ime-standard-install)
  (w32-ime-initialize))

#+end_src

*** mozc

[[https://www.kkaneko.jp/tools/server/mozc.html][日本語変換 Mozc の設定,emacs 用の Mozc の設定(Ubuntu 上)]] を参考にしています。

**** 必要なパッケージを導入

#+begin_src shell :tangle no
  
  sudo apt install fcitx-libs-dev
  sudo apt install emacs-mozc
  fcitx-config-gtk

#+end_src

**** Emacs側の設定

#+begin_src emacs-lisp :tangle yes
  
(use-package mozc
  :straight t
  :if IS-LINUX
  :custom (default-input-method "japanese-mozc"))

#+end_src

下記に私の設定を ox-qmd を使用して、orgファイルからMarkdownに変換して貼り付けています。

early-init.el

header

;;; package --- Summary
;;; Commentary:
;;; Code:

package.elを起動しない


(setq package-enable-at-startup nil)

画面最大化


(push '(fullscreen . maximized) default-frame-alist)

スクロール非表示


(push '(vertical-scroll-bars) default-frame-alist)

メニューバー非表示


(push '(menu-bar-lines . 0) default-frame-alist)

ツールバー非表示


(push '(tool-bar-lines . 0) default-frame-alist)

footer


(provide 'early-init)
  ;;; early-init.el ends here

config.el

header


;;; package --- Summary
;;; Commentary:
;;; Code:

OS判定用定数


(defconst IS-MAC (eq system-type 'darwin))
(defconst IS-LINUX (memq system-type '(gnu gnu/linux gnu/kfreebsd berkeley-unix)))
(defconst IS-WINDOWS (memq system-type '(cygwin windows-nt ms-dos)))

straight

Emacs標準の package.el+quelpael-get 等を色々試した結果、たどり着いたパッケージマネージャー。設定は init.el に記述。

コマンド 動作
straight-use-package パッケージのインストール
straight-pull-all 全てのパッケージを最新の状態に更新
straight-freeze-versions ロックファイルを作成
straight-thaw-versions ロックファイルのバージョンでインストールックファイルのバージョンでインストール

use-package


(straight-use-package 'use-package)

bind-key


(use-package bind-key
  :straight t)

general


(use-package general
  :straight t
  :custom (general-override-states '(insert emacs hybrid normal visual motion operator replace))
  :config
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC" :prefix-command 'my-space-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC s" :prefix-command 'search-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC g" :prefix-command 'goto-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC o" :prefix-command 'my-org-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC f" :prefix-command 'my-file-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC b" :prefix-command 'my-buffer-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC e" :prefix-command 'my-error-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC q" :prefix-command 'my-quit-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC t" :prefix-command 'my-toggle-map)
  (general-def :states '(normal visual) :keymaps 'override :prefix "SPC c" :prefix-command 'my-string-inflection-map))

org

org-mode に関する基本的な設定をしています。


(use-package org
  :demand t
  :custom ((org-startup-folded 'content)
           (org-fold-core-style 'overlays))
  :bind (nil
         :map my-org-map
         ("o" . org-open-at-point)
         ("l" . org-link)
         ("e" . org-edit-special)))

org-agenda

org-agenda のキーバインドを設定しています。また、同じ内容が2行表示されることがあったので、フックを追加しています。


(use-package org-agenda
  :custom (org-agenda-files (directory-files org-directory))
  :bind (nil
         :map my-org-map
         ("a" . org-agenda)))

org-pomodoro

カーソル上のタスクでポモドーロタイマーを設定してくれます。


(use-package org-pomodoro
  :straight t
  :bind (nil
         :map my-org-map
         ("p" . org-pomodoro)))

org-rainbow-tags


(use-package org-rainbow-tags
  :straight (:host github :repo "KaratasFurkan/org-rainbow-tags")
  :custom (org-rainbow-tags-extra-face-attributes '(:inverse-video t :box t :weight 'bold))
  :hook (org-mode . org-rainbow-tags-mode))

ox-qmd (qiita投稿用)


(use-package ox-qmd
  :straight t)

denote


(use-package denote
  :straight t
  :demand t
  :custom ((denote-directory org-directory)
           (denote-infer-keywords t)
           (denote-sort-keywords t)
           (denote-file-type nil)
           (denote-prompts '(title keywords))
           (denote-date-prompt-use-org-read-date t)
           (denote-allow-multi-word-keywords t)
           (denote-date-format nil)
           (denote-link-fontify-backlinks t))
  :bind (nil
         :map my-space-map
         ("d" . denote))
  :hook (dired-mode . denote-dired-mode-in-directories))

  1. consult-notes

    
    (use-package consult-notes
      :straight t
      :custom (consult-notes-sources `(("Notes" ?n ,denote-directory)))
      :bind (nil
             :map my-space-map
             ("n" . consult-notes)))
    
    

Emacs標準機能の設定

最適化


(when IS-WINDOWS
  (setq w32-get-true-file-attributes nil
        w32-pipe-read-delay 0
        w32-pipe-buffer-size (* 64 1024)))

(unless IS-MAC
  (setq command-line-ns-option-alist nil))

(unless IS-LINUX
  (setq command-line-x-option-alist nil))

(setq read-process-output-max #x10000)

(setq ffap-machine-p-known 'reject)

画面の更新頻度


(use-package simple
  :custom (idle-update-delay 1.0))

補完時に大文字小文字を区別しない


(setq read-file-name-completion-ignore-case t
      read-buffer-completion-ignore-case t
      completion-ignore-case t)

yes-or-noをy-or-nに変更


(setq use-short-answers t)

自動生成ファイルを無効にする


(use-package files
  :custom ((make-backup-files nil)
           (auto-save-default nil)))

デーモン起動

emacsclient コマンドで高速にファイルが開けます。


(use-package server
  :init
  (server-mode +1))

最後のカーソル位置を保存する


(use-package saveplace
  :init
  (save-place-mode +1))

ファイルの閲覧履歴を保存する


(use-package recentf
  :custom (recentf-max-saved-items 200)
  :init
  (recentf-mode +1))

コマンドの履歴を保存


(use-package savehist
  :init
  (savehist-mode +1))

対応括弧を強調表示


(use-package paren
  :init
  (show-paren-mode +1))

括弧の補完


(use-package elec-pair
  :init
  (electric-pair-mode +1))

現在行を強調表示


(use-package hl-line
  :init
  (global-hl-line-mode +1))

他プロセスの編集をバッファに反映


(use-package autorevert
  :init
  (global-auto-revert-mode +1))

行番号の表示


(use-package display-line-numbers
  :custom (display-line-numbers-width 4)
  :init
  (global-display-line-numbers-mode +1))

外枠の調整


(setq indicate-buffer-boundaries nil
      indicate-empty-lines nil)


(use-package frame
  :custom ((window-divider-default-places t)
           (window-divider-default-bottom-width 1)
           (window-divider-default-right-width 1))
  :init
  (window-divider-mode +1))

水平分割より垂直分割を優先する


(use-package window
  :custom ((split-width-threshold 160)
           (split-height-threshold nil)))

カーソル上の関数名等をモードラインに表示


(use-package which-func
  :init
  (which-function-mode +1))

メニューバーを無効化


(use-package menu-bar
  :init
  (menu-bar-mode -1))

ツールバーを無効化


(use-package tool-bar
  :init
  (tool-bar-mode -1))

スクロールバーの無効化


(use-package scroll-bar
  :init
  (scroll-bar-mode -1))

便利コマンドをEvil向けに登録


(use-package simple
  :bind (nil
         :map my-space-map
         ("SPC" . execute-extended-command)))

(use-package files
  :bind (nil
         :map my-quit-map
         ("q" . save-buffers-kill-terminal)
         :map my-file-map
         ("f" . find-file)))

(use-package bookmark
  :bind (nil
         :map my-file-map
         ("b" . bookmark-jump)))

(use-package window
  :bind (nil
         :map my-space-map
         ("0" . delete-window)
         ("1" . delete-other-windows)
         ("2" . split-window-below)
         ("3" . split-window-right)
         ("4" . switch-to-buffer-other-window)
         ("w" . other-window)
         :map my-buffer-map
         ("b" . switch-to-buffer)))

(use-package frame
  :bind (("<f12>" . other-frame)
         :map my-space-map
         ("W" . other-frame)))

(use-package subr
  :bind (nil
         :map my-space-map
         ("5" . ctl-x-5-prefix)))

(use-package menu-bar
  :bind (nil
         :map my-buffer-map
         ("d" . kill-this-buffer)))

(use-package project
  :bind (nil
         :map my-buffer-map
         ("p" . project-switch-to-buffer)))

文字コード


(use-package mule
  :init
  (set-language-environment "Japanese")
  (prefer-coding-system 'utf-8))

(use-package mule
  :if IS-WINDOWS
  :init
  (set-file-name-coding-system 'cp932)
  (set-keyboard-coding-system 'cp932)
  (set-terminal-coding-system 'cp932))

shift-jisよりcp932を優先


(when IS-WINDOWS
  (set-coding-system-priority 'utf-8
                              'euc-jp
                              'iso-2022-jp
                              'cp932))

whitespace

末尾のスペースやタブを可視化することができます。highlight-indent-guides と相性が悪いのでタブは可視化していません。


(use-package whitespace
  :custom (whitespace-style '(face trailing))
  :init
  (global-whitespace-mode +1))

IME

Emacsは~C-\~で日本語入力を切り替えることができますが、デフォルトだとあまり補完が賢くないのでOSに合わせて導入します。

tr-ime


(use-package tr-ime
  :straight t
  :if IS-WINDOWS
  :custom (default-input-method "W32-IME")
  :init
  (tr-ime-standard-install)
  (w32-ime-initialize))

mozc

日本語変換 Mozc の設定,emacs 用の Mozc の設定(Ubuntu 上) を参考にしています。

  1. 必要なパッケージを導入

    
    sudo apt install fcitx-libs-dev
    sudo apt install emacs-mozc
    fcitx-config-gtk
    
    
  2. Emacs側の設定

    
    (use-package mozc
      :straight t
      :if IS-LINUX
      :custom (default-input-method "japanese-mozc"))
    
    

フォントの設定

私は fontaine を使用してフォントを設定しています。


(use-package fontaine
  :straight t
  :demand t
  :hook (kill-emacs . fontaine-store-latest-preset)
  :config
  (cond ((eq system-type 'gnu/linux)
         (setq fontaine-presets
               '((regular
                  :default-family "VLゴシック"
                  :default-height 100
                  :fixed-pitch-family "VLゴシック"
                  :variable-pitch-family "VLPゴシック"
                  :italic-family "VLゴシック"
                  :line-spacing 1)
                 (large
                  :default-family "VLゴシック"
                  :default-height 150
                  :variable-pitch-family "VLPゴシック"
                  :line-spacing 1))))

        ((eq system-type 'windows-nt)
         (setq fontaine-presets
               '((regular
                  :default-family "BIZ UDゴシック"
                  :default-height 120
                  :fixed-pitch-family "BIZ UDゴシック"
                  :variable-pitch-family "BIZ UDPゴシック"
                  :italic-family "BIZ UDゴシック"
                  :line-spacing 1)
                 (large
                  :default-family "BIZ UDゴシック"
                  :default-height 150
                  :variable-pitch-family "BIZ UDPゴシック"
                  :line-spacing 1)))))

  (fontaine-set-preset (or (fontaine-restore-latest-preset) 'regular)))

modeline

nyan-mode

バッファー上での位置をニャンキャットが教えてくれるパッケージです。マウスでクリックすると大体の位置にジャンプもできます。


(use-package nyan-mode
  :straight t
  :custom ((nyan-animate-nyancat t)
           (nyan-bar-length 24))
  :init
  (nyan-mode +1))

doom-modeline


(use-package doom-modeline
  :straight t
  :init
  (doom-modeline-mode +1))

evil

VimキーバインドをEmacs上で実現してくれるパッケージです。


(use-package evil
  :straight t
  :custom ((evil-want-keybinding nil)
           (evil-symbol-word-search t)
           (evil-kill-on-visual-paste nil))
  :init
  (evil-mode +1))

evil-collection

各モードのキーバインドを自動的に設定してくれます。


(use-package evil-collection
  :straight t
  :after evil
  :init
  (evil-collection-init))

evil-commentary

gc でコメントアウトしてくれるパッケージです。


(use-package evil-commentary
  :straight t
  :after evil
  :init
  (evil-commentary-mode +1))

evil-surround

選択中に S を入力して任意の文字を入力すると囲んでくれるパッケージです。

  • (例1) aaaを選択中に S(

aaa -> ( aaa )

  • (例2) aaaを選択中に S)

aaa -> (aaa)


(use-package evil-surround
  :straight t
  :after evil
  :init
  (global-evil-surround-mode +1))

evil-matchit

% でHTMLのタグ間をジャンプしてくれるようになります。


(use-package evil-matchit
  :straight t
  :after evil
  :init
  (global-evil-matchit-mode +1))

evil-org

org-agenda 等のorg系の特殊なモードでキーバインドを設定してくれます。


(use-package evil-org
  :straight t
  :after evil
  :hook (org-mode . evil-org-mode)
  :config
  (require 'evil-org-agenda)
  (evil-org-set-key-theme '(navigation insert textobjects additional calendar))
  (evil-org-agenda-set-keys))

evil-lion

gl gL で整列してくれます。


(use-package evil-lion
  :straight t
  :after evil
  :init
  (evil-lion-mode +1))

fussy


(use-package fussy
  :straight t
  :custom ((completion-styles '(fussy))
           (completion-category-defaults nil)
           (completion-category-overrides nil)
           (fussy-use-cache t)
           (fussy-filter-fn #'fussy-filter-default)
           (fussy-default-regex-fn #'fussy-pattern-flex-2))
  :config
  (with-eval-after-load 'fuz-bin
    (setq fussy-score-fn #'fussy-fuz-bin-score))

  (with-eval-after-load 'prescient
    (setq fussy-compare-same-score-fn #'fussy-strlen<))

  (with-eval-after-load 'company
    (defun j-company-capf (f &rest args)
      "Manage `completion-styles'."
      (let ((fussy-max-candidate-limit 5000)
            (fussy-default-regex-fn 'fussy-pattern-first-letter)
            (fussy-prefer-prefix nil))
        (apply f args)))
    (advice-add 'company-auto-begin :before #'fussy-wipe-cache)
    (advice-add 'company-capf :around 'j-company-capf)))

fuz-bin


(use-package fuz-bin
  :straight (fuz-bin :repo "jcs-elpa/fuz-bin" :fetcher github :files (:defaults "bin"))
  :config
  (fuz-bin-load-dyn))

prescient

prescient-persist-mode で履歴を永続的に保存


(use-package prescient
  :straight t
  :custom ((prescient-aggressive-file-save t)
           (prescient-use-case-folding t))
  :config
  (prescient-persist-mode +1)

  (with-eval-after-load 'fussy
    (setq prescient-sort-length-enable nil)))

company-prescient

prescient だけでは company の履歴が保存できないので、~company-prescient~ を導入します。


(use-package company-prescient
  :straight t
  :after company
  :custom (company-prescient-sort-length-enable nil)
  :init
  (company-prescient-mode +1))

vertico-prescient


(use-package vertico-prescient
  :straight t
  :custom ((vertico-prescient-enable-filtering nil)
           (vertico-prescient-override-sorting t))
  :init
  (vertico-prescient-mode +1))

company

入力補完用のパッケージです。


(use-package company
  :straight t
  :custom ((company-minimum-prefix-length 1)
           (company-idle-delay 0)
           (company-require-match nil)
           (company-dabbrev-other-buffers nil)
           (company-dabbrev-downcase nil)
           (company-dabbrev-ignore-case nil))
:bind (([remap indent-for-tab-command] . company-indent-or-complete-common)
       ([remap c-indent-line-or-region] . company-indent-or-complete-common))
  :init
  (global-company-mode +1))

company-posframe


(use-package company-posframe
  :straight t
  :after company
  :custom (company-tooltip-minimum-width 40)
  :init
  (company-posframe-mode +1)
  :config
  (with-eval-after-load 'desktop
    (push '(company-posframe-mode . nil) desktop-minor-mode-table)))

company-dwim

company の挙動を ac-dwim のように変えてくれるパッケージです。私は tng のような挙動にしたかったので、forkして一部修正して使っています。


(use-package company-dwim
  :straight (company-dwim :type git :host github :repo "nobuyuki86/company-dwim")
  :after company
  :demand t
  :custom (company-selection-default nil)
  :bind (([remap company-select-next] . company-dwim-select-next)
         ([remap company-select-previous] . company-dwim-select-previous)
         :map company-active-map
         ("RET" . company-dwim-complete-or-newline)
         ("<return>" . company-dwim-complete-or-newline)
         ("TAB" . company-dwim-select-next)
         ("<tab>" . company-dwim-select-next)
         ("S-TAB" . company-dwim-select-previous)
         ("<backtab>" . company-dwim-select-previous))
  :config
  (add-to-list 'company-frontends 'company-dwim-frontend t)
  (delq 'company-preview-if-just-one-frontend company-frontends))

company-anywhere

通常 company は途中から入力しても補完候補が表示されませんが、こちらのパッケージで補完候補が表示されるようになります。


(use-package company-anywhere
  :straight (company-anywhere :type git :host github :repo "zk-phi/company-anywhere")
  :after company)

company-tabnine

tabnine を利用できるようにするパッケージです。


(use-package company-tabnine
  :straight (company-tabnine :type git :host github :repo "karta0807913/company-tabnine")
  :after company
  :config
  (add-to-list 'company-backends '(:separate company-capf company-yasnippet company-tabnine)))

vertico

helmivy よりも補完インタフェース新しくシンプルな補完パッケージです。


(use-package vertico
  :straight t
  :custom (vertico-cycle t)
  :init
  (vertico-mode +1)

  :config
  ;; Add prompt indicator to ~completing-read-multiple'.
  ;; We display [CRM<separator>], e.g., [CRM,] if the separator is a comma.
  (defun crm-indicator (args)
    (cons (format "[CRM%s] %s"
                  (replace-regexp-in-string
                   "\\~\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
                   crm-separator)
                  (car args))
          (cdr args)))
  (advice-add #'completing-read-multiple :filter-args #'crm-indicator)

  ;; Do not allow the cursor in the minibuffer prompt
  (setq minibuffer-prompt-properties
        '(read-only t cursor-intangible t face minibuffer-prompt))
  (add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)

  ;; Emacs 28: Hide commands in M-x which do not work in the current mode.
  ;; Vertico commands are hidden in normal buffers.
  (setq read-extended-command-predicate
        #'command-completion-default-include-p)

  ;; Enable recursive minibuffers
  (setq enable-recursive-minibuffers t)

  (with-eval-after-load 'consult
    ;; Use ~consult-completion-in-region' if Vertico is enabled.
    ;; Otherwise use the default ~completion--in-region' function.
    (setq completion-in-region-function
          (lambda (&rest args)
            (apply (if vertico-mode
                       #'consult-completion-in-region
                     #'completion--in-region)
                   args)))))

vertico-repeat

verito の拡張機能の一つで直前のコマンドを再度表示します。


(use-package vertico-repeat
  :after vertico
  :load-path "straight/build/vertico/extensions/"
  :bind (nil
         :map my-space-map
         ("z" . vertico-repeat))
  :hook (minibuffer-setup . vertico-repeat-save))

vertico-directory

verito の拡張機能の一つで find-file 等、ファイルやディレクトリの操作を良くします。


(use-package vertico-directory
  :after vertico
  :load-path "straight/build/vertico/extensions/"
  :bind (nil
         :map vertico-map
         ("<backspace>" . vertico-directory-delete-char)))

consult

verticoselectrum で利用できる便利なコマンドを提供してくれます。


;; Example configuration for Consult
(use-package consult
  :straight t
  :demand t
  ;; Replace bindings. Lazily loaded due by `use-package'.
  :bind (;; C-c bindings (mode-specific-map)
         ("C-c h" . consult-history)
         ("C-c m" . consult-mode-command)
         ("C-c k" . consult-kmacro)
         ;; C-x bindings (ctl-x-map)
         ("C-x M-:" . consult-complex-command)     ;; orig. repeat-complex-command
         ([remap switch-to-buffer] . consult-buffer)                ;; orig. switch-to-buffer
         ([remap switch-to-buffer-other-window] . consult-buffer-other-window) ;; orig. switch-to-buffer-other-window
         ([remap switch-to-buffer-other-frame] . consult-buffer-other-frame)  ;; orig. switch-to-buffer-other-frame
         ([remap bookmark-jump] . consult-bookmark)            ;; orig. bookmark-jump
         ([remap project-switch-to-buffer] . consult-project-buffer)      ;; orig. project-switch-to-buffer
         ;; Custom M-# bindings for fast register access
         ("M-#" . consult-register-load)
         ("M-'" . consult-register-store)          ;; orig. abbrev-prefix-mark (unrelated)
         ("C-M-#" . consult-register)
         ;; Other custom bindings
         ("M-y" . consult-yank-pop)                ;; orig. yank-pop
         ("<help> a" . consult-apropos)            ;; orig. apropos-command
         :map goto-map
         ("e" . consult-compile-error)
         ("f" . consult-flymake)               ;; Alternative: consult-flycheck
         ("g" . consult-goto-line)             ;; orig. goto-line
         ("M-g" . consult-goto-line)           ;; orig. goto-line
         ("o" . consult-outline)               ;; Alternative: consult-org-heading
         ("m" . consult-mark)
         ("k" . consult-global-mark)
         ("i" . consult-imenu)
         ("I" . consult-imenu-multi)
         :map search-map
         ("d" . consult-find)
         ("D" . consult-locate)
         ("g" . consult-grep)
         ("G" . consult-git-grep)
         ("r" . consult-ripgrep)
         ("l" . consult-line)
         ("L" . consult-line-multi)
         ("m" . consult-multi-occur)
         ("k" . consult-keep-lines)
         ("u" . consult-focus-lines)
         ("e" . consult-isearch-history)
         :map isearch-mode-map
         ("M-e" . consult-isearch-history)         ;; orig. isearch-edit-string
         ("M-s e" . consult-isearch-history)       ;; orig. isearch-edit-string
         ("M-s l" . consult-line)                  ;; needed by consult-line to detect isearch
         ("M-s L" . consult-line-multi)            ;; needed by consult-line to detect isearch
         ;; Minibuffer history
         :map minibuffer-local-map
         ("M-s" . consult-history)                 ;; orig. next-matching-history-element
         ("M-r" . consult-history))                ;; orig. previous-matching-history-element

  ;; Enable automatic preview at point in the *Completions* buffer. This is
  ;; relevant when you use the default completion UI.
  :hook (completion-list-mode . consult-preview-at-point-mode)

  ;; The :init configuration is always executed (Not lazy)
  :init

  ;; Optionally configure the register formatting. This improves the register
  ;; preview for `consult-register', `consult-register-load',
  ;; `consult-register-store' and the Emacs built-ins.
  (setq register-preview-delay 0.5
        register-preview-function #'consult-register-format)

  ;; Optionally tweak the register preview window.
  ;; This adds thin lines, sorting and hides the mode line of the window.
  (advice-add #'register-preview :override #'consult-register-window)

  ;; Use Consult to select xref locations with preview
  (setq xref-show-xrefs-function #'consult-xref
        xref-show-definitions-function #'consult-xref)

  ;; Configure other variables and modes in the :config section,
  ;; after lazily loading the package.
  :config

  ;; Optionally configure preview. The default value
  ;; is 'any, such that any key triggers the preview.
  ;; (setq consult-preview-key 'any)
  ;; (setq consult-preview-key (kbd "M-."))
  ;; (setq consult-preview-key (list (kbd "<S-down>") (kbd "<S-up>")))
  ;; For some commands and buffer sources it is useful to configure the
  ;; :preview-key on a per-command basis using the `consult-customize' macro.
  (consult-customize
   consult-theme :preview-key '(:debounce 0.2 any)
   consult-ripgrep consult-git-grep consult-grep
   consult-bookmark consult-recent-file consult-xref
   consult--source-bookmark consult--source-file-register
   consult--source-recent-file consult--source-project-recent-file
   ;; :preview-key (kbd "M-.")
   :preview-key '(:debounce 0.4 any))

  ;; Optionally configure the narrowing key.
  ;; Both < and C-+ work reasonably well.
  (setq consult-narrow-key "<") ;; (kbd "C-+")

  ;; Optionally make narrowing help available in the minibuffer.
  ;; You may want to use `embark-prefix-help-command' or which-key instead.
  (define-key consult-narrow-map (vconcat consult-narrow-key "?") #'consult-narrow-help)

  ;; By default `consult-project-function' uses `project-root' from project.el.
  ;; Optionally configure a different project root function.
  ;; There are multiple reasonable alternatives to chose from.
    ;;;; 1. project.el (the default)
  ;; (setq consult-project-function #'consult--default-project--function)
    ;;;; 2. projectile.el (projectile-project-root)
  (autoload 'projectile-project-root "projectile")
  (setq consult-project-function (lambda (_) (projectile-project-root)))
    ;;;; 3. vc.el (vc-root-dir)
  ;; (setq consult-project-function (lambda (_) (vc-root-dir)))
    ;;;; 4. locate-dominating-file
  ;; (setq consult-project-function (lambda (_) (locate-dominating-file "." ".git")))
  )

consult-dir


(use-package consult-dir
  :straight t
  :bind (nil
         :map my-file-map
         ("d" . consult-dir)))

その他


(defun consult-ripgrep-current-directory ()
  (interactive)
  (consult-ripgrep default-directory))

(define-key search-map (kbd "R") #'consult-ripgrep-current-directory)

marginalia

vertico の候補に情報を追加してくれます。


(use-package marginalia
  :straight t
  :init
  (marginalia-mode +1))

embark

vertico の候補等に様々なアクションを提供してくれます。


(use-package embark
  :straight t
  :bind (("C-." . embark-act)         ;; pick some comfortable binding
         ("C-;" . embark-dwim)        ;; good alternative: M-.
         ("C-h B" . embark-bindings)) ;; alternative for `describe-bindings'
  :init
  (setq prefix-help-command #'embark-prefix-help-command)

  :config
  ;; Hide the mode line of the Embark live/completions buffers
  (add-to-list 'display-buffer-alist
               '("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
                 nil
                 (window-parameters (mode-line-format . none)))))

embark-consult

embarkconsult を連動させます。


(use-package embark-consult
  :straight t ; only need to install it, embark loads it after consult if found
  :hook (embark-collect-mode . consult-preview-at-point-mode))

yasnippet

スニペット機能を提供してくれます。


(use-package yasnippet
  :straight t
  :init
  (yas-global-mode +1))

yasnippet-snippets

各言語のスニペットを提供してくれます。


(use-package yasnippet-snippets
  :straight t
  :after yasnippet)

dtrt-indent

インデントを推測して、設定してくれます。


(use-package dtrt-indent
  :straight t
  :init
(dtrt-indent-global-mode +1))

magit

Emacs上でGitを快適に操作できるようにしてくれます。


(use-package magit :straight t)

diff-hl


(use-package diff-hl
  :straight t
  :init
  (global-diff-hl-mode +1))

which-key

キーバインドを可視化してくれます。


(use-package which-key
  :straight t
  :init
  (which-key-mode +1))

projectile

プロジェクトに関する便利機能を提供してくれます。


(use-package projectile
  :straight t
  :bind (("C-c p" . projectile-command-map)
         :map my-space-map
         ("p" . projectile-command-map))
  :init
  (projectile-mode +1))

flycheck

構文チェック機能を提供してくれます。


(use-package flycheck
  :straight t
  :custom (flycheck-idle-change-delay 1.0)
  :bind (nil
         :map my-error-map
         ("l" . flycheck-list-errors)
         ("n" . flycheck-next-error)
         ("p" . flycheck-previous-error))
  :init
  (global-flycheck-mode +1))

flycheck-posframe

エラー内容などを posframe を使用して表示してくれます。


(use-package flycheck-posframe
  :straight t
  :custom ((flycheck-posframe-warning-prefix "! ")
           (flycheck-posframe-info-prefix "··· ")
           (flycheck-posframe-error-prefix "X "))
  :hook (flycheck-mode-hook . flycheck-posframe-mode)
  :config
  (with-eval-after-load 'company
    (add-hook 'flycheck-posframe-inhibit-functions 'company--active-p))
  (with-eval-after-load 'evil
    (add-hook 'flycheck-posframe-inhibit-functions 'evil-insert-state-p)
    (add-hook 'flycheck-posframe-inhibit-functions 'evil-replace-state-p)))

consult-flycheck

チェック内容を consult を使用して絞り込めます。


(use-package consult-flycheck
  :straight t
  :bind (nil
         :map my-error-map
         ("e" . consult-flycheck)))

undo-tree

編集履歴をツリー表示してくれます。


(use-package undo-tree
  :straight t
  :custom (undo-tree-auto-save-history nil)
  :bind (nil
         :map my-space-map
         ("u" . undo-tree-visualize))
  :init
  (global-undo-tree-mode +1))

rg

ripgrep を利用してGrep検索してくれます。


(use-package rg :straight t)

rainbow-delimiters

括弧を色付けしてくれます。


(use-package rainbow-delimiters
  :straight t
  :hook (prog-mode . rainbow-delimiters-mode))

highlight-indent-guides

インデントを可視化してくれます。


(use-package highlight-indent-guides
  :straight t
:custom ((highlight-indent-guides-method 'character)
         (highlight-indent-guides-character 124)
         (highlight-indent-guides-responsive 'top))
  :bind (nil
         :map my-toggle-map
       ("i" . highlight-indent-guides-mode))
:hook ((prog-mode . highlight-indent-guides-mode)
     (text-mode . highlight-indent-guides-mode)))

theme

ef-themes


(use-package ef-themes
  :straight t
  :init
  ;; (load-theme 'ef-deuteranopia-light t)
  )

solarized-theme


(use-package solarized-theme
  :straight t
  :init
  ;; (load-theme 'solarized-light t)
  )

doom-themes


(use-package doom-themes
  :straight t
  :custom ((doom-themes-enable-bold t)
           (doom-themes-enable-italic t)
           (doom-themes-treemacs-theme "doom-colors"))
  :init
  (load-theme 'doom-nord-light t)
  (doom-themes-visual-bell-config)
  (doom-themes-treemacs-config)
  (doom-themes-org-config))

restart-emacs

Emacsを再起動してくれます。


(use-package restart-emacs
  :straight t
  :bind (nil
         :map my-quit-map
         ("r" . restart-emacs)))

all-the-icons

アイコンのインストールなど、アイコンに関する機能を提供してくれます。


(use-package all-the-icons
  :straight t
  :if (display-graphic-p))

all-the-icons-completion

vertico でアイコンが表示されるようになります。


(use-package all-the-icons-completion
  :straight t
  :hook (marginalia-mode . all-the-icons-completion-marginalia-setup)
  :init
  (all-the-icons-completion-mode +1))

expand-region

er/expand-region を押すと選択範囲をどんどん広げてくれます。


(use-package expand-region
  :straight t
  :demand t
  :bind (("C-=" . er/expand-region)
         :map my-space-map
         ("v" . er/expand-region)))

beacon

カーソルの移動を強調表示してくれます。


(use-package beacon
  :straight t
  :custom (beacon-color "red")
  :init
  (beacon-mode +1))

gcmh

ウィンドウが非活性な時などにガベージコレクションを実行してくれます。


(use-package gcmh
  :straight t
  :custom ((gcmh-idle-delay 'auto)
           (gcmh-auto-idle-delay-factor 10)
           (gcmh-high-cons-threshold (* 128 1024 1024)))
  :init
  (gcmh-mode +1))

dashboard

起動画面をいい感じにしてくれます。


(use-package dashboard
  :straight t
  :custom ((dashboard-center-content t)
           (dashboard-set-heading-icons t)
           (dashboard-set-file-icons t)
           (dashboard-set-navigator t)
           (dashboard-set-init-info t))
  :init
  (dashboard-setup-startup-hook))

volatile-highlights

Redo等、一部の操作を強調表示して操作がわかりやすくなります。


(use-package volatile-highlights
  :straight t
  :init
  (volatile-highlights-mode +1))

alert

通知機能を利用できるようにします。主に org-pomodoro で使用します。


(use-package alert
  :straight t
  :custom (alert-default-style 'libnotify))

alert-toast

Windows用の設定です。


(use-package alert-toast
  :straight t
  :if IS-WINDOWS
  :custom (alert-default-style 'toast))

valign

org-modemarkdown のテーブル機能で日本語が含まれてもずれないようにしてくれます。


(use-package valign
  :straight t
  :hook ((org-mode . valign-mode)
         (markdown-mode . valign-mode)))

tree-sitter

tree-sitter をEmacsで利用できるようにします。


(use-package tree-sitter
  :straight t
  :hook (tree-sitter-after-on . tree-sitter-hl-mode)
  :init
  (global-tree-sitter-mode))

tree-sitter-langs


(use-package tree-sitter-langs
  :straight t
  :after tree-sitter)

polymode

一つのバッファーに対して、複数のメジャーモードを適用してくれるようになります。org-babel 等で活躍します。


(use-package polymode :straight t)
(use-package poly-markdown :straight t)
(use-package poly-org :straight t)

treemacs

lsp-mode を利用すると一緒にインストールされます。普段は利用しませんが、READMEをもとに設定しています。


(use-package treemacs
  :straight t
  :defer t
  :init
  (with-eval-after-load 'winum
    (define-key winum-keymap (kbd "M-0") #'treemacs-select-window))
  :config
  (progn
    (setq treemacs-collapse-dirs                   (if treemacs-python-executable 3 0)
          treemacs-deferred-git-apply-delay        0.5
          treemacs-directory-name-transformer      #'identity
          treemacs-display-in-side-window          t
          treemacs-eldoc-display                   'simple
          treemacs-file-event-delay                2000
          treemacs-file-extension-regex            treemacs-last-period-regex-value
          treemacs-file-follow-delay               0.2
          treemacs-file-name-transformer           #'identity
          treemacs-follow-after-init               t
          treemacs-expand-after-init               t
          treemacs-find-workspace-method           'find-for-file-or-pick-first
          treemacs-git-command-pipe                ""
          treemacs-goto-tag-strategy               'refetch-index
          treemacs-header-scroll-indicators        '(nil . "^^^^^^")
          treemacs-hide-dot-git-directory          t
        treemacs-indentation                     2
        treemacs-indentation-string              " "
          treemacs-is-never-other-window           nil
          treemacs-max-git-entries                 5000
          treemacs-missing-project-action          'ask
          treemacs-move-forward-on-expand          nil
          treemacs-no-png-images                   nil
          treemacs-no-delete-other-windows         t
          treemacs-project-follow-cleanup          nil
          treemacs-persist-file                    (expand-file-name ".cache/treemacs-persist" user-emacs-directory)
          treemacs-position                        'left
          treemacs-read-string-input               'from-child-frame
          treemacs-recenter-distance               0.1
          treemacs-recenter-after-file-follow      nil
          treemacs-recenter-after-tag-follow       nil
          treemacs-recenter-after-project-jump     'always
          treemacs-recenter-after-project-expand   'on-distance
          treemacs-litter-directories              '("/node_modules" "/.venv" "/.cask")
          treemacs-show-cursor                     nil
          treemacs-show-hidden-files               t
          treemacs-silent-filewatch                nil
          treemacs-silent-refresh                  nil
          treemacs-sorting                         'alphabetic-asc
          treemacs-select-when-already-in-treemacs 'move-back
          treemacs-space-between-root-nodes        t
          treemacs-tag-follow-cleanup              t
          treemacs-tag-follow-delay                1.5
          treemacs-text-scale                      nil
          treemacs-user-mode-line-format           nil
          treemacs-user-header-line-format         nil
          treemacs-wide-toggle-width               70
          treemacs-width                           35
          treemacs-width-increment                 1
          treemacs-width-is-initially-locked       t
          treemacs-workspace-switch-cleanup        nil)

    ;; The default width and height of the icons is 22 pixels. If you are
    ;; using a Hi-DPI display, uncomment this to double the icon size.
    ;;(treemacs-resize-icons 44)

    (treemacs-follow-mode t)
    (treemacs-filewatch-mode t)
    (treemacs-fringe-indicator-mode 'always)
    (when treemacs-python-executable
      (treemacs-git-commit-diff-mode t))

    (pcase (cons (not (null (executable-find "git")))
                 (not (null treemacs-python-executable)))
      (`(t . t)
       (treemacs-git-mode 'deferred))
      (`(t . _)
       (treemacs-git-mode 'simple)))

    (treemacs-hide-gitignored-files-mode nil))
  :bind (nil
         :map global-map
         ("M-0"       . treemacs-select-window)
         ("C-x t 1"   . treemacs-delete-other-windows)
         ("C-x t t"   . treemacs)
         ("C-x t d"   . treemacs-select-directory)
         ("C-x t B"   . treemacs-bookmark)
         ("C-x t C-t" . treemacs-find-file)
         ("C-x t M-t" . treemacs-find-tag)
         :map my-file-map
         ("t" . treemacs)))

(use-package treemacs-evil
  :after (treemacs evil)
  :straight t)

(use-package treemacs-projectile
  :after (treemacs projectile)
  :straight t)

(use-package treemacs-icons-dired
  :hook (dired-mode . treemacs-icons-dired-enable-once)
  :straight t)

(use-package treemacs-magit
  :after (treemacs magit)
  :straight t)

(use-package treemacs-all-the-icons
  :straight t
  :after treemacs treemacs-icons-dired
  :config
  (treemacs-load-theme "all-the-icons"))

apheleia

保存時などに自動的にフォーマットしてくれます。


(use-package apheleia
  :straight t
  :hook (python-mode . apheleia-mode))

string-inflection


(use-package string-inflection
  :straight t
  :bind (nil
         :map my-string-inflection-map
         ("a" . string-inflection-all-cycle)
         ("_" . string-inflection-underscore)
         ("p" . string-inflection-pascal-case)
         ("c" . string-inflection-camelcase)
         ("u" . string-inflection-upcase)
         ("k" . string-inflection-kebab-case)
         ("C" . string-inflection-capital-underscore)))

line-reminder


(use-package line-reminder
  :straight t
  :init
  (global-line-reminder-mode +1))

super-save


(use-package super-save
  :straight t
  :custom ((super-save-auto-save-when-idle t)
           (super-save-idle-duration 4))
  :config
  (with-eval-after-load 'evil
    (add-to-list 'super-save-triggers 'evil-window-next)
    (add-to-list 'super-save-triggers 'evil-window-prev))

  (super-save-mode +1))

lsp-mode

EmacsでLSP機能が利用できるようになります。


(use-package lsp-mode
  :straight t
  :custom ((lsp-keymap-prefix "M-l")
           (lsp-idle-delay 1.0)
           (lsp-signature-auto-activate '(:on-trigger-char :after-completion :on-server-request)))
  :general (:states '(normal visual) :keymaps 'lsp-mode-map :prefix "SPC m" :prefix-command 'lsp-command-map)
  :hook ((lsp-mode . lsp-enable-which-key-integration)
         (html-mode . lsp)
         (css-mode . lsp)
         (rust-mode . lsp)
         (nxml-mode . lsp)
         (java-mode . lsp)
         (js-mode . lsp)))

lsp-ui

UIを提供してくれます。


(use-package lsp-ui
  :straight t
  :custom ((lsp-ui-doc-delay 1.0)
           (lsp-ui-sideline-delay 1.0)))

lsp-java

JavaのLSは特殊なため、専用の拡張パッケージをインストールします。


(use-package lsp-java
  :straight t
  :after java-mode)

lsp-pyright

pyright を利用したい場合、こちらのパッケージをインストールします。


(use-package lsp-pyright
  :straight t
  :after python-mode)

lsp-treemacs

treemacslsp-mode を組み合わせてくれます。


(straight-use-package 'lsp-treemacs)

language

各言語のインデントの設定や専用のパッケージを設定しています。

elisp

  1. highlight-defined

    既知のシンボルに色を付けてくれます。

    
    (use-package highlight-defined
      :straight t
      :hook (emacs-lisp-mode . highlight-defined-mode))
    
    
  2. highlight-quoted

    引用符と引用記号を色付けしてくれます。

    
    (use-package highlight-quoted
      :straight t
      :hook (emacs-lisp-mode . highlight-quoted-mode))
    
    

java


(use-package cc-mode
  :hook (java-mode . (lambda () (setq-local tab-width 2))))

web

  1. html

    
    (use-package sgml-mode
      :hook (html-mode . (lambda () (setq-local tab-width 2)))
      :mode ("\\.jsp\\'" . html-mode))
    
    
  2. css

    
    (use-package css-mode
      :hook (css-mode . (lambda () (setq-local tab-width 2))))
    
    
  3. javascript

    
    (use-package js
      :hook (js-mode . (lambda () (setq-local tab-width 2))))
    
    
  4. emmet-mode

    Emmet を導入します。

    
    (use-package emmet-mode
      :straight t
      :hook ((html-mode . emmet-mode)
             (css-mode . emmet-mode)))
    
    
  5. web-beautify

    web-beautify を導入します。

    
    (use-package web-beautify
      :straight t)
    
    

rust-mode


(use-package rust-mode
  :straight t
  :hook (rust-mode . (lambda ()
                       (setq-local tab-width 4))))

  1. cargo

    
    (use-package cargo
      :straight t
      :hook (rust-mode . cargo-minor-mode))
    
    

python

  1. pyvenv

    
    (use-package pyvenv
      :straight t)
    
    

common lisp

  1. slime

    
    (use-package slime
      :straight t
      :custom (inferior-lisp-program "sbcl"))
    
    
  2. slime-company

    
    (use-package slime-company
      :straight t
      :after slime company
      :config
      (slime-setup '(slime-fancy slime-company slime-banner)))
    
    

sql

  1. sql-indent

    
    (use-package sql-indent
      :straight t
      :hook (sql-mode . sqlind-minor-mode))
    
    
  2. sql-format

    
    (use-package sqlformat
      :straight t
      :custom (sqlformat-command "sqlfluff"))
    
    

xml


(use-package nxml-mode
  :hook (nxml-mode . (lambda () (setq-local tab-width 4))))

markdown


(use-package markdown-mode
  :hook (markdown-mode . (lambda ()
                           (setq-local tab-width 4
                                       indent-tabs-mode nil))))

sql


(use-package sqlformat
  :straight t
  :custom (sqlformat-command "sqlfluff"))

footer


(provide 'config)
    ;;; config.el ends here

終わりに

Emacsの設定をorg-modeで管理することで、コード全体が見やすくなったり、Markdownファイルを生成することでQiitaへの投稿が楽になりました。
色々な便利パッケージを導入できたら、随時こちらの記事に反映していきたいです。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5