1

When review code, it would be convenient if we can highlight multiple lines at the same time, instead of only current cursor line is highlighted.

:set cursorline only highlight current line.
:nnoremap <silent> <Leader>l ml:execute 'match Search /\%'.line('.').'l/'<CR> can only highlight one extra line.

Want to keep all marked lines highlighted.

3
  • You question is a bit too broad so far: are you talking about cursorline or some syntax highlighting? And on which criterion would you like to highlight your lines?
    – statox
    Commented Jun 21, 2018 at 8:35
  • Requirement is simple: move cursor to line a, highlight current line; move cursor to another line b, highlight it, ..., keep all marked lines highlighted. It doesn't need syntax highlighting. cursorline can only highlight current line.
    – Fisher
    Commented Jun 21, 2018 at 8:46
  • 1
    I don't think it is possible to keep some lines highlighted while doing something else than visual selection. One possible solution would be to add these two lines to your .vimrc: syntax region CoreReview start=/"CR/ end=/"RC/ and highlight CoreReview ctermbg=LightGreen. You can then put "CR at the beginning of the code you are reviewing and "RC at the end to highlight the background. It is even possible to create a mapping which will add the markers for you when you are in visual selection and remove them when you don't need them anymore.
    – statox
    Commented Jun 21, 2018 at 11:17

4 Answers 4

4

As you mention in your question, the :match command only allows a single match to be set up. For multiple matches, you can use the matchadd() function. You can adapt your existing mapping to use this like so:

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

For your expressed purpose of reviewing code, this should be sufficient. However, if you are adding/removing lines above the lines you have marked, this might not work as you desire: the marks will not move with the lines. There are ways around this, but all will result in much more complicated code.

1

Thanks for below answer, find a way to highlight multiple lines. https://superuser.com/a/697070/366981

Install plugin:multiselect-2.2.zip.
Ctrl and left click to select and highlight any line.
Ctrl and left click again to remove highlight.
<leader>mscto clear all selection.

enter image description here

Another method: Highlight multiple words enter image description here

1

This answer may be a couple years late but since I just had the same question, and none of these answers were even close to being helpful I though I'd share my solution.

I was wanting to improve my mapping that is similar to OP's that highlights a single line :

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

to be able to visually select multiple lines and highlight all of them.

After being unsuccessful trying to simply modify the mapping to a vnnoremap that takes a range I wrote a function that will run a loop for each line in the selection.

highlight LineHighlight ctermbg=100 guibg=#374090
vnoremap <leader>l :<c-u>call HiglightVisualLines()
nnoremap <leader>c :call clearmatches()

" HiglightVisualLines()
" loop throough visually selected lines and give them highlighting
function! HiglightVisualLines()
    for i in range(line('v'),line("'>"))
        call matchadd('LineHighlight', '\%'.i.'l')
    endfor
endfunction

As Rich mentioned with his comment, the highlighting is based on line number, not the content of the line, so adding lines above the highlighting will 'break' it.

-1

You press capital V and j or k to select other lines.

2
  • 1
    This only select continuous lines?
    – Fisher
    Commented Jun 21, 2018 at 16:22
  • 1
    I can see why you'd think the OP wants what you think. But you're wrong. You should remove your answer from here.
    – 3N4N
    Commented Jun 22, 2018 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.