Smart Wrap in Vim

I was wondering if Vim can use smart lines of code output, so that it retains the same indentation as the line which is indentation. I noticed this in some other text editor, such as an electronic text editor, and found that it helped me understand what I was looking for.

For example, instead of

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

he will look like

 <p> <a href="somelink"> This is a bogus link, used to demonstrate an example </a> </p> 
+71
vim word-wrap
Jul 30 '09 at 2:28
source share
8 answers

This feature was implemented on June 25, 2014 as a patch 7.4.338. The following are a few patches that refine this feature, the last of which is 7.4.354, so you need a version.

 :help breakindent :help breakindentopt 

Excerpts from vim help below:

 'breakindent' 'bri' boolean (default off) local to window {not in Vi} {not available when compiled without the |+linebreak| feature} Every wrapped line will continue visually indented (same amount of space as the beginning of that line), thus preserving horizontal blocks of text. 'breakindentopt' 'briopt' string (default empty) local to window {not in Vi} {not available when compiled without the |+linebreak| feature} Settings for 'breakindent'. It can consist of the following optional items and must be seperated by a comma: min:{n} Minimum text width that will be kept after applying 'breakindent', even if the resulting text should normally be narrower. This prevents text indented almost to the right window border occupying lot of vertical space when broken. shift:{n} After applying 'breakindent', wrapped line beginning will be shift by given number of characters. It permits dynamic French paragraph indentation (negative) or emphasizing the line continuation (positive). sbr Display the 'showbreak' value before applying the additional indent. The default value for min is 20 and shift is 0. 

For this, also showbreak parameter wherein the change amount will be showbreak characters you specified.

Configuration example

 " enable indentation set breakindent " ident by an additional 2 characters on wrapped lines, when line >= 40 characters, put 'showbreak' at start of line set breakindentopt=shift:2,min:40,sbr " append '>>' to indent set showbreak=>> 

Pay attention to the behavior

If you do not specify the sbr option, any showbreak any characters you put is attached to the indent. Removing sbr from the above example results in an effective indent of 4 characters; with this option, if you just want to use showbreak without extra indentation, specify shift:0 .

You can also showbreak negative shift, which will drag the showbreak and showbreak characters back into any available indentation space.

If the min value is specified, the offset number will be squeezed if the width of the terminal is showbreak , but showbreak characters showbreak always saved.

+46
Sep 24 '14 at 11:30
source share

There is a patch for this, but it has been delayed for many years, and the last time I checked it was not applied cleanly. See the entry “Correctly indent wrapped lines” in http://groups.google.com/group/vim_dev/web/vim-patches - I really want this to get to the trunk.

Update: this link seems to have bitrotted. Here is a more current version of the patch .

Update 2: merge upstream (from 7.4.345), so now you only need :set breakindent .

+33
Feb 07 '10 at 17:48
source share

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

 :set showbreak=>>> 

Example:

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

The real thing looks better than the sample code above, because Vim uses a different color for "→>".

+17
Jul 30 '09 at 2:39
source share

UPDATE: in June 2014, a patch supporting the breakindent option was merged with Vim (version 7.4.346 or later for better support).




You can also try :set nowrap , which will allow vim to display long lines by scrolling to the right. This may be useful for studying the general structure of the document, but may be less convenient for editing.

Other options close to those you are looking for are linebreak and showbreak . With showbreak you can change what is displayed in the left margin of the lines that are wrapped, but unfortunately it does not allow the variable to be indented depending on the current context.

+8
Jul 30 '09 at 2:36
source share

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

 :set formatoptions = tcqw :set textwidth = 50 gggqG 

If you have configured formatoptions , it’s better to just do:

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

What does it do:

 :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. 

Please note that this is not the same as the soft shell: it will wrap the lines both in the source file and on the screen (unless you save it, of course!). There are other parameters that can be added to formatoptions , which will be automatically formatted 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' 
+5
Jul 30 '09 at 7:20
source share
 :set smartindent :set autoindent 

I think you still have to use refund though

+3
Jul 30 '09 at 2:35
source share

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

 :%!xmllint --html --format 
+2
Jul 30 '09 at 2:32
source share

Macro Solution:




Edit:

gq{motion} works automatically, regardless of the variable "textwidth". This is simpler / better than using 80lBi^M for my macro.




If you have auto power on

 :set autoindent 

Then, by entering a return at the end of the line, the next line of the same amount will be indented. You can use this to get in linewraps in time if you want. The following macro uses this to automatically indent your text:

set register z to:

 gg/\v^.{80,}$^M@x (change 80 to whatever length you want your text to be) 

and set register x to:

 80lBi^M^[n@x (change 80 to whatever length you want your text to be) 

Then do

 @x 

to activate macros. After a few seconds, you will be completely printed in lines with a line of 80 characters or less.

Explanation:

Here's the macro expansion:

Part 1 (macro z):

 gg/\v^.{80,}$^M@x gg - start at the top of the file (this avoids some formatting issues) / - begin search \v - switch search mode to use a more generic regex input style - no weird vim 'magic' ^.{80,}$ - regex for lines that contain 80 or more characters ^M - enter - do the search (don't type this, you can enter it with ctrl+v then enter) @x - do macro x 

Part 2 (macro x):

 80lBi^M^[n@x 80l - move right 80 characters B - move back one WORD (WORDS include characters like "[];:" etc.) i^M - enter insert mode and then add a return (again don't type this, use ctrl+v) ^[ - escape out of insert mode (enter this with ctrl+v then escape) @x - repeat the macro (macro will run until there are no more lines of 80 characters or more) 

Warning:

  • This macro will be broken if there is a WORD that is 80 characters long.

  • This macro will not do smart things like indentation passing tags.

  • Use the lazyredraw (: set lazyredraw) parameter to speed this up.

+2
Feb 25 '14 at 0:17
source share



All Articles