Smart Wrap in Vim

后端 未结 8 1484
离开以前
离开以前 2020-12-02 06:28

I have been wondering if Vim has the capability to smart wrap lines of code, so that it keeps the same indentation as the line that it is indenting. I have noticed it on som

8条回答
  •  旧巷少年郎
    2020-12-02 07:05

    The only way I know of that you could do this would be to use a return character (as mentioned by Cfreak) and combine the textwidth option with the various indentation options. If your indent is configured correctly (as it is by default with the html syntax I believe, but otherwise see the autoindent and smartindent options), you can:

    :set formatoptions = tcqw
    :set textwidth = 50
    gggqG
    

    If you have any customisation of the formatoptions setting, it may be better to simply do:

    :set fo += w
    :set tw = 50
    gggqG
    

    What this does:

    :set fo+=w  " Add the 'w' flag to the formatoptions so 
                " that reformatting is only done when lines
                " end in spaces or are too long (so your 

    " isn't moved onto the same line as your

    Note that this is not the same as a soft wrap: it will wrap the lines in the source file as well as on the screen (unless you don't save it of course!). There are other settings that can be added to formatoptions that will auto-format as you type: details in :help fo-table.

    For more information, see:

    :help 'formatoptions'
    :help fo-table
    :help 'textwidth'
    :help gq
    :help gg
    :help G
    :help 'autoindent'
    :help 'smartindent'
    

提交回复
热议问题