136

I want to use Vim's soft wrap capability (:set wrap) to wrap some code at 80 characters, regardless of my actual window width.

I haven't been able to find a way to do this yet - all the soft wrapping seems tied to the width of the window

  • textwidth and wrapmargin are both for hard wrapping (they insert newline characters into the file)
  • vertical splitting into multiple windows and using :vertical resize 80 (possibly with :set breakat= to allow breaks on any character) on one of them sort of works (even though it's a bit hackish), but breaks when using :set number as the line numbers take up a variable number of columns (depending on the file length) and these are part of the 80.

Is there any way to do this in vim? It doesn't look promising, according to other sources.

Right now my approximation is just to have /^.\{80}\zs.\+ as my default search so it's at least highlighted. I thought about adding a :syntax item for it, but that broke when it overlapped other syntax items, so I dropped that idea.

Machavity
  • 28,730
  • 25
  • 78
  • 91
rampion
  • 82,104
  • 41
  • 185
  • 301

5 Answers5

41

You could

  • set a large minimum width for the line numbers column via :set numberwidth=6 and
  • then you could resize your window with :set columns=86 (or with the mouse) to the proper size.

If you edit a file with a million lines in it, you may have trouble, but that's unlikely. You're wasting 6 columns of screen real estate this way too. So there are still all kinds of problems.

You can highlight past the 80th column using :match like it says here and here.

Beyond that I can't see any way to do this. Seems like it'd be a nice feature though.

Sridhar Sarnobat
  • 19,595
  • 12
  • 74
  • 93
Brian Carper
  • 66,618
  • 25
  • 158
  • 164
  • Unfortunately vim doesn't keep the columns setting if the window is resized. This leads to an unfortunate situation with tiling window managers where there's no good way to soft wrap to 80 columns if the window is too wide. – gravitation Feb 16 '13 at 00:24
  • This works, but it isn't sticky. I guess the next step is to setup an autocmd to keep the columns at the new desired size for the filetypes you want. – xer0x Oct 26 '15 at 21:51
  • 2
    This is the right answer in that it confirms you ca't do it. And I'm sure I'm not the only one who doesn't want to resize my terminal window just for this. I want lots more width for non-vim uses. – Sridhar Sarnobat Jul 22 '18 at 23:29
24

Try this:

set columns=80
autocmd VimResized * if (&columns > 80) | set columns=80 | endif
set wrap
set linebreak
set showbreak=+++

You can remove the if (&columns > 80) | if you always want 80 columns.

Community
  • 1
  • 1
eborisch
  • 556
  • 3
  • 3
  • On my iTerm terminal, this makes the window smaller. I'm not sure if that's an issue with my environment (I see you're doing something with automatic resizing), but otherwise this is not desirable. Thanks for the suggestion though. – Sridhar Sarnobat Jul 22 '18 at 23:32
  • The autocmd did work perfectly for me (using guake terminal). – mzcarlos Feb 05 '19 at 10:43
  • Works like a charm with Tmux + NeoVim in Alacritty. – meh Jun 25 '19 at 15:50
17

I don't have a solution to the soft wrap, but as for marking a column, as of Vim 7.3 (released 2010-08-15) :set colorcolumn=80 will highlight column 80. The color will depend on your syntax file.

See Vim 80 column layout concerns, :h colorcolumn.

Community
  • 1
  • 1
ftvs
  • 428
  • 1
  • 6
  • 14
  • Quotes are not valid URL so it makes sense. Anchor name should not contain invalid URL characters, SO should not fix errors of other site developers. – Shadow The Vaccinated Wizard Jun 12 '11 at 13:55
  • 1
    Shadow Wizard is referring to a previous revision of my answer in which I wonder about how to place single quotes in URLs using Markdown. Then [ib suggested](http://stackoverflow.com/revisions/6322217/2) using %27, which worked. Thanks, ib. – ftvs Jun 12 '11 at 14:24
  • Recently discovered how to only highlight columns past 80 ~ `:let w:eighty_column_match = matchadd('ColorColumn', '\%81v.\+', 100)` – rampion Oct 10 '13 at 15:12
  • The `colorwidth` thing is helpful for other reasons, so I'm glad you posted this. – Sridhar Sarnobat Jul 22 '18 at 23:29
3

Have you tried 'linebreak'?

        *'linebreak'* *'lbr'* *'nolinebreak'* *'nolbr'*
  'linebreak' 'lbr' boolean (default off)
        local to window
        {not in Vi}
        {not available when compiled without the  |+linebreak|
        feature}
If on Vim will wrap long lines at a character in 'breakat' rather
than at the last character that fits on the screen.  Unlike
'wrapmargin' and 'textwidth', this does not insert <EOL>s in the file,
it only affects the way the file is displayed, not its contents.  The
value of 'showbreak' is used to put in front of wrapped lines.
This option is not used when the 'wrap' option is off or 'list' is on.
Note that <Tab> characters after an <EOL> are mostly not displayed
with the right amount of white space.
glts
  • 19,167
  • 11
  • 65
  • 85
TK.
  • 23,367
  • 19
  • 60
  • 72
  • 3
    Hmm... but `breakat` is just a pattern (not a length), so I don't see how I could use this to force soft wrap at 80.... – rampion Apr 14 '10 at 15:32
  • Hmmm, I wonder if it's possible to make a regex that will match words whose total length (plus whitespace in between) are less than or equal to n chars. – Shane Creighton-Young Jun 21 '14 at 18:50
  • @ShaneCreighton-Young: `^.{,80}\b` would do for people who use soft/expand tabs. Note that's perl regex, not vim regex. – Mark K Cowan Feb 26 '16 at 12:16
  • @MarkKCowan Vim equivalent would be `^.\{,80}\zs` – tejasvi88 Feb 27 '21 at 02:36
  • @ShaneCreighton-Young Actually I just tried that and it seems `breakat` is just a plain string of characters instead of a regex pattern. – tejasvi88 Feb 27 '21 at 02:55
-1

There is no good way to do it. We can hack a makeshift setlocal softwrap with autocmd if we modify @eborisch answer. If we resize every time we enter a buffer, and we resize to a particular length when the local variable softwrap is set, we get the desired behaviour.

Let's suppose that we want to soft wrap to 80 columns, we can write the following in .vimrc.

augroup softwrap
    autocmd VimResized * if (exists('b:softwrap') && &columns > 80) | set columns=80 | endif
    autocmd BufEnter * set columns=999
augroup END

To turn on the mode for a particular buffer, use the following commands:

let b:softwrap=1
set columns=80
durum
  • 2,904
  • 1
  • 23
  • 29