rembremading
rembremading

Reputation: 43

Display some special character as a linebreak in Vim

I would like to display some (arbitrary) special character as linebreak <CR> in vim. So far I tried misusing (certainly extreme misuse:) the non-breakable space typing

:set list listchars=nbsp:<CR>

which does not work, seemingly because the command does not accept the <CR>. Is there anything which I can use here for <CR>? \r didn't work either.

Note that I don't want to edit the text file. The goal is to have blocks of lines (some related code) treated as a single line with respect to vim actions but displayed as multiple lines. The special character (to be defined) would be used only to save this block structure in the file replacing the linebreak \r in these cases.

Upvotes: 2

Views: 841

Answers (3)

ib.
ib.

Reputation: 28964

Given the wider context of the problem that you have provided in a later comment, I would suggest the following solution. Group dependent lines of code in folds by indentation, language’s syntax, or markers. All of these three methods are automatic and do not require manual creation of folds. Which one to choose depends on the language you use. See :help foldmethod, and feel free to comment this answer if you need any help with folding.

Unless the syntax of the language you use has extensive support in Vim, the most convenient methods would be using fold markers or defining a custom expression to calculate fold level of each line. The former method implies surrounding every group of lines to fold with special text markers (which could be enclosed in a comment not to break the syntax rules of the language). By default, those markers are {{{ and }}}; see :help fold-marker and :help foldmarker to find out how to change them. Use

:set foldmethod=marker

to enable this mode of folding.

Defining an expression to calculate fold level for every line is an even more flexible method. It allows to use any logic (that can be expressed in Vimscript) to determine the fold level. For example, to fold groups of lines that start with a single space use the following settings:

:set foldmethod=expr
:set foldexpr=getline(v:lnum)[0]=='\ '

See :help fold-expr for further details on customizing the fold expression.

When the lines that depend on each other are grouped into folds, you can easily pass the contents of any particular fold to a filter program. Move the cursor to a line inside a target fold, then type [zV]z to select the entire fold, followed by !, and enter the command to run. To save typing, you can define the mapping

:nnoremap <leader>z [zV]z!

If the command is always the same, you can include it in the mapping:

:nnoremap <leader>z [zV]z!cat -n<cr>

Substitute the cat -n portion above—my example command—with the appropriate command in your case.

Upvotes: 1

rembremading
rembremading

Reputation: 43

My solution, in the end, was to insert non-breaking spaces and linebreaks in the respective places in the file. :set list listchars=nbsp:$ can then be used to display these 'special linebreaks'. Their presence allows interpreting code to identify the blocks of lines separated by this combination as related lines.

Of course this doesn't answer the question. The answer, according to my best knowledge now, is that neither :set list nor :wrap can be used to achieve this.

Upvotes: 0

sa125
sa125

Reputation: 28971

I think you might want check out this vimcasts episode. May not be exactly what you want, but could get you there. Good luck!

Upvotes: 0

Related Questions