:set filetype=sql
did the correct highlighting of the sql-code for me.
I hope this helps.
Peng Yu schrieb:
> No. You misunderstood me. I think that the vim can recognized the file
> type by the suffix of the filename. There is no need to ":set
> filetype=sql" explicitly.
>
> But the default syntax highlight facility in vim doesn't reliably
> syntax highlight SQL code embedded in a bash script. In the following
> example, the SQL code after "*.txt" is not recognized correctly.
The syntax highlighting for PHP handles highlighting other languages
within heredocs. Perhaps you can find a solution somewhere in there.
I believe it 'detects' the language via the heredoc label, though, so
you might need to use <<SQL
SQL
instead of <<EOF
EOF
See also:
:help :syn-include
--
Best,
Ben
As I see it, the script is being highlighted correctly from sh's
viewpoint, as a heredoc is essentially just a long string.
What you appear to want is for the sh syntax script to recognize sqlite3
in the command and highlight the following heredoc using that other
syntax. As Ben H pointed out, it is possible to use the syn-include
approach to get what you want. I suggest writing a bit of extra syntax
handling for this purpose and put it in your .vim/after/syntax/sh.vim
file. Such a region would be recognized at the outset with sqlite3,
etc. However, I don't think sh.vim is the place to try to recognize
every possible language that heredoc strings might be using.
HTH,
Chip Campbell
Yep. That's what I was suggesting. And I agree that it shouldn't be in
the general 'syntax/sh.vim'. The following worksforme:
==> ~/.vim/after/syntax/sh.vim <==
unlet b:current_syntax
syntax include @sql syntax/sql.vim
syn region shHereDoc matchgroup=shRedir start=/<<\s*\\\=\z(\S*\)/ end=/^\z1\s*$/ contains=@shDblQuoteList,@sql
==================================
I got the pattern for the last line by looking at the patterns already
in the 'sh' filetype (With a 'sh' file open, run :syn). If it doesn't
work right for how you style your heredocs, you might want to try a
different one. (Not sure what the subtleties are that the many similar
matches are trying to capture, other than the contains-quoted-items
dichotomy.) You should also add the 'fold' flag if you use
g:sh_fold_enabled.
--
Best,
Ben
Regardless of what the main stuff is, it's still wrapped as a bash
script. I'd just use the .sh or .bash suffix, since that's what the
file is.
But, Vim will also let you call an apple an orange. In your vimrc:
aug FileTypeOverride
au!
au BufRead *.sql if -1 != match(getline(1),'^#!.*sh\>') | set ft=sh | endif
aug END
Note the use of 'set ft' rather than 'setf', which forces the filetype
to 'sh', even if it gets set to 'sql' before this autocmd is run.
The '^#!.*sh\>' pattern matches a shebang line containing a shell-like
name. e.g. #!/bin/bash, #!/bin/sh, #!/usr/local/bin/ash
--
Best,
Ben