Word Wrap in Vim (preserving indentation)

后端 未结 4 1296
南旧
南旧 2021-01-31 00:53

I was just looking at this post which describes how to wrap entire words in vim. The accepted solution was this:

:set formatoptions=l
:set lbr

4条回答
  •  有刺的猬
    2021-01-31 01:23

    I agree with the answer that says 'showbreak' is the best option. Showbreak does not typically allow you to put nonprinting characters (e.g., spaces or tabs) into the showbreak string, so as typically used it will just give you an indicator along left margin, i.e., no real indent. This isn't great, since main goal of OP, I think, is to give wrapped lines an indent keep them from cluttering left margin area and looking like lines of their own.

    So one way to add an (ugly) indent using showbreak is to just use a lot of characters, .e.g, ":set showbreak=>--------------->". This results in something that looks like this:

     *Inside of window                        *Outside of window
    |---------------------------------------|    
    |\t\tthis is a like of text that will   |
    |>--------------->wrap here             |
    |\t\tcan you see the wrap               |
    |                                       |
    |---------------------------------------|
    

    A better alternative might be to make use of nonbreaking space characters (assuming your instance of Vim is unicode enabled), each of which can be entered into the showbreak string using the key sequence of ctrl-v,160. That way you can enter a showbreak string that is blank at the left side and appear to be a true indent. E.g., ":set showbreak=. . . . . . . . . . >>" where each '.' in the command is actually a nonbreaking space character entered by pressing ctrl-V,160. That way you end up with a wrap that is nicely indented, like this:

     *Inside of window                        *Outside of window
    |---------------------------------------|    
    |\t\tthis is a like of text that will   |
    |            >>wrap here                |
    |\t\tcan you see the wrap               |
    |                                       |
    |---------------------------------------|
    

    You still don't have any ability to vary the level of indent according to indent of previous line, but at least you get clean indent of wrapped lines without lots of visual clutter along left margin of window. There could still be confusion if indent of a wrapped line is less than that of the beginning of an actual line, but this could perhaps be avoided by making the showbreak "indent" quite large (i.e., greater than any indent commonly found in your code) but still small enough that it provides enough space for legible wrapping of the text. For many uses I think a showbreak indent of 40 or 50 spaces would do this pretty well.

提交回复
热议问题