Syntax Highlighting While Editing in MySQL Client

Ovid on 2008-05-06T12:53:43

You see, the great thing about posting vim stuff here is that people (Smylers, Aristotle, etc.) immediately post corrections. I appreciate that :)

Problem: when in the MySQL client, you can type 'edit' and edit your last SQL statement (can I go back further? I thought so, but I can't recall how). However, I want my syntax highlighting, but the filetype isn't set. After a bit of testing, I found out that the filename is always /var/tmp/sql* (on my system). So the following addition to my .vimrc fixes this:

" this is for MySQL's 'edit' command while in the client
au! BufRead,BufNewFile /var/tmp/sql* :call SetMySQL()
function! SetMySQL()
    set ft=sql
    SQLSetType mysql
endfunction


Having problems with this?

Fox on 2008-05-06T20:58:01

For this to work it might also be necessary in certain cases for the following to appear in your .vimrc:

filetype plugin on

Re:Having problems with this?

Ovid on 2008-05-06T21:25:23

Yes, I should have pointed that out. Thanks.

Very nice

baest on 2008-05-07T06:02:01

Thanks!

If anybody use PostgreSQL like I do, use "SQLSetType psql" instead

I hate to be so predictable ;-)

Aristotle on 2008-05-07T07:11:00

When you set filetype in an autocommand, it’s better to use the setf command instead of set ft=. And why not make it a one-liner?

au! BufRead,BufNewFile /var/tmp/sql* setf sql | SQLSetType mysql

Re:I hate to be so predictable ;-)

Ovid on 2008-05-07T08:16:10

I guess setf is better in this case (didn't know about it until you suggested it), but I had tried to make this a one-liner, but I was using &&. I didn't think to try the bar. Thanks.

Now to try and integrate SQL::Tidy :)