私は、Pythonプログラム内でSQLクエリに複数行の文字列を使用します。Vimの同じファイルでPythonとSQL構文の強調表示の両方を使用できますか?
--SQL
マーカーを使用してVimに異なるハイライトルールを適用するよう指示する方法はありますか?
私は、Pythonプログラム内でSQLクエリに複数行の文字列を使用します。Vimの同じファイルでPythonとSQL構文の強調表示の両方を使用できますか?
--SQL
マーカーを使用してVimに異なるハイライトルールを適用するよう指示する方法はありますか?
私はhereから-貼り付けコピー-変更:
今function! TextEnableCodeSnip(filetype,start,end,textSnipHl) abort
let ft=toupper(a:filetype)
let group='textGroup'.ft
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
" do nothing if b:current_syntax is defined.
unlet b:current_syntax
endif
execute 'syntax include @'.group.' syntax/'.a:filetype.'.vim'
try
execute 'syntax include @'.group.' after/syntax/'.a:filetype.'.vim'
catch
endtry
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
else
unlet b:current_syntax
endif
execute 'syntax region textSnip'.ft.'
\ matchgroup='.a:textSnipHl.'
\ start="'.a:start.'" end="'.a:end.'"
\ [email protected]'.group
endfunction
au FileType python call TextEnableCodeSnip('sqlpostgres', "'''", "'''", 'SpecialComment')
ごとに複数行のトリプルシングルをquote stringはSQL構文を取得します。三重引用符で囲まれた文字列は、まだ普通です。 sqlpostgres.vimを変更して、SQLのニュアンスを使って2つの言語を区別し、256色インクポットスキームで甘く見えます。
また、関連あなたはあなたのファイルに次のオプションを試すことができます。
setfiletype=python.sql
それはあなたのファイルに両方のファイルタイプを与え、両方の構文の強調表示を適用する必要があります。
それはあなたのために働く場合は、あなたが編集し、すべてのそれのPythonのファイルに適用するためにあなたの.vimrc
に次の行を追加することができます。
autocmd BufRead,BufNewFile *.py setfiletype=python.sql
しかし、それは本当に間の紛争を扱っていません2つのハイライトグループ...あなたのケースでは動作しないかもしれません。
また、すばやく開いているバッファのファイルタイプを切り替えるための関数を作成することができます。
function! ToggleFiletype()
if &filetype=="sql"
set filetype=python
endif
if &filetype=="python"
set filetype=sql
endif
endfunction
map <F11> <Esc>:call ToggleFiletype()<cr>
au contraire、それはPythonコードにSQL構文を適用し、文字列はプレーンです。 –
私は受け入れ答えがあると知っているが、まだ、ここでそれを行うための別の方法である:それと
if exists("b:current_syntax")
finish
endif
" Load Python syntax at the top level
runtime! syntax/python.vim
" Needed to make syntax/sql.vim do something
unlet b:current_syntax
" Load SQL syntax
syntax include @SQL syntax/sql.vim
" Need to add the keepend here
syn region pythonString matchgroup=pythonQuotes
\ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
\ contains=pythonEscape,@Spell keepend
syn region pythonRawString matchgroup=pythonQuotes
\ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
\ [email protected] keepend
syn region SQLEmbedded [email protected] containedin=pythonString,pythonRawString contained
\ start=+\v(ALTER|BEGIN|CALL|COMMENT|COMMIT|CONNECT|CREATE|DELETE|DROP|END|EXPLAIN|EXPORT|GRANT|IMPORT|INSERT|LOAD|LOCK|MERGE|REFRESH|RENAME|REPLACE|REVOKE|ROLLBACK|SELECT|SET|TRUNCATE|UNLOAD|UNSET|UPDATE|UPSERT)+
\ end=+;+
let b:current_syntax = "pysql"
、与えられたSQLのキーワードのいずれかで開始さを強調し、最初の;
で停止し、次のSQLキーワードで再起動するか、Python文字列の最後で停止します(keepend
参照)。
OK、緑は吸いますが、アイデアは音です –
これはどのように使用しますか? – skyler