Soft wrap at 80 characters in Vim in window of arbitrary width

41,595

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

Combining eborisch's answer with some other answers I found here and things I had to work around, I came up with the following two-part solution:

This first part makes it easier to edit text with long lines:

" Allow enabling by running the command ":Freeform", or <leader>sw
command! Softwrap :call SetupSoftwrap()
map <Leader>sw :call SetupSoftwrap() <CR>

func! SetupFreeform()
  " Use setlocal for all of these so they don't affect other buffers

  " Enable line wrapping.
  setlocal wrap
  " Only break at words.
  setlocal linebreak
  " Turn on spellchecking
  setlocal spell

  " Make jk and 0$ work on visual lines.
  nnoremap <buffer> j gj
  nnoremap <buffer> k gk
  nnoremap <buffer> 0 g0
  nnoremap <buffer> $ g$

  " Disable colorcolumn, in case you use it as a column-width indicator
  " I use: let &colorcolumn = join(range(101, 300), ",")
  " so this overrides that.
  setlocal colorcolumn=

  " cursorline and cursorcolumn don't work as well in wrap mode, so
  " you may want to disable them. cursorline highlights the whole line,
  " so if you write a whole paragraph on a single line, the whole
  " paragraph will be highlighted. cursorcolumn only highlights the actual
  " column number, not the visual line, so the highlighting will be broken
  " up on wrapped lines.
  setlocal nocursorline
  setlocal nocursorcolumn
endfunc

With this alone you can get decent text wrapping for writing something like markdown, or a Readme.

As noted in other answers, getting wrapping at an exact column width requires telling vim exactly how many columns there are, and overwriting that each time vim gets resized:

command! -nargs=? Draft :call SetupDraftMode(<args>)
func! SetupDraftMode()
  " I like 80 columns + 4 for line numbers
  set columns=84
  autocmd VimResized * if (&columns > 84) | set columns=84 | endif

  :Softwrap
endfunc

There are still a couple of problems with this:

  • vim won't clear the screen outside of the columns you specify after calling set columns, and I can't figure out how to tell it to, so ideally you should call this immediately after opening vim
  • vim shows a prompt with the version number and some helpful commands when you open it, so these won't be cleared. You can add set shm+=I to disable that prompt
  • You can't open any vertical splits, because then both splits will be ~40 column. You would need to set columns to 2x your desired width and then always have a split open.
  • My vimscript is awful, but ideally someone could modify the Draft function above to take a column width as an argument, or use a global variable (g:explicit_vim_width?) that can be set manually if your window size changes.
Share:
41,595
rampion
Author by

rampion

Mathematician, programmer, and researcher; with interests in algorithmic design, software engineering theory, and massively parallel computing. I'm also a keyboard junkie and an aspiring language nerd.

Updated on May 12, 2022

Comments

  • rampion
    rampion almost 2 years

    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.

    • thethinman
      thethinman over 14 years
      Actually, how did you set up a default search?
    • fbence
      fbence over 2 years
      I usually end up with just doing :vsplit and resizing the right split so the left is the size I want ...
  • rampion
    rampion about 14 years
    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....
  • Shadow The Kid Wizard
    Shadow The Kid Wizard almost 13 years
    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.
  • ftvs
    ftvs almost 13 years
    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 using %27, which worked. Thanks, ib.
  • gravitation
    gravitation about 11 years
    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.
  • rampion
    rampion over 10 years
    Recently discovered how to only highlight columns past 80 ~ :let w:eighty_column_match = matchadd('ColorColumn', '\%81v.\+', 100)
  • Shane Creighton-Young
    Shane Creighton-Young almost 10 years
    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.
  • xer0x
    xer0x over 8 years
    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.
  • Mark K Cowan
    Mark K Cowan about 8 years
    @ShaneCreighton-Young: ^.{,80}\b would do for people who use soft/expand tabs. Note that's perl regex, not vim regex.
  • Sridhar Sarnobat
    Sridhar Sarnobat almost 6 years
    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
    Sridhar Sarnobat almost 6 years
    The colorwidth thing is helpful for other reasons, so I'm glad you posted this.
  • Sridhar Sarnobat
    Sridhar Sarnobat almost 6 years
    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.
  • mzcarlos
    mzcarlos about 5 years
    The autocmd did work perfectly for me (using guake terminal).
  • meh
    meh almost 5 years
    Works like a charm with Tmux + NeoVim in Alacritty.
  • Suuuehgi
    Suuuehgi almost 4 years
    This just transforms my files into gibberish.
  • tejasvi88
    tejasvi88 about 3 years
    @MarkKCowan Vim equivalent would be ^.\{,80}\zs
  • tejasvi88
    tejasvi88 about 3 years
    @ShaneCreighton-Young Actually I just tried that and it seems breakat is just a plain string of characters instead of a regex pattern.