Highlight specific lines

4.4
(41)

I often pair program remotely with colleagues over a screen share. (Shout out to Tuple, hands down the best pair programming app.) When I’m driving and sharing my screen, I often want to call attention to a certain line to my remote pair. Usually, I just press shift-V to visually select the current line. This does the job, but the highlighting disappears as soon as I move my cursor.

I recently added two new mappings that let me highlight a specific line or lines. The highlights persist as I move around the screen, allowing me to draw attention to a line or multiple lines. Whether pairing or just working on my own, having a specific area highlighted temporarily can be of great benefit. Here’s how I do it:

" define line highlight color
highlight LineHighlight ctermbg=darkgray guibg=darkgray

" highlight the current line
nnoremap <silent> <Leader>l :call matchadd('LineHighlight', '\%'.line('.').'l')<CR>

" clear all the highlighted lines
nnoremap <silent> <Leader>c :call clearmatches()<CR>

With these mappings added to my .vimrc, I can press <Leader>l (mnemonic is “line”) to highlight the current line. I can move to another line and repeat to highlight that line as well. And then when I’m done, I can clear all the highlights in the buffer by pressing <Leader>c (mnemonic is “clear”).

Check out the short screencast below where I demonstrate this in action. Do you pair program much with Vim? I’d be curious to hear what setup you use and what configuration, mappings, or plugins aid you when doing so. Email us or post a comment on the post.

How useful was this tip?

Average rating 4.4 / 5. Vote count: 41

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

90 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

3 thoughts on “Highlight specific lines”

  1. Joyce, that is much harder to implement I think. Matchadd essentially returns a number as the id of the matched line(s). So, you have to write a function to remember and delete those specific lines with the match-id.

    Bottom line: Not possible as far as I understand.

Leave a reply

Your email address will not be published. Required fields are marked *