vim line wrap with indent

21

4

Vim/gvim will wrap long lines like this:

000000000000000000000000000000000000|
00000000000000                      |
    11111111111111111111111111111111|
111111111111111111                  |
    22222222222222222222222222222222|
222222222222222222                  |
        3333333333333333333333333333|
3333333333333333333333              |

Is there a way to get Vim to wrap those lines like this instead:

000000000000000000000000000000000000|
 00000000000000                     |
    11111111111111111111111111111111|
     111111111111111111             |
    22222222222222222222222222222222|
     222222222222222222             |
        3333333333333333333333333333|
         3333333333333333333333     |

I want the wrapped line to start a little past the indent of where that line started. (Just to be clear, I'm talking about wrap, not textwidth.)

I want the indentation of the line to be considered in the wrapping of that line so that the code structure isn't hidden by wrapped lines.

retracile

Posted 2009-11-19T16:46:58.463

Reputation: 2 586

Answers

18

UPDATE: This functionality landed in vim 7.4.338, though you'll want 7.4.354 or later.


So apparently this requires a patch to Vim. There is a patch by Vaclav Smilauer from back in 2007. I updated the patch to work with Vim 7.2.148 from Fedora 11. But it does seem to do the job.

retracile

Posted 2009-11-19T16:46:58.463

Reputation: 2 586

1As of today (June 25, 2014) the breakindent patch is officially part of Vim 7.4 -- it is added by patch 338 and 345. – Heptite – 2014-06-25T21:21:13.720

@Heptite Thank you for the heads-up! – retracile – 2014-07-03T02:40:08.273

1

As Fedora releases updates to Vim, I'm updating the patch. See https://retracile.net/wiki/VimBreakIndent for the latest.

– retracile – 2010-11-23T04:16:16.720

Bonus points to someone who can explain how to get this to work with MacVim. – donut – 2011-03-29T20:01:15.250

Where is the patch? That site doesn't link to any I can see – puk – 2012-02-23T11:57:54.470

@puk: Sorry, a major software upgrade had changed some link locations. I've fixed those now. – retracile – 2012-02-24T17:17:44.037

Le awesome! Thanks a lot, works like a charm on top of vim-7.3.944 on Fedora 19. For those like me who didn't know: Here's how to patch and recreate an RPM.

– nidi – 2013-07-08T00:22:10.627

14

In your .vimrc:

set wrap               " soft-wrap lines

" requires +linebreak compile-time option (not in the 'tiny' and 'small' builds); check your :version
set showbreak=----->   " prefix for soft-wrapped lines (no actual line break character)
"set linebreak          " soft-wrap lines only at certain characters (see :help breakat)

" If you like line numbers, you may want this instead:
"set number
"set showbreak=------>\  " line up soft-wrap prefix with the line numbers
"set cpoptions+=n        " start soft-wrap lines (and any prefix) in the line-number area

Or just type :set showbreak=-----> in any session.

For reference, my research trail (Vim 6.2): :help 'wrap' -> :help 'linebreak' -> ( :help 'showbreak' -> :help 'cpoptions', :help 'breakat')

Chris Johnsen

Posted 2009-11-19T16:46:58.463

Reputation: 31 786

1Very cool, but not really what I meant. You took it a bit more literally than I intended, so my fault there. I'll see if I can clarify. – retracile – 2009-11-20T15:03:35.813

2

The solution to your question can be achieved setting two parameters in your vimrc:

To break the lines with the same indentation: set breakindent

And one space for indenting soft-wrapped lines: let &showbreak=' '

It will work this way:

111111111111111|
 11111111111111|
 11111         |
222222222222222|
 222222222222  |
333333333333333|
 33333         |

aturegano

Posted 2009-11-19T16:46:58.463

Reputation: 341

0

Not the perfect answer you're looking for, but here's what I do to get around vim's lack of proper indentation.

First, download Nathanael Kane's indent guide (optional) as it better indicates what indentation level you are at, even to the point where you can set tabwidths to be only 2 spaces!

Second, map a key combination to toggle word wrap. I have mine set to 'r' for 'wrap' ('w' is for saving) like so

:noremap <leader>r :set nowrap! <CR>

Then I always have word wrap off, since it is more aesthetically pleasing, and I quickly toggle it on if I need to edit the line.

puk

Posted 2009-11-19T16:46:58.463

Reputation: 607

0

The foul "workaround" I currently use, is to have a fixed but big indentation in the prefix. It looks good, as long as this fixed indentation is bigger than the wrapped line's indentation, which is the case most of the time.

" 'showbreak'   : string to put before wrapped screen lines
set sbr=\ \ \ \ \ \ \ \ \ \ \ \ \ \|\ 

Aaron Thoma

Posted 2009-11-19T16:46:58.463

Reputation: 550