Why can't I stop vim from wrapping my code? - vim

I can't stop vim from wrapping my Python code. If I enter :set nowrap like a champ, but it still wraps.
I can hit J to unite the split lines of code, so it seems like a real carriage return is being inserted. I just don't understand why or how to stop it.

'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.
'wrapmargin' 'wm' number (default 0)
local to buffer
Number of characters from the right window border where wrapping
starts. When typing text beyond this limit, an <EOL> will be inserted
and inserting continues on the next line.
Options that add a margin, such as 'number' and 'foldcolumn', cause
the text width to be further reduced. This is Vi compatible.
When 'textwidth' is non-zero, this option is not used.
See also 'formatoptions' and |ins-textwidth|. {Vi: works differently
and less usefully}
If you refer to auto wrapping of long lines sending them to the next one, try
:set textwidth=0
:set wrapmargin=0

None of the other answers worked for me (IDK why).
:set wrap! Did the trick for me (using GVim for Windows).

set formatoptions-=t should do the trick. set formatoptions+=t will turn auto-wrapping back on.
For more information on what you can do with formatoptions, see the docs.

For preventing vim from wrapping long lines I use these two lines in my .vimrc:
set nowrap " do not automatically wrap on load
set formatoptions-=t " do not automatically wrap text when typing

To disable line wrap, you can enter
:set wrap! or append this command to your ~/.vimrc.

Maybe it's the textwidth that's set, which automatically breaks lines when it reaches a certain length
Try
:set tw=0
If that fails play with e.g.
:set wrap linebreak textwidth=0
and
:set virtualedit=insert

Vim may have to be in vi-compatible mode.

Open vimrc_example.vim (Yes, this is the file in Vim74) and set textwidth=0.

On macbook pro I outcommented in .vimrc the line
autocmd FileType text setlocal textwidth=78
so it became
" autocmd FileType text setlocal textwidth=78
.
(I installed a version of vim via homebrew.)
This helped for all .txt files.

Related

How to turn off Vim relativenumber setting?

Today I have discovered vim's relativenumber option. I really would like to use it, but sometimes I need to swap between relative numbering and classic one.
I have tried to turn it off with :set relativenumber=off option (which returns me error attached above) and using :set number again but none of those works.
To turn on relative line numbering use this:
:set rnu
To turn it off use this:
:set nornu
By the way, if you had Googled around for your question, you would have found this great post which discusses things in more detail.
To turn on whatever option in vim:
:set <option>
To turn off whatever option in Vim:
:set no<option>
To toggle an option on/off:
:set <option>!
To check if an option is on or off:
:set <option>?
To set an option back to its default:
:set <option>&
I just use this toggle switch in my vimrc file.
function! NumberToggle()
if(&rnu == 1)
set nornu
else
set rnu
endif
endfunc
nnoremap <C-l> :call NumberToggle()<cr>
Relative Numbering is not turned on by default in vim, which means that you are probably turning it on through your ~/.vimrc file or one of your plugins. Look for set relativenumber or set rnu.
To turn it off for the current vim session you would simply run set norelativenumber or set nornu for short. This is a normal vim pattern for turning on and off settings like this. For example, spell check is set spell to activate and set nospell to deactivate.
To find this information and more on relative numbering, I recommend that you look in the vim help docs. For this case, while in vim run :h relativenumber
You actually have hybrid mode on (the line number your cursor is on is labelled as the absolute line number, not 0). In this case, both nu and rnu are on. You'll need to remove both to remove line numbers:
The following should work:
set nornu nonu

vim bug?! random character "ghosting" and ruler in the middle of the text with :set columns on

This is super strange and preventing me from writing because it's super bothersome. As I write weird characters appear if I have columns= on. First thing that will appear (and all of these issues are after the 1st line) is the ruler or gutter (at least thats the only two things with that background color) appear in the middle of the text. Secondly, extra characters will appear at the end of the line. For example:
Here is some text
here is the wrap, this is still part of the first line
The actual output would be
Here is some textt
here is the wrap, this is still part of the first line
And if I go to delete all the text I'll be left with
t
e
Or something. Here's a video where I caught some of the issues:
http://screencast.com/t/QsjZ0b2jG6bh
Again, this is only with set columns=80 on. Also, here is what is in my vimrc file that also changes in this :Write mode I made.
func! WordProcessorMode()
setlocal formatoptions=1
setlocal noexpandtab
setlocal spell spelllang=en_us
set complete+=s
set formatprg=par
setlocal wrap
setlocal linebreak
let g:solarized_termcolors=256
set background=light
colorscheme solarized
colors solarized
set nocul
set columns=80
set noruler
AutoCloseOff
endfu
com! Write call WordProcessorMode()
Not sure if this helps, but I had the same issue. I am using iTerm2, with zsh + tmux on Mac OS 10.8.1. I fixed it by removing the "set wrap" option, and setting the "set nowrap" option.
Yes, it's not a true solution since we have to disable wrap, but at least I can work in vim now.
Hope that helps.

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

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

Word wrap in Gvim

