0

I use Vim for presentations and note-taking.

I would like to be able to highlight a portion of text in a buffer , split the window, go to the bottom of the buffer, and carry on typing the notes, whilst keeping the highlight on.

If I use , say , shift-V to highlight a line, then the highlight disappears when I resume the note-taking.

Of course, I will want to clear the highlights at some point.

How can I achieve the persistent highlight described above ?

1 Answer 1

1

If by "persisted" you mean only in a current vim session (not stored with text file), then :h textprop API might be what you want.

Here is the proof of the concept:

func! Markline(line)
    " check if 'markline' text property exists
    if empty(prop_type_get("markline"))
        " define Markline highlight group to be used for ... highlighting
        hi! Markline guibg=#C0C0C0 guifg=#000000 ctermbg=253 ctermfg=0
        " define 'markline' property that should use 'Markline' highlight
        call prop_type_add('markline', {'highlight': 'Markline'})
    endif
    " add 'markline' property for a line passed into function with a length of the passed line
    call prop_add(a:line, 1, {'length': len(getline(a:line)), 'type': 'markline'})
endfunc
"mapping to mark current line
nnoremap <space>m :call Markline(line('.'))<CR>
"mapping to remove marks from ALL lines
nnoremap <space>d :call prop_remove({"id": "markline"})<CR>

It is possible to make it work for an arbitrary visual selection or a text objects but this minimal example should be enough to get started.

enter image description here

Your Answer

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

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