Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto wrap lines in vim without inserting newlines

Tags:

vim

How do set vim to wrap text without inserting newlines?

Basically:

  • I want to have a max width, say 80 lines
  • As I type out a paragraph, if a word passes the 80 line mark, it should wrap the entire word (no splitting the word)
  • If I type more than a couple of lines, it should still be wrapping
  • If I save the file, I shouldn't see any line breaks unless I explicitly hit Enter when typing in edit mode

I can get some of this behavior with:

:set textwidth=80
:set wrap

Except this will insert newlines, and I don't want it to insert newlines. I already tried this but it doesn't work.

like image 865
antimatter Avatar asked Apr 30 '16 03:04

antimatter


2 Answers

If your goal, while typing in insert mode, is to automatically soft-wrap text (only visually) at the edge of the window:

set number # (optional - will help to visually verify that it's working)
set textwidth=0
set wrapmargin=0
set wrap
set linebreak # (optional - breaks by word rather than character)

If your goal, while typing insert mode, is to automatically hard-wrap text (by inserting a new line into the actual text file) at 80 columns:

set number # (optional - will help to visually verify that it's working)
set textwidth=80
set wrapmargin=0
set formatoptions+=t
set linebreak # (optional - breaks by word rather than character)

If your goal, while typing in insert mode, is to automatically soft-wrap text (only visually) at 80 columns:

set number # (optional - will help to visually verify that it's working)
set textwidth=0
set wrapmargin=0
set wrap
set linebreak # (optional - breaks by word rather than character)
set columns=80 # <<< THIS IS THE IMPORTANT PART

The latter took me 2-3 significant Internet-scouring sessions to find from here (give 'er a read - it's very well-written): https://agilesysadmin.net/how-to-manage-long-lines-in-vim/

like image 95
Acryce Avatar answered Sep 28 '22 07:09

Acryce


Reducing the width of the window to circa 80 characters, set wrap, and set linebreak should satisfy all your requirements.

See :help 'wrap' and :help 'linebreak'.

like image 37
romainl Avatar answered Sep 28 '22 09:09

romainl