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

前端 未结 11 552
刺人心
刺人心 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 17:59

    To split long lines in the complete document without removing already present line breaks, use:

    :set formatoptions+=w
    :set tw=80
    gggqG
    
    0 讨论(0)
  • 2021-01-29 18:00

    First set your vim so that it understands that you want 80 characters:

    :set tw=80
    

    then, hilight the line:

    V
    

    and make vim reformat it:

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

    For solid lines of text highlight the area using v in normal mode, then press

    :s/\v(.{80})/\1\r/g
    

    This will add a newline at the end of every 80th character.

    :s/       replaces within the current select
    \v        uses regular expressions
    (.{80})   selects 80 characters & placed them into group one
    \1\r      replaces group one with group one and a newline
    
    0 讨论(0)
  • 2021-01-29 18:01

    I manually break up the long line at place. I think where the main point is by pressing "r" (normal mode) then press .

    This will make delete a character where cursor is. So remember to do it at the space before the word you want to make a new line, else you will have to insert the missing character.

    I just don't know how to break the line and shift it down 2 line space so that there will be space between the 2 lines.

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

    As a quick and nasty, maybe try the following map:

    map q 080lwbels<CR><ESC>
    

    which says:

    • start a 0th position of line,
    • move to 80th char to the right,
    • go to beginning of next word,
    • go back to previous word,
    • go to end of current word,
    • go one char right, and
    • substitute a CR for that char.

    Then hitting q and CR will break the line up into chunks on the word boundary.

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

    If you're on *nix you probably have fold available.

    Select the region you want using v, then you can break on spaces at width 80 using:

    !fold --spaces --width=80

    This is esentially the same as using gq.

    However, if you just want to break at character 80 and not be restricted to whitespaces you can use:

    !fold --width=80

    If you want it with one keystroke just set a mapping - I've used

    vmap <f1> !fold --width=80<CR>

    0 讨论(0)
提交回复
热议问题