How do I set an unlimited textwidth in Vim? - vim

In Vim, I don't want to have a text width at all. I would like lines to go on forever. I tried set textwidth=0 but fq will still wrap the lines up to a certain number. The only way I can do this now is to set textwidth to a very large (but not too large) number. Is there a way to say that I want an unlimited text width?

Setting textwidth to 0 is the right way to not have a limit on the text width. As for gq, it's documented that it uses the screen width if textwidth is set to 0.
If you want to just join lines and not wrap them at all, then you can just use J instead of gq, which will join the lines into one line, and may provide what you're looking for in terms of formatting.

Related

How to fix problem with tabstop configuration in ~/. vimrc

I set tab configuration in ~/.vimrc as below:
set ts=4 sts=4 sw=4
I notice that if the word is 4 characters long or above, the cursor shift into right 4 spaces as in configuration for tabstop.
But if the word is less than 4 characters long, it didn't shift into 4 spaces.
Example:
'name' + <Tab>: tab produced correct number of spaces (i.e 4 spaces)
'age' + <Tab>: tab produced wrong number of spaces (i.e 1 space only)
Why is it ?
Does the word length effect tab?
What can I do if I want to shift the cursor to 4 spaces as configured regardless of the word length?
Thanks a lot
You’re probably inserting regular tabs, which display variable-width according to what’s before and after. I find having set list on is really handy for this (though you probably won’t like the default listchars settings).
If you really want spaces (which I find better anyway, set expandtab.
Also, most long-time users recommend leaving tabstop at 8, since you can’t control how wide every one’s tabs are.
The way that the tabstop, shiftwidth, and softtabstop options work is that they control indentation to certain points that are commonly referred to as "tab stops". In other words, they're designed to always indent to a column that's a multiple of the setting.
So if your tab stops are at multiples of 4, then hitting the Tab key will cause the cursor to indent to a column that is the next multiple of 4. This is the behavior of inserting a literal tab (U+0009, CHARACTER TABULATION) into a document and then rendering it on a normal terminal (except that the width is usually 8 there). This results in text that is aligned at fixed columns, which is the desired style for most programming languages and text markup formats.
As you've noted, this does result in different amounts of indent if the words are different lengths. Typically in code, we would just cause the second column to be at the next tab stop and not care that the indents are of different lengths. That is, in your example, we'd hit Tab once on the first line and twice on the second, and start the next column at column 8.
I'm not aware of any way to force Vim to insert a specific number of spaces other than the standard editing commands. Normally users who are in this situation just hit Space four times if they really want four spaces and not an indentation to the next tab stop. You can of course create a mapping if you need to do that a lot.

In Vim statusline, right-pad number

Say I do set statusline+=col:%3v; in Vim 8.
Now if I'm on column 1, my statusline looks like col: 1;.
How do I get the padding on the other side of the number? E.g., how do I get my column 1 statusline to look like col:1 ;?
Use %-3v. As documented in :help 'statusline:
field meaning
- Left justify the item. The default is right justified
when minwid is larger than the length of the item.
This is also the convention used by printf, which vim is clearly imitating.

Cursorline only highlights textwidth not entire window

I like to edit my code in fullscreen and like a visual hint what the textwidth is. I have the text that is passed the 79 characters highlighted using OverLength. I created another visual indicator by setting the ColorColumn to +1. Instead of the ColorColumn, I would prefer if the CursorLine only highlighted the portion of the line is the textwidth rather than the entire line, i.e. CursorLine stops after 79 characters. Is that possible?
Thanks!
CursorLine always covers the entire width of the current window (multiple lines if 'wrap' is set), regardless of the actual length of the line. The length also cannot be influenced by any built-in option. Same applies to CursorColumn in the vertical direction.
The recommended way to highlight a certain text width is via ColorColumn.

Vim colorcolumn interrupted when using wrap

When using set colorcolumn=80 in conjunction with set wrap, the colored vertical column is interrupted, since wrapped lines are still considered as one single line. I think a solution might be to highlight columns 160, 240, ... as well, but I don't know enough Vimscript to implement this.
Please have a look at the following .gif animation to see what I mean:
Is it possible to display the colorcolumn as a continuous vertical bar?
It doesn't appear that vim provides a way of doing this by default. However,
the colorcolumn option allows you to specify more than one column to highlight. (in a comma seperated list) So I think this should do what you want:
set colorcolumn=80,160,240

Limiting text width does not work as expected if not inserting at the end of a line

I know we can use :set tw=80 to limit the text width. However, if we insert text before the end, this function doesn't work at all.
For example, let's say we type "Would you like to have responses to your questions sent to you via email?"
If we continue to input after "?", tw works fine. But if we insert before say "have", it doesn't break off the line even if it exceeds the specified text width.
Is there any way to make this work in the latter case?
You may try
:set fo+=a
which reformats your paragraph as you type.
See :h fo-table.
This might be configurable using formatoptions or formatexpr, but I'm not sure how.
Another solution is to manually format using gq on a visual line selection i.e. add a breakline if the text width is >= tw.
Type in whatever text you like inside another block of text, thus making it 80 chars or more in width
Select the text you think is misformatted using V (Shiftv)
gq
From :h gq:
Format the lines that {motion} moves over.
Formatting is done with one of three methods:
1. If 'formatexpr' is not empty the expression is
evaluated. This can differ for each buffer.
2. If 'formatprg' is not empty an external program
is used.
3. Otherwise formatting is done internally.
In the third case the 'textwidth' option controls the
length of each formatted line (see below).
If the 'textwidth' option is 0, the formatted line
length is the screen width (with a maximum width of
79).
The 'formatoptions' option controls the type of
formatting fo-table.
[..]
For more information on how Vim's textwidth works, take a look at
How to use Vim’s textwidth like a pro

Resources