2

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.

ib.
  • 27,830
  • 11
  • 80
  • 100
FrankBro
  • 229
  • 3
  • 12
  • I don't think this is currently possible. – Conner Sep 08 '12 at 23:39
  • That screenshot looks like a "show whitespace" editor mode - but I don't know what editor it is. – Dai Sep 09 '12 at 00:03
  • @FrankBro might seem that way, but I don't believe there's a way to display characters for something that doesn't exist. There is :help conceal and :help listchars, but these don't cover this particular example. – Conner Sep 09 '12 at 00:27
  • @FrankBro - Why would you like that? Is there some reason behind it? – Rook Sep 09 '12 at 00:41
  • @ldigas It helps me see better. Same reason why I use ">---" for tabs. – FrankBro Sep 09 '12 at 01:16

1 Answers1

6

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.

ib.
  • 27,830
  • 11
  • 80
  • 100
  • See also [my answer](http://stackoverflow.com/a/6997274/254635) to similar question "[Page feed symbol display in Vim](http://stackoverflow.com/q/5347522/254635)". – ib. Sep 09 '12 at 06:00