Making emacs to highlight postgresql syntax by def

2020-06-01 05:26发布

问题:

I use emacs for editing my sql code. I work 99% of time on postgresql plpgsql code. All my files with extension .sql contain postgresql. I'm curious is there a way to set sql-highlight-postgres-keywords SQL highlighting default instead of ANSI SQL, because it's pretty annoying to switch mode every time I open a file.

回答1:

Usually in emacs, if you want to change the settings every time some mode is opened, you use a hook. Something similar to this should work:

(add-to-list 'auto-mode-alist
             '("\\.psql$" . (lambda ()
                              (sql-mode)
                              (sql-highlight-postgres-keywords))))


回答2:

If you need to work with different databases, rather than using a hook to always switch to PostgreSQL highlighting when you open a .sql file, you can use Emacs' file variables feature to set the product on a file-by-file basis.

For example, if the first line of your .sql file is

-- -*- mode: sql; sql-product: postgres; -*-

sql-mode will automatically use PostgreSQL highlighting.

Full details on Emacs file variables here (you can also set them in a block anywhere in the file), and the list of product names is probably eaiest found by doing M-x sql-set-product, backspacing the ansi default, and hitting TAB to see the completion list. Examples are "mysql", "oracle", "sqlite", etc (about a dozen in my install).