The listings package in LaTeX has an option to collapse multiple empty lines into a single empty line when typesetting code lists. Today, there was a question on TeX.se how to do something similar when using the minted package. Since the vim module uses the same principle as the minted package, I wondered how one could collapse multiple empty lines into a single line?
One of the fetures of the vim module is that you can source an arbitrary vimrc file before processing the code through the vim editor to generate syntax highlighted code. This feature makes it possible to delegate the task to collapsing multiple blanks lines into a single blank line to vim, the editor. Since the vim module first writes the source code in a file with extension .tmp, the following vimrc snippet will collapse all multiple blank lines into a single blank line whenever a .tmp file is loaded:
au BufEnter *.tmp %s/\(^\s*\n\)\{2,\}/\r/ge | w
Use this inside the vim module as follows (example also available on github):
\usemodule[vim]
\startvimrc[name=collapse]
au BufEnter *.tmp %s/\(^\s*\n\)\{2,\}/\r/ge | w
\stopvimrc
\definevimtyping[CPPtyping][syntax=cpp, vimrc=collapse]
\starttext
\startCPPtyping
i++;
i++;
i--;
\stopCPPtyping
\stoptext
Agreed, this is not as simple as the extralines=1 option in the listings package. But, it is not too complicated when you consider the fact that I had not thought about this feature at all when I wrote the vim module.
Nice trick!
I’m in the middle of a rethinking/reconfiguring of my
~/.vimrcand~/.vim/directory (just posted something about it on my blog), and this can come handy.Thanks for sharing!