In vim, it is now possible since patch 8.1.0579 using text properties (:help textprop
).
As the name suggests, a text property is a property that can be associated to text, and moves with it. Properties can be used for highlighting, but the concept is more general and could have other use cases (e.g. tagging for jumping).
To do highlighting with properties, firstly it is necessary to define a property type for the desired highlight group, and secondly to apply it to regions of text. A property type can be defined using the function prop_type_add()
call prop_type_add('error_property', {'highlight': 'Error'})
where 'error_property'
is a name for the new property, and the second argument defines what the property is, in this case it is the Error
syntax highlight group (for more options, please refer to |prop_type_add()|
).
Once a property type is defined, it can be assigned to a region of text using the function prop_add()
. An instance of property can be identified by an optional id. What I find handy is the fact that a property can span over multiple lines:
let l:options = {
\ 'id': l:id,
\ 'type': 'error_property',
\ 'end_lnum': l:end_line,
\ 'end_col': l:end_column,
\ }
call prop_add(l:start_line, l:start_column, l:options)
For additional options, please refer to |prop_add()
|.
Properties can be removed with prop_remove()
, either by id or by type, e.g.
" Remove property with ID equal to 1
call prop_remove({'id': 1})
" Remove all properties of type 'error_property' within lines l:start and l:end (inclusive)
call prop_remove({'type': 'error_property', 'all': v:true}, l:start, l:end)