Highlighting arbitrary lines in VIM

∥☆過路亽.° 提交于 2021-02-07 19:16:06

问题


During the implementation process of a program I generally insert many append code lines, mainly with print command, to help me understand and debug the implemented program. Unfortunately, it is common to me to forget which lines are from the code and with were appended and should be deleted some time after. This problem gets worst with large programs.

Well, I found this article that teaches how to keep one arbitrary user selected line highlighted (see section: Highlighting that stays after cursor moves). The solution given by the article is to include in .vimrc the following code:

:nnoremap <silent> <Leader>l ml:execute 'match Search /\%'.line('.').'l/'<CR>

So, every time when I press \l the current line is highlighted and kept so, and the previous highlighted line, if there are one, is unhighlighted.

This isn't the behavior that I would like. Instead, I would like to be able to highlight as many arbitrary lines as I want without unhighlighting the previous highlighted lines. And if it is possible, with a unique command like \l.

Someone knows a solution for this?

Thanks in advance.

EDITED:

The command proposed by yolenoyer solved the initial problem. But, now other problem raised. The following command:

:call clearmatches()

proposed to clean the highlighted lines cleans all lines and I would like to be able to clean specific highlighted lines, instead off all of them at once. Is it possible?


回答1:


I program in C quite alot, and when debugging tend to pepper the code with debug prints.

I use the vim command

:syntax match Error /\<debug_printf\>/

to ensure the word 'debug_printf' is highlighted in the default 'Error' colors for the particular colorscheme.

This doesn't help you bookmarking a series of lines, but for that you should check out the 'bookmark' plugin which allows you to create and remove bookmarks throughout the file.

VIM Bookmarks Plugin




回答2:


:match accepts only one match.

Use the matchadd({highlight-group}, {pattern}) function instead, for example:

nnoremap <silent> <leader>l :call matchadd('Search', '\%'.line('.').'l')<cr>

To clear the matches you added, run :call clearmatches().




回答3:


I think your first paragraph, which explains your problem, has nothing to do with vim, so maybe you don't need to use vim to solve your problem.

What about not debugging with regular print statements, but with a function that wraps print? That would be really easy to search for program wide and also file wide (just search with * or # for all occurrences of your debug printing function).



来源:https://stackoverflow.com/questions/36813466/highlighting-arbitrary-lines-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!