tags:

views:

2422

answers:

6
+11  Q: 

Smart Wrap in Vim

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 some other text editor, such as e-text editor, and found that it helped me to comprehend what I'm looking at easier.

For example rather than

<p>
    <a href="http://www.example.com"&gt;
        This is a bogus link, used to demonstrate
an example
    </a>
</p>

it would appear as

<p>
    <a href="somelink">
        This is a bogus link, used to demonstrate
        an example
    </a>
</p>
+1  A: 

If your HTML is sufficiently well formed, running it through xmllint might help:

:%!xmllint --html --format
scrible
+3  A: 
:set smartindent
:set autoindent

I think you still have to use a return though

Cfreak
This will only work for writing new code, not reading existing one...
scrible
+3  A: 

I don't think there is a way to configure vim to do that, but you might try :set nowrap which will allow vim to display long lines by scrolling to the right. This may be useful for examining the overall structure of a document, but can be less convenient for actually editing.

The options closest to what you're looking for are linebreak and showbreak. With showbreak, you can modify what is displayed at the left margin of lines that are wrapped, but unfortunately it doesn't allow a variable indent depending on the current context.

Greg Hewgill
+2  A: 

I don't think it's possible to have exactly the same indentation, but you can still get a better view by setting the 'showbreak' option.

:set showbreak=>>>

Example:

<p>
    <a href="http://www.example.com"&gt;
        This is a bogus link, used to demonstrate
>>>an example
    </a>
</p>

The real thing looks better than the example code above, because Vim uses a different colour for '>>>'.

too much php
+2  A: 

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 <p>
            " isn't moved onto the same line as your <a...).
:set tw=50  " Set the textwidth up to wrap at column 50
gg          " Go to the start of the file
gq{motion}  " Reformat the lines that {motion} moves over.
G           " Motion that goes to the end of the file.

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'
Al
+2  A: 

There is a patch for this, but it's been lingering for years and last time I checked did not apply cleanly. See the "Correctly indent wrapped lines" entry in http://groups.google.com/group/vim_dev/web/vim-patches -- I really wish this would get in the mainline.

ergosys