Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting arbitrary lines in VIM

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?

like image 352
Randerson Avatar asked Apr 23 '16 16:04

Randerson


People also ask

How do I highlight a line in Vim?

If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor. Finally, you can select text in columns by pressing ctrl+v and moving up or down the block.

Does vim have syntax highlighting?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.


2 Answers

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

like image 54
Dave Avatar answered Sep 28 '22 07:09

Dave


: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().

like image 32
yolenoyer Avatar answered Sep 28 '22 06:09

yolenoyer