在vim中突出显示不匹配的括号 [英] Highlighting unmatched brackets in vim
问题描述
在 vim 中编写 python 代码时,我被不匹配的括号反复烧毁.我喜欢它们对 C 代码的处理方式——vim 用红色突出显示了所有跟在不匹配括号后面的花括号.我简要地查看了 c.vim
语法文件以试图理解它,但是处理括号错误的部分非常复杂.谁能解释该代码的工作原理并建议我如何为 python 代码编写类似的代码?
I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the c.vim
syntax file briefly to try to understand it, but the section that handles bracket errors is very complex. Can anyone explain how that code works and suggest how I might write something similar for python code?
具有不匹配括号的示例 C 代码:
Example C code with unmatched parens:
int main(void
{ /* brace highlighted in red */
} /* brace highlighted in red */
由于 python 代码没有花括号来突出显示,我们将不得不选择其他东西(可能是其他括号).
Since python code doesn't have curly braces to highlight, we'll have to choose something else (perhaps other parentheses).
顺便说一句,我尝试了 这个 vim 插件,但我没有对这种行为不满意.
BTW, I tried out this vim plugin but I wasn't happy with the behavior.
我正在使用 python 生成 C++ 代码(一种喜欢括号和分号的语言).我有一个讨厌的习惯,就是在 file.write()
方法调用中去掉尾随括号.如果我能让 vim 使这个错误在视觉上更明显,那就太好了.
I'm using python to generate C++ code (a language that likes parentheses and semicolons). I have a nasty habit of leaving the trailing paren off a file.write()
method call. It would be nice if I could get vim to make that mistake more visually obvious.
更新:
好的,这是我目前尝试过的.
Ok, here's what I've tried so far.
:syn region pParen transparent start="(" end=")" contains=ALL
:syn match pError display ")"
:hi def link pError Error
不幸的是,这一切所做的只是将所有平衡括号的右括号突出显示为错误,这与我想要做的相反.我真的不明白我在这里做什么(只是从现有的 C 语法文件中复制出来的).如果有人能解释我做了什么(错误),我将不胜感激.
Unfortunately, all this does is highlight as an error the right paren of all balanced parentheses, the opposite of what I want to do. I really don't understand what I'm doing here (just copied off of the existing C syntax file). If anyone could explain what I did (wrong), I would appreciate it.
推荐答案
如果我理解正确,并且您正在尝试查看 C 代码(在 Python 中生成)中的非匹配括号,我建议您安装 Rainbow.vim 来自 奇普博士的网站.这将根据缩进级别以不同颜色突出显示大括号,并根据您的要求以红色突出显示不匹配的大括号.屏幕截图 http://img294.imageshack.us/img294/8586/rainbow.jpg http://img294.imageshack.us/img294/8586/rainbow.jpg
If I understand correctly and you are trying to look at non-matching parenthesis in C code (that was generated in python), I would recommend you install rainbow.vim from Dr Chip's Site. This will highlight braces in different colours depending on the levels of indentation and will highlight unmatching braces in red as you have requested. A screenshot http://img294.imageshack.us/img294/8586/rainbow.jpg http://img294.imageshack.us/img294/8586/rainbow.jpg
要安装,下载 rainbow.vim
并放置在 vimfiles/after/syntax/c/
(如果不是,请创建此目录存在).
To install, download rainbow.vim
and place in vimfiles/after/syntax/c/
(create this directory if it is not present).
在 Linux 上,这将是 ~/.vim/after/syntax/c/rainbow.vim
On Linux, this will be ~/.vim/after/syntax/c/rainbow.vim
在 Windows 上,它可能是 c:\vim\vimfiles\after\syntax\c\rainbow.vim
或其他地方,请参阅 :help runtimepath
.
On Windows, it may be c:\vim\vimfiles\after\syntax\c\rainbow.vim
or possibly somewhere else, see :help runtimepath
.
注意有一些插件和rainbow.vim
有冲突,但是让它们配合起来并不难.
Note that there are some plugins that conflict with rainbow.vim
, but it's not too hard to make them co-operate.
如果你想在 python 代码中突出显示不匹配的括号,你可以修改 Rainbow.vim 来使用 python 语法簇而不是 C 语法簇,但这有点复杂,但你可以使用一些东西(奇普博士彩虹代码的修改版本)的行:
If you are trying to highlight non-matching parenthesis in the python code, you could modify rainbow.vim to use the python syntax clusters instead of the C ones, but this is a little more involved, but you could use something along the lines of (modified version of Dr Chip's rainbow code):
syn cluster pyParenGroup contains=pythonString,pythonRawString,pythonEscape,pythonNumber,pythonBuiltin,pythonException
syn match pyParenError display ')'
syn region pyParen transparent matchgroup=hlLevel0 start='(' end=')' contains=@pyParenGroup,pyParen1
syn region pyParen1 transparent matchgroup=hlLevel1 start='(' end=')' contains=@pyParenGroup,pyParen2
syn region pyParen2 transparent matchgroup=hlLevel2 start='(' end=')' contains=@pyParenGroup,pyParen3
syn region pyParen3 transparent matchgroup=hlLevel3 start='(' end=')' contains=@pyParenGroup,pyParen4
syn region pyParen4 transparent matchgroup=hlLevel4 start='(' end=')' contains=@pyParenGroup,pyParen5
syn region pyParen5 transparent matchgroup=hlLevel5 start='(' end=')' contains=@pyParenGroup,pyParen6
syn region pyParen6 transparent matchgroup=hlLevel6 start='(' end=')' contains=@pyParenGroup,pyParen7
syn region pyParen7 transparent matchgroup=hlLevel7 start='(' end=')' contains=@pyParenGroup,pyParen8
syn region pyParen8 transparent matchgroup=hlLevel8 start='(' end=')' contains=@pyParenGroup,pyParen9
syn region pyParen9 transparent matchgroup=hlLevel9 start='(' end=')' contains=@pyParenGroup,pyParen
hi link pyParenError Error
if &bg == "dark"
hi default hlLevel0 ctermfg=red guifg=red1
hi default hlLevel1 ctermfg=yellow guifg=orange1
hi default hlLevel2 ctermfg=green guifg=yellow1
hi default hlLevel3 ctermfg=cyan guifg=greenyellow
hi default hlLevel4 ctermfg=magenta guifg=green1
hi default hlLevel5 ctermfg=red guifg=springgreen1
hi default hlLevel6 ctermfg=yellow guifg=cyan1
hi default hlLevel7 ctermfg=green guifg=slateblue1
hi default hlLevel8 ctermfg=cyan guifg=magenta1
hi default hlLevel9 ctermfg=magenta guifg=purple1
else
hi default hlLevel0 ctermfg=red guifg=red3
hi default hlLevel1 ctermfg=darkyellow guifg=orangered3
hi default hlLevel2 ctermfg=darkgreen guifg=orange2
hi default hlLevel3 ctermfg=blue guifg=yellow3
hi default hlLevel4 ctermfg=darkmagenta guifg=olivedrab4
hi default hlLevel5 ctermfg=red guifg=green4
hi default hlLevel6 ctermfg=darkyellow guifg=paleturquoise3
hi default hlLevel7 ctermfg=darkgreen guifg=deepskyblue4
hi default hlLevel8 ctermfg=blue guifg=darkslateblue
hi default hlLevel9 ctermfg=darkmagenta guifg=darkviolet
endif
作为测试,我从 ftp://ftp.vim.org/pub/vim/pc/(这些是 Vim 7.0 的 Windows 版本).我将这两个文件解压缩到一个新目录中,然后从 vim/vim70/gvim.exe
运行 gvim.exe
.我没有在C:\Documents and Settings"中存储任何vim配置,所以运行这个vim与运行'vanilla'配置是一样的.然后我从 amk.ca/python/下载了 pyprint.py
simple/pyprint.html 作为一段示例代码,并将上面的代码复制到一个名为 code.vim 的文件中.在 gVim 中,我输入了 :e pyprint.py
.它在白色背景窗口中打开,没有语法突出显示.然后我输入 :syntax on
,它打开了默认的语法突出显示.我在第 8 行添加了第二个 )
字符.最后,我输入了 :source code.vim
,这使得第二个 )
字符在红色.
As a test, I downloaded gvim70.zip and vim70rt.zip from ftp://ftp.vim.org/pub/vim/pc/ (these are the Windows versions of Vim 7.0). I unzipped the two files into a new directory and ran gvim.exe
from vim/vim70/gvim.exe
. I do not have any vim configuration stored in "C:\Documents and Settings", so running this vim is the same as running a 'vanilla' configuration. I then downloaded pyprint.py
from amk.ca/python/simple/pyprint.html as a piece of sample code and copied the above code into a file called code.vim. In gVim, I entered :e pyprint.py
. It opened in the white-background window, with no syntax highlighting. I then entered :syntax on
, which switched the default syntax highlighting on. I added a second )
character on line 8. Finally, I entered :source code.vim
, which made the second )
character be highlighted in red.
我也在 Linux(使用 Vim 7.2)上进行了这个测试,输入以下命令序列:
I've also carried out this test on Linux (with Vim 7.2), by entering the following command sequence:
cd ~
mv .vimrc old_dot_vimrc
mv .gvimrc old_dot_gvimrc
mv .vim old_dot_vim
vim pyprint.py
:e pyprint.py
" Add extra bracket here!
:syntax on
:source code.vim
同样,第二个括号突出显示,其他一切似乎正常.
Again, the second bracket is highlighted and everything else seems normal.
这篇关于在vim中突出显示不匹配的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!