I have defined some abbreviations for org-mode, but I want them to conditionally expand. For example, n2 expands to N_{2}, and I want to prevent that from happening in src-blocks.

The bottom of this page: https://www.gnu.org/software/emacs/manual/html_node/elisp/Abbrev-Expansion.html suggests I should use advice to achieve this. That doesn't seem too flexible though, I don't want all abbrevs to not work in src blocks, just some.

Are there other ways to get context-specific abbreviations?

1 Answer 1


Yes, you can tell the abbrev table to use an explicit function to test whether or not to fire in a given instance. The relevant argument in define-abbrev-table is :enable-function, which, according to the docstring, reads:

(define-abbrev-table TABLENAME DEFINITIONS &optional DOCSTRING &rest PROPS)

Define TABLENAME (a symbol) as an abbrev table name. Define abbrevs in it according to DEFINITIONS, which is a list of elements of the form (ABBREVNAME EXPANSION ...) that are passed to define-abbrev. PROPS is a property list to apply to the table. Properties with special meaning:

(blah blah blah)

  • :enable-function can be set to a function of no argument which returns non-nil if and only if the abbrevs in this table should be used for this instance of expand-abbrev.

So: the following toy example (untested) should work:

(define-abbrev-table 'org-mode-abbrev-table
  '(("test1"  "this is a test")
    ("test2"  "this is also a test"))
  "Table of abbrevs that should NOT fire when in a source block."
  :enable-function (lambda ()
                     (not (org-in-src-block-p))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.