Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Last active April 27, 2024 13:26
Show Gist options
  • Save jeffkreeftmeijer/19e8cc39b202c96ca67d20401583e31a to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/19e8cc39b202c96ca67d20401583e31a to your computer and use it in GitHub Desktop.

linguist.el: A language modes pack for Emacs

Linguist is a language modes pack for Emacs, inspired by vim-polyglot.

Language packs

(use-package dockerfile-mode
  :config (linguist--loaded-message "dockerfile-mode")
  :mode ("\\.dockerfile\\'" . dockerfile-mode)
  :mode ("[/\\]\\(?:Containerfile\\|Dockerfile\\)\\(?:\\.[^/\\]*\\)?\\'" . dockerfile-mode))
(use-package elixir-mode
  :config (linguist--loaded-message "elixir-mode")
  :mode ("mix\\.lock" . elixir-mode)
  :mode ("\\.exs\\'" . elixir-mode)
  :mode ("\\.ex\\'" . elixir-mode)
  :mode ("\\.elixir\\'" . elixir-mode))
(use-package git-modes
  :config (linguist--loaded-message "gitattributes-mode")
  :mode ("/git/attributes\\'" . gitattributes-mode)
  :mode ("/info/attributes\\'" . gitattributes-mode)
  :mode ("/\\.gitattributes\\'" . gitattributes-mode))
(use-package git-modes
  :config (linguist--loaded-message "gitconfig-mode")
  :mode ("/etc/gitconfig\\'" . gitconfig-mode)
  :mode ("/\\.gitmodules\\'" . gitconfig-mode)
  :mode ("/git/config\\'" . gitconfig-mode)
  :mode ("/modules/.*/config\\'" . gitconfig-mode)
  :mode ("/\\.git/config\\'" . gitconfig-mode)
  :mode ("/\\.gitconfig\\'" . gitconfig-mode))
(use-package git-modes
  :config (linguist--loaded-message "gitignore-mode")
  :mode ("/git/ignore\\'" . gitignore-mode)
  :mode ("/info/exclude\\'" . gitignore-mode)
  :mode ("/\\.gitignore\\'" . gitignore-mode))
(use-package ledger-mode
  :config (linguist--loaded-message "ledger-mode")
  :mode ("\\.ledger\\'" . ledger-mode))
(use-package markdown-mode
  :config (linguist--loaded-message "markdown-mode")
  :mode ("\\.\\(?:md\\|markdown\\|mkd\\|mdown\\|mkdn\\|mdwn\\)\\'" . markdown-mode)
  :mode ("\\.mdx\\'" . markdown-mode))
(use-package nix-mode
  :config (linguist--loaded-message "nix-mode")
  :mode ("\\.nix\\'" . nix-mode)
  :mode ("^/nix/store/.+\\.drv\\'" . nix-drv-mode))
(use-package yaml-mode
  :config (linguist--loaded-message "yaml-mode")
  :mode ("\\.\\(e?ya?\\|ra\\)ml\\'" . yaml-mode))

Adding language packs

Each language pack requires one or multiple :mode keys with patterns to automatically load the mode when a file that matches it is opened.

The easiest way to determine patterns for new language packs is to extract them from auto-mode-alist. To do this, first install the package by installing it for the current session:

(use-package yaml-mode)

Then, open the help page for auto-mode-alist (C-h v auto-mode-alist <RET>) and find the patterns for the package being added:

("\\.\\(e?ya?\\|ra\\)ml\\'" . yaml-mode)

Take all pattern lines corresponding to the new package and add them as :mode=​s in a call to =use-package:

(use-package yaml-mode
  :config (linguist--loaded-message "yaml-mode")
  :mode ("\\.\\(e?ya?\\|ra\\)ml\\'" . yaml-mode))

If the package has multiple pattern lines, add a :mode for each:

(use-package elixir-mode
  :config (linguist--loaded-message "elixir-mode")
  :mode ("mix\\.lock" . elixir-mode)
  :mode ("\\.exs\\'" . elixir-mode)
  :mode ("\\.ex\\'" . elixir-mode)
  :mode ("\\.elixir\\'" . elixir-mode))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment