Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making emacs to highlight postgresql syntax by default

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.

like image 630
Andriy Senyshyn Avatar asked Jan 25 '11 13:01

Andriy Senyshyn


2 Answers

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).

like image 55
David Arnold Avatar answered Oct 06 '22 04:10

David Arnold


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))))
like image 33
Mario F Avatar answered Oct 06 '22 06:10

Mario F