Auto wrap lines in vim without inserting newlines - 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.

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/

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

Related

How to keep white spaces when to format text with fixed width?

To set line to be 100 characters for the edited text as below.
:set textwidth=100
gggqG
I found that all lines set 100 characters but all the white spaces deleted.
How to keep all white spaces at the same time to set width 100 for every line?
Vim's built-in formatter keeps multiple spaces inside text; these only get truncated when Vim breaks the line right there.
As for indent (whitespace at the beginning of the line), the first (or with 2 in 'formatoptions', second) line determines the indent for the reformatted block.
In general, this setting makes sense. If you have special formatting needs, you can instruct Vim to either use an external formatter that does the job (:help 'formatprg'), or write your own in Vimscript (:help 'formatexpr').

how to set line width in gvim to be 80 characters even if maximize the gvim window?

I am in win7+gvim7.4 ,how to set line width in gvim?
In my _vimrc file:
set nowrap
set tw=80
set formatoptions=tnmM
when i open a file with gvim ,
1. the window is small window ,and 80 character in one line.
2. when Maximize windows to fill the entire gvim screen,i found that 157
characters can be displayed in one line.
how can i set 80 character in one line envn if you Maximize gvim window ?
where can i change the 157 characters in one maximized gvim window?
I think you are misunderstanding the implication of setting tw. Here's what :help tw shows:
'textwidth' 'tw' number (default 0)
local to buffer
{not in Vi}
Maximum width of text that is being inserted. A longer line will be
broken after white space to get this width. A zero value disables
this. 'textwidth' is set to 0 when the 'paste' option is set. When
'textwidth' is zero, 'wrapmargin' may be used. See also
'formatoptions' and |ins-textwidth|.
When 'formatexpr' is set it will be used to break the line.
NOTE: This option is set to 0 when 'compatible' is set.
The value of tw simply indicates the column number at which a break occurs when you are in insert mode. It does not change how a line is displayed when it is longer than the value of tw.
Lines can become longer than tw by various means: pasting from the clipboard, pasting from a register, joining two lines, etc. None of those operations are limited by the value of tw.
In case you worry that the window width is too large in maximized GVIM, you can create a vertical dummy window next to it, e.g. with :bot vnew | wincmd p.
My LimitWindowSize plugin offers a command to reduce the current window size by placing an empty padding window next to it.
Other plugins like DistractFree - An WriteRoom/DarkRoom/OmniWrite like plugin go further and add padding on all sides around a single centered window (and turn off embellishments), so that you can focus on the current editing task.

how do I prevent vim from auto-wrapping at column 80

As I'm writing code, when the cursor reaches column 80, vim automatically wraps the text by inserting a newline character. How do I prevent vim from doing so? Is there any setting that am missing?
The auto-wrapping is defined by set wrap and this option depends on the width of your window/screen, but it does not insert a newline character into the file.
The column 80 is defined by set tw=80 this will change the text by adding new linebreaks for long lines.
To check details, you can:
:h 'wrap'
:h 'tw'
To disable auto-wrapping, you could:
:set nowrap
To disable long line auto broken, you can:
:set tw=0
0 is default.
Just found that set tw=0 works. It doesn't wrap lines by inserting a new line character at a predefined limit.
Added that set to vimrc to make it permanent.

How to stop line breaking in vim

I like that the long lines are displayed over more than one terminal line; I don’t like that vim inserts newlines into my actual text. Which part of .vimrc I should change?
Use
:set wrap
To wrap lines visually, i.e. the line is still one line of text, but Vim displays it on multiple lines.
Use
:set nowrap
To display long lines as just one line (i.e. you have to scroll horizontally to see the entire line).
I like that the long lines are displayed over more than one terminal line
This sort of visual/virtual line wrapping is enabled with the wrap window option:
:set wrap
By default this will wrap at the first character that won't fit in the window. This means it will wrap in the middle of a word if that's where the window boundary lies. To change it to wrap on word boundaries, you can also:
:set linebreak
This will cause wrap to only wrap at the characters in the breakat setting, which defaults to space, tab, and small set of punctuation characters.
:set breatat
breakat= ^I!#*-+;:,./?
I don’t like that vim inserts newlines into my actual text.
To turn off physical line wrapping, clear both the textwidth and wrapmargin buffer options:
:set textwidth=0 wrapmargin=0
I'm not sure I understand completely, but you might be looking for the 'formatoptions' configuration setting. Try something like :set formatoptions-=t. The t option will insert line breaks to make text wrap at the width set by textwidth. You can also put this command in your .vimrc, just remove the colon (:).
:set tw=0
VIM won't auto-insert line breaks, but will keep line wrapping.
Use :set nowrap .. works like a charm!
You may find set linebreak useful; with set wrap on this will wrap but only cutting the line on whitespace and not in the middle of a word.
e.g.
without linebreak the li
ne can be split on
a word
and
with linebreak on the
line will be
split on
whitespace only
set formatoptions-=t Keeps the visual textwidth but doesn't add new line in insert mode.
Its strange that such a simple setting would require this amount of 'hocus-pocus' to work.
To answer your question now, for me it seemed to work with the combination of the following:
:set wrap linebreak nolist
(this seems to prevent existing lines from breaking, just wrap.)
AND
set formatoptions=l
(this prevents new/edited lines from breaking, while += does not do it for me as other settings/plugins seem to find space and add their own options which override mine.)
If, like me, you're running gVim on Windows then your .vimrc file may be sourcing another 'example' Vimscript file that automatically sets textwidth (in my case to 78) for text files.
My answer to a similar question as this one – How to stop gVim wrapping text at column 80 – on the Vi and Vim Stack Exchange site:
In my case, Vitor's comment suggested I run the following:
:verbose set tw?
Doing so gave me the following output:
textwidth=78
Last set from C:\Program Files (x86)\Vim\vim74\vimrc_example.vim
In vimrc_example.vim, I found the relevant lines:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
...
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
...
And I found that my .vimrc is sourcing that file:
source $VIMRUNTIME/vimrc_example.vim
In my case, I don't want textwidth to be set for any files, so I just commented out the relevant line in vimrc_example.vim.
It is correct that set nowrap will allow you to paste in a long line without vi/vim adding newlines, but then the line is not visually wrapped for easy reading. It is instead just one long line that you have to scroll through.
To have the line visually wrap but not have newline characters inserted into it, have set wrap (which is probably default so not needed to set) and set textwidth=0.
On some systems the setting of textwidth=0 is default. If you don't find that to be the case, add set textwidth=0 to your .exrc file so that it becomes your user's default for all vi/vim sessions.
I personnally went for:
set wrap,
set linebreak
set breakindent
set showbreak=ͱ.
Some explanation:
wrap option visually wraps line instead of having to scroll horizontally
linebreak is for wrapping long lines at a specific character instead of just anywhere when the line happens to be too long, like in the middle of a word. By default, it breaks on whitespace (word separator), but you can configure it with breakat. It also does NOT insert EOL in the file as the OP wanted.
breakat is the character where it will visually break the line. No need to modify it if you want to break at whitespace between two words.
breakindent enables to visually indent the line when it breaks.
showbreak enables to set the character which indicates this break.
See :h <keyword> within vim for more info.
Note that you don't need to modify textwidth nor wrapmargin if you go this route.

Smart Wrap in Vim

I have been wondering if Vim has the capability to smart wrap lines of code, so that it keeps the same indentation as the line that it is indenting. I have noticed it on some other text editor, such as e-text editor, and found that it helped me to comprehend what I'm looking at easier.
For example rather than
<p>
<a href="http://www.example.com">
This is a bogus link, used to demonstrate
an example
</a>
</p>
it would appear as
<p>
<a href="somelink">
This is a bogus link, used to demonstrate
an example
</a>
</p>
This feature has been implemented on June 25, 2014 as patch 7.4.338. There followed a few patches refining the feature, last one being 7.4.354, so that's the version you'll want.
: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.
Also relevant to this is the showbreak setting, this will suffix your shift amount with character(s) you specify.
Example configuration
" 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=>>
Note on behaviour
If you don't specify the sbr option, any showbreak any characters put appended to the indentation. Removing sbr from the above example causes an effective indent of 4 characters; with that setting, if you just want to use showbreak without additional indentation, specify shift:0.
You can also give a negative shift, which would have the effect of dragging showbreak characters, and wrapped text, back into any available indent space.
When specifying a min value, the shifted amount will be squashed if you terminal width is narrower, but showbreak characters are always preserved.
There is a patch for this, but it's been lingering for years and last time I checked did not apply cleanly. See the "Correctly indent wrapped lines" entry in http://groups.google.com/group/vim_dev/web/vim-patches -- I really wish this would get in the mainline.
Update: that link seems to have bitrotted. Here is a more up to date version of the patch.
Update 2: it has been merged upstream (as of 7.4.345), so now you only have to :set breakindent.
I don't think it's possible to have exactly the same indentation, but you can still get a better view 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 example code above, because Vim uses a different colour for '>>>'.
UPDATE: In June 2014, a patch to support a breakindent option was merged into Vim (version 7.4.346 or later for best support).
You might also try :set nowrap which will allow vim to display long lines by scrolling to the right. This may be useful for examining the overall structure of a document, but can be less convenient for actually editing.
Other options close to what you're looking for are linebreak and showbreak. With showbreak, you can modify what is displayed at the left margin of lines that are wrapped, but unfortunately it doesn't allow a variable indent depending on the current context.
The only way I know of that you could do this would be to use a return character (as mentioned by Cfreak) and combine the textwidth option with the various indentation options. If your indent is configured correctly (as it is by default with the html syntax I believe, but otherwise see the autoindent and smartindent options), you can:
:set formatoptions = tcqw
:set textwidth = 50
gggqG
If you have any customisation of the formatoptions setting, it may be better to simply do:
:set fo += w
:set tw = 50
gggqG
What this does:
: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.
Note that this is not the same as a soft wrap: it will wrap the lines in the source file as well as on the screen (unless you don't save it of course!). There are other settings that can be added to formatoptions that will auto-format 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'
:set smartindent
:set autoindent
I think you still have to use a return though
If your HTML is sufficiently well formed, running it through xmllint might help:
:%!xmllint --html --format
A Macro Solution:
Edit:
The operate gq{motion} auto-formats to whatever the variable "textwidth" is set to. This is easier/better than using the 80lBi^M I have for my macro.
If you have autoindent enabled
:set autoindent
Then entering a return at the end of a line will indent the next line the same amount. You can use this to hard enter in linewraps if you'd like. The following macro takes advantage of 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 the macros. After a few seconds you're text will all be in properly indented lines of 80 characters or less.
Explanation:
Here's a dissection of the macros:
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)
Caveats:
This macro will break if the there's a WORD that is 80 characters or longer.
This macro will not do smart things like indent lines past tags.
Use the lazyredraw setting (:set lazyredraw) to speed this up

Resources