
▲ 12 r/emacs
Abbreviations, but locally
I found this article https://www.rahuljuliato.com/posts/abbrev-mode, but I do not want to pollute my .emacs too much. Hence the question on how to make local abbreviations.
Here is my workaround (for LaTeX projects, but the idea is quite general), but please tell me if this can be achieved by some other trick buried in the Emacs' manual.
- Create a file
local-abbrevs.elwith the following content for example (from one of my works):
;;; local-abbrevs.el --- Local abbreviations -*- lexical-binding: t; -*-
;;;
;;; Commentary:
;;; Abbreviations for the current project
;;;
;;; Author: indrjo
;;;
;;; Code:
(defun inline-math (expr)
"Put mathematical expression EXPR between dollars."
(format "\\(%s\\)" expr))
(defun abbrev-math (abb full)
"If outside a math env, then put expand ABB into FULL within a math env."
`(,abb
""
,(lambda () (insert (if (texmathp) full (inline-math full))))))
(defun abbrev-only-in-math (abb full)
"Expand ABB into FULL only if point is in a math env."
`(,abb
""
,(lambda () (insert (if (texmathp) full abb)))))
(define-abbrev-table 'LaTeX-mode-abbrev-table
`(;; common expressions
("fol" "first-order language")
("struc" "structure")
("mdl" "model")
;; particular symbols
,(abbrev-math "cll" "\\mathscr{L}")
,(abbrev-math "mda" "\\mathfrak{A}")
,(abbrev-math "mdb" "\\mathfrak{B}")
,(abbrev-math "mdc" "\\mathfrak{C}")
,(abbrev-math "bn" "\\mathbb{N}")
;; arrows
,(abbrev-only-in-math "not" "\\not")
,(abbrev-only-in-math "and" "\\land")
,(abbrev-only-in-math "or" "\\land")
,(abbrev-only-in-math "ra" "\\to")
,(abbrev-only-in-math "rra" "\\Rightarrow")
,(abbrev-only-in-math "aa" "\\forall")
,(abbrev-only-in-math "ee" "\\exists")
,(abbrev-only-in-math "sbs" "\\subseteq")
,(abbrev-only-in-math "sqs" "\\sqsubseteq")))
(provide 'local-abbrevs)
;;; local-abbrevs.el ends here
- Append magic comments to other files, for instance
%%% Local Variables:
%%% ... stuff ...
%%% eval: (load-file "./local-abbrevs.el")
%%% End:
- Just
C-u C-c C-nor close and reopen Emacs (you will get a complain about security, but you can replyyor!) et voilà you can use your abbrevs.
u/indrjo — 14 hours ago