How to stop line breaking in vim - 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.

Related

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.

What is the Dollar sign ("$") at the end of every line in Vim

I am relatively new to Vim. Whenever I start Vim using vim LearnRuby.rb, a dollar sign appears at every line.
Why?
:set nolist
will turn off special characters for the current buffer, such as tabs being presented as ^I and end of line characters showing up as $.
However, if it's doing that consistently when you run vim, you need to look into your .vimrc (or other startup file where applicable) and find out what's doing the set list that causes it.
Open ~/.vimrc and check its contents
If you see a line like this:
set list
It means, it will display $ in every line to mark the end of line.
Either remove it or use :set nolist command in the vi editor.
Obviously, the solution is not to :set nolist, as that also disables other characters like tabs and trailing spaces, which can be very useful. :set nolist would be the correct answer if you wanted all of the special characters gone.
If you just want a quick fix, you could add this to your vimrc instead:
set listchars+=eol:\ .
That is a backslash followed by a whitespace.
Alternatively you can set all the arguments of listchars, for exampleset listchars=tab:>\ ,trail:. and not include the eol argument, which determines the character used for end of line.
Refer to :h listhcars and :h list

Stop Vim wrapping lines in the middle of a word

After doing :set wrap, Vim wraps lines longer than the window.
But is it possible to have Vim wrap to a new line on blank spaces only, not half-way through a word?
:help wrap
This option changes how text is displayed. It doesn't change the text
in the buffer, see 'textwidth' for that.
When on, lines longer than the width of the window will wrap and
displaying continues on the next line. When off lines will not wrap
and only part of long lines will be displayed. When the cursor is
moved to a part that is not shown, the screen will scroll
horizontally.
The line will be broken in the middle of a word if necessary. See
'linebreak' to get the break at a word boundary.
:help linebreak
If on Vim will wrap long lines at a character in 'breakat' rather
than at the last character that fits on the screen.
:help breakat
'breakat' 'brk' string (default " ^I!#*-+;:,./?")
So, :set linebreak and it should work out of box. Or you can restrict breakat to just break on spaces, instead of spaces+punctuation.
Use
:set linebreak
Or 'lbr' for short. It will break lines on characters included in your 'breakat' option, which includes a space by default.
With vim open, press esc and enter
:set lbr
The following will do a line wrap without breaking any words and preserve the shorter lines.
:set formatoptions+=w
:set tw=80
gggqG
To try and format the current paragraph try the follwoing:
:nnoremap Q gqip
The following commands work for me, and I am using Red Hat 7.x at work, and Cygwin 3.1.4 at home. The exclamation point acts like a not operator.
:set wrap
:set wrap!

What is Vim's feature name for this: # vim:sw=4:ts=4:et:

Even after 20 years with Vim, I keep forgetting name for the Vim feature where the editor picks up config statements from a comment at the beginning (or I think end) of a file:
# vim:sw=4:ts=4:et:
Thanks for a reminder!
It's called modeline
:he modeline
If you start editing a new file, and the 'modeline' option is on, a
number of lines at the beginning and end of the file are checked for
modelines. There are two forms of modelines.
The first form: [text]{white}{vi:|vim:|ex:}[white]{options}
[text] any text or empty
{white} at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:} the string "vi:", "vim:" or "ex:"
[white] optional white space
{options} a list of option settings, separated with white space or ':',
where each part between ':' is the argument for a ":set"
command (can be empty)
Add this to $MYVIMRC:
setglobal modeline
It's called modeline. In help it can be found by grepping
helpgrep # vim
If you wish to check whether modeline are active, do set modeline? (if the are it will say modeline, otherwise nomodeline)
To turn them off for certain, add this in your vimrc
:set modelines=0 "number of modelines vim parses
:set nomodeline "turn off parsing
:h 'ts (i.e. :help 'tabstop') will bring up a detailed explanation of how to use et, ts, and sw as well as giving a pointer to modeline (modeline is the option that is in question).

Auto wrap long lines in VIM

When I edit a haskell file in Vim, my editor automatically wraps comments that go past 80 characters to the next line. I'd like Vim to use the same behavior for python files (and text files), but I can't find the setting that does this anywhere, in my ~/.vim/syntax folder or vimrc.
Here are relevant lines of my .vimrc:
set wrap
set textwidth=80
Setting textwidth will put your maximum line length. This would put a new line at that character point (white space could play with an exact 80 a bit though). (This effects the actual formatting of your file).
wrap is indeed what you want for your splitting/wrapping though.
Make sure your .vimrc is in your home directory.
I uses the following script in my vimrc to wrap .txt file automatically. This may give you some hints.
if has('autocmd')
au BufRead,BufNewFile *.txt set wm=2 tw=80
endif
If a line is already longer than textwidth when insert mode is started, the line may not be wrapped even if text is added to the line. A long line is not wrapped when text is added if formatoptions contains "l". If needed, "l" can be removed like so:
:set fo-=l
You can check the value of your formatoptions via:
:set fo?
You can learn the meaning of those letters here or via:
:he fo-table

Resources