FrankBro
FrankBro

Reputation: 229

Can empty lines be displayed as something else in Vim?

I want my Vim to display empty lines as a string of ### characters, like so:

Desired display of empty lines

I would like it to be work similarly to how I replace my tabs char to >--- with set listchars=tab:>-. Just display it that way, not actually insert them.

Also, it would be great if it could adapt to size of my terminal.

Upvotes: 2

Views: 607

Answers (1)

ib.
ib.

Reputation: 29014

The desired effect can be achieved via folding. If we create one-line folds separately containing empty lines of a buffer, they all will be marked out as folded. The only thing left will be to customize the highlighting accordingly.

First of all, we need to automatically create the folds. For this purpose, we can switch folding to the expr method and then set the foldexpr option to evaluate to a non-zero value for empty lines only:

:setl foldmethod=expr
:setl foldexpr=empty(getline(v:lnum))

What we should do next for the trick to work out is to make those folds close automatically in order to trigger the folding highlighting:

:setl foldminlines=0
:setl foldlevel=0
:set foldclose=all

Finally, to repeat a custom character in the folding line, we just empty the text displayed for a closed fold, and change the filling character:

:setl foldtext=''
:set fillchars+=fold:#

Combining the above commands in one function for convenience, we obtain the following:

function! FoldEmptyLine()
    setl foldmethod=expr
    setl foldexpr=empty(getline(v:lnum))
    setl foldminlines=0
    setl foldlevel=0
    set foldclose=all
    setl foldtext=''
    set fillchars+=fold:#
endfunction

The downside of this trick, of course, is that it interferes with the usual applications of folding, and cannot be easily used without modifications if the user extensively relies on folding for other purposes.

Upvotes: 6

Related Questions