1

I use multiple programming languages, and there are a considerable number of abbreviations that I use across all of their corresponding major mode (they are usually name of certain variables, but not limited to this category). One way of customising the emacs is as following: As explained here one need to store them in an Elisp file (see the example below) and load it in the init file.

Example Elisp file for storing the abbreviations (ignore that syntaxes for the specified languages is not correct):

(when (boundp 'python-mode-abbrev-table)
  (clear-abbrev-table python-mode-abbrev-table))

(define-abbrev-table 'python-mode-abbrev-table

    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ("r" "return")
    ("st" "string")
    ("eq" "==")
    ("v" "var x = 3")
    ("df" "x := 3")
    ("c" "const x = 3")
    ))

(when (boundp 'matlab-mode-abbrev-table)
  (clear-abbrev-table matlab-mode-abbrev-table))

(define-abbrev-table 'matlab-mode-abbrev-table

    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ("r" "return")
    ("st" "string")
    ("eq" "==")
    ("v" "var x = 3")
    ("df" "x := 3")
    ("c" "const x = 3")
    ))

(set-default 'abbrev-mode t)

(setq save-abbrevs nil)

But then if you want to expand the list, you need to copy them for abbreviation of all major mode. Is there any way that I maintain only one list, and somehow call them for the needed major modes (maybe the solution is something like the \input{} in latex).

By the way, I prefer not to use the solution proposed here, as the usual/traditional way to setup abbreviations in emacs was not very efficient for me (of course if I don't manage to find any other way I may have to use it). Furthermore, each major mode can have their own abbrev table on top of the common one. In particular I don't want any of the major modes inherent all the abbreviation used in other modes (because language syntaxes are different in each language). Ideally, I'm trying to have something like this (I don't know any Elisp syntax, so ignore them!):

;; Common ones 
(when (boundp 'python-mode-abbrev-table OR matlab-mode-abbrev-table)

(define-abbrev-table 'python-mode-abbrev-table AND matlab-mode-abbrev-table

    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ("r" "return")
    ("st" "string")
    ("eq" "==")
    ("v" "var x = 3")
    ("df" "x := 3")
    ("c" "const x = 3")
    ))

;; Python specific ones 
(when (boundp 'python-mode-abbrev-table)

(define-abbrev-table 'python-mode-abbrev-table

    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ))

;; MATLAB specific ones 
(when (boundp 'matlab-mode-abbrev-table)

(define-abbrev-table 'matlab-mode-abbrev-table

    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ))


(set-default 'abbrev-mode t)

(setq save-abbrevs nil)

I'm new to emacs (and this is the first time posting here), so sorry if the question and the way I phrase is too naive!

0

I don't think there's a good UI to access it, but abbrev-tables can have parents, so you could create a new abbrev-table to hold your shared abbrevs and then setup those different major-mode abbrev-tables to have your new table as one of their parents.

Another option is to leave the major-mode tables as-is, but to turn local-abbrev-table into a list of abbrev-tables, e.g.:

(add-hook 'python-mode-hook
  (lambda ()
    (setq local-abbrev-table
          (list local-abbrev-table my-shared-abbrev-table))))

If those abbreviations are valid "almost everywhere" then you may just use the global-abbrev-table of course.

Finally, you could create a dummy minor-mode that does nothing else than provide an abbrev table:

(define-abbrev-table 'my-shared-abbrev-table ....)
(define-minor-mode my-shared-abbrevs-mode
  "Enable my shared abbrevs."
  :local t)
(add-to-list 'abbrev-minor-mode-table-alist
             (cons 'my-shared-abbrevs-mode
                   my-shared-abbrev-table))
  • Thanks for your answer. To me the most straightforward solution is the fist one. But I couldn't figure it out how to implement it (sorry for my naiveness in emacs). I could only find this which doesn't have an example. If I may ask, could you give me minimal example to define the shared abbrev-tabel and then how to tell the needed major mode to use this shared abbrev-tabel? – neuroprinciplist May 13 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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