In VIM, how do I break one really long line into multiple lines?

前端 未结 11 553
刺人心
刺人心 2021-01-29 17:53

Say I have a super long line in the VIM editor (say around 300+ characters). How would I break that up into multiple lines so that the word boundaries roughly break at 80 chara

相关标签:
11条回答
  • 2021-01-29 18:14

    Vim does this very easy (break lines at word boundaries).

    gq{motion} % format the line that {motion} moves over
    {Visual}gq % format the visually selected area
    gqq        % format the current line
    ...
    

    I'd suggest you check out :help gq and :help gw.

    Also setting textwidth (tw) will give you auto line break when exceeded during typing. It is used in gq too, though if disabled gq breaks on window size or 79 depending on which comes first.

    :set tw=80
    

    By setting format options to include text width vim will automatically break at the tw setting.

    :set fo+=t
    
    0 讨论(0)
  • 2021-01-29 18:15

    I manually inserted '\' (and then CR / tab to format) in each LONGLINE after the last whitespace but before the 80 column. That is to say:

    1 this is a long, long, line
    

    now looks like

    1 this is a long, \
            long line
    

    and compiles normally.

    0 讨论(0)
  • 2021-01-29 18:18

    This is not really related to VIM, but you could use the fmt program as in

    $ fmt myfile
    
    0 讨论(0)
  • 2021-01-29 18:18

    fmt also works quite well in VIM, and will change something like this:

    md5: A55B4EEB6FC24B2377A31A37C490D236 | sha1: BB4E344C5F271BF8B76B3FDC626A26627E97F453 | sha256: 7A386ADBBF9CE26E892F044128F21C70B13695CE7931456C12868776BC680582 | sha512: DECB7B5B66FA5A272FDAB56CD4B6639CA216B30418E050C16A3821FE2FBF9B90C3DC35671AED44B0AE8C5471FCD6393D4955237E1497DF2CA2B427615FEE7B32

    To a more favorable HASH:

    |

    It will put the type of hash, hash number, and pipe all on their own respective lines successively...

    Just visually select the text you need, then:

    !fmt -1

    0 讨论(0)
  • 2021-01-29 18:21

    I needed to reformat an entire file rather than one line. As Wernsey points out, I could have used 'fmt', but the following sequence in vim did the trick also (borrowing from the various answers here):

    <ESC>
    :setl tw=80 fo=t
    1GVGgq
    
    0 讨论(0)
提交回复
热议问题