How do I make Gvim word wrap in such a way that doesn't break words in the middle?
Looks like there is a solution online after all.
:set formatoptions=l
:set lbr
Link: http://vim.wikia.com/wiki/Word_wrap_without_line_breaks
You can
:set nowrap
to just let huge lines scroll of the edge of your screen. But tw is probably the better way to go.
you can
:set wrap linebreak nolist
:set tw=78
sets the text width to 78 characters. You can use "[movement]gq" to re-wrap some text.
I use the following settings to wrap long lines for things like markdown files.
:set wrap
:set linebreak
:set nolist " list disables linebreak
:set textwidth=0
:set wrapmargin=0
Source: http://vim.wikia.com/wiki/Word_wrap_without_line_breaks
You can also use wrapmargin, which the manual defines as:
Number of characters from the right window border where wrapping
starts. When typing text beyond this limit, an <EOL> will be inserted
and inserting continues on the next line.

Tab key == 4 spaces and auto-indent after curly braces in Vim

How do I make vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like Emacs does?
Also, how do I save these settings so I never have to input them again?
I've seen other questions related to this, but it always seems to be a little off from what I want.
As has been pointed out in a couple of other answers, the preferred method now is NOT to use smartindent, but instead use the following (in your .vimrc):
filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
In your [.vimrc:][1] file:
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
The help files take a bit of time to get used to, but the more you read, the better Vim gets:
:help smartindent
Even better, you can embed these settings in your source for portability:
:help auto-setting
To see your current settings:
:set all
As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:
:help C-indenting
Related, if you open a file that uses both tabs and spaces, assuming you've got
set expandtab ts=4 sw=4 ai
You can replace all the tabs with spaces in the entire file with
:%retab
The best way to get filetype-specific indentation is to use filetype plugin indent on in your vimrc. Then you can specify things like set sw=4 sts=4 et in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).
To have 4-space tabs in most files, real 8-wide tab char in Makefiles, and automatic indenting in various files including C/C++, put this in your ~/.vimrc file:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Use filetype detection and file-based automatic indenting.
filetype plugin indent on
" Use actual tab chars in Makefiles.
autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif
" For everything else, use a tab width of 4 space chars.
set tabstop=4 " The width of a TAB is set to 4.
" Still it is a \t. It is just that
" Vim will interpret it to be having
" a width of 4.
set shiftwidth=4 " Indents will have a width of 4.
set softtabstop=4 " Sets the number of columns for a TAB.
set expandtab " Expand TABs to spaces.
On many Linux systems, like Ubuntu, the .vimrc file doesn't exist by default, so it is recommended that you create it first.
Don't use the .viminfo file that exist in the home directory. It is used for a different purpose.
Step 1: Go to your home directory
cd ~
Step 2: Create the file
vim .vimrc
Step 3: Add the configuration stated above
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
Step 3: Save file, by pressing Shift + ZZ.
The recommended way is to use filetype based indentation and only use smartindent and cindent if that doesn't suffice.
Add the following to your .vimrc
set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on
Hope it helps as being a different answer.
From the VIM wiki:
:set tabstop=4
:set shiftwidth=4
:set expandtab
edit your ~/.vimrc
$ vim ~/.vimrc
add following lines :
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a { and hitting Enter indents the following line.
As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.
You can put these settings in a .vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.
Firstly, do not use the Tab key in Vim for manual indentation. Vim has a pair of commands in insert mode for manually increasing or decreasing the indentation amount. Those commands are Ctrl-T and Ctrl-D. These commands observe the values of tabstop, shiftwidth and expandtab, and maintain the correct mixture of spaces and tabs (maximum number of tabs followed by any necessary number of spaces).
Secondly, these manual indenting keys don't have to be used very much anyway if you use automatic indentation.
If Ctrl-T instead of Tab bothers you, you can remap it:
:imap <Tab> ^T
You can also remap Shift-Tab to do the Ctrl-D deindent:
:imap <S-Tab> ^D
Here ^T and ^D are literal control characters that can be inserted as Ctrl-VCtrl-T.
With this mapping in place, you can still type literal Tab into the buffer using Ctrl-VTab. Note that if you do this, even if :set expandtab is on, you get an unexpanded tab character.
A similar effect to the <Tab> map is achieved using :set smarttab, which also causes backspace at the front of a line to behave smart.
In smarttab mode, when Tab is used not at the start of a line, it has no special meaning. That's different from my above mapping of Tab to Ctrl-T, because a Ctrl-T used anywhere in a line (in insert mode) will increase that line's indentation.
Other useful mappings may be:
:map <Tab> >
:map <S-Tab> <
Now we can do things like select some lines, and hit Tab to indent them over. Or hit Tab twice on a line (in command mode) to increase its indentation.
If you use the proper indentation management commands, then everything is controlled by the three parameters: shiftwidth, tabstop and expandtab.
The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4, or the abbreviation :set sw=4.
If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab. This causes tab characters which you type into the buffer to expand into spaces, and for Vim-managed indentation to use only spaces.
When expandtab is on, and if you manage your indentation through all the proper Vim mechanisms, the value of tabstop becomes irrelevant. It controls how tabs appear if they happen to occur in the file. If you have set tabstop=8 expandtab and then sneak a hard tab into the file using Ctrl-VTab, it will produce an alignment to the next 8-column-based tab position, as usual.
Afterall, you could edit the .vimrc,then add the conf
set tabstop=4
Or exec the command
Simplest one will be n vim file
set tabstop=4

Resources