Abbrevs

From WikEmacs
Jump to: navigation, search

Abbrevs[edit]

Abbbrevs are small pieces of text that are expanded as you type.

The expansion is context sensitive, so it depends on the mode you opened the file:

  • if you open a .py file, it is python-mode so it uses the table for python
  • if you open a .c file, it is C language so it uses de table for C language
  • so on

Abbrevs are loaded from a Emacs-LISP file. The traditional location of this file is:

~/.abbrev_defs

The format of the sections in this file is the following:

 (define-abbrev-table 'xxxx-mode-abbrev-table '(
   ("abbr" "text to expand for abbr" nil 0)
   ("other" "other text" nil 0)
              ...
   ))

Example:

 (define-abbrev-table 'html-mode-abbrev-table '(
   ("htmls" "<html>\n<head>\n<title></title>\n<style></style>\n</head>\n<body>\n</body>\n</html>\n" nil 0)
   ("table" "<table>\n</table>" nil 0)
   ("tr" "<tr><td></td></tr>" nil 0)
   ("td" "<td>  </td>")
   ... other usefull and frequently used HTML definitions here ...
   ))

Abbrev files can be read using this Emacs Lisp call:

(read-abbrev-file "~/.abbrev_defs")

You can use it dynamically as a Emacs command:

M-x read-abbrev-file

There is a minor mode of Emacs called Abbrev-mode. It can be switched on an off with this command:

M-x abbrev-mode

If you (as me, rcaguiar) don't like very much this mode, you can disable it in .emacs file adding the following line:

(setq default-abbrev-mode -1)

At the end of Emacs session it usually writes the current Abbrevs to file. For it to NOT happen, you need to include the following line in .emacs file:

(setq save-abbrevs nil)

To make the use of Abbrevs less invasive than Abbrev Mode, you can use the Emacs Lisp function "expand-abbrevs". It is usually bound to some shortcut as < C-x ' > for instance. You can use < C-h b > to confer the bindings. To bind it to some other key combination you can use the following line in your .emacs file:

(global-set-key [f4] 'expand-abbrev)