Vim Tips Wiki
m (Toggle auto-wrap using txtwidth in INSERT mode moved to Toggle auto-wrap: Page moved by JohnBot to improve title)
(Format.)
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=441
 
|id=441
  +
|previous=440
|title=Toggle auto-wrap using txtwidth in INSERT mode
 
  +
|next=443
|created=March 11, 2003 9:20
+
|created=March 11, 2003
 
|complexity=basic
 
|complexity=basic
 
|author=Xiangjiang Ma
 
|author=Xiangjiang Ma
 
|version=6.0
 
|version=6.0
 
|rating=14/5
 
|rating=14/5
  +
|category1=
|text=
 
  +
|category2=
 
}}
 
In insert mode, I would like to
 
#Keep typing without auto-wrap (good for editing vimrc and C).
 
#Wrap long lines at will (good for email and text).
   
 
Following is the map I figured out, using <C-B>:
  +
<pre>
 
set sr fo=roqm1 tw=64
 
im <C-B> <C-O>:setl sr! fo<C-R>=strpart("-+",&sr,1)<CR>=tc<CR>_<BS><Right>
  +
</pre>
   
 
By default, it goes without auto-wrap. If I want, I can type <C-B> to toggle auto-wrap. Another <C-B> toggles back.
   
 
Basically it toggles two settings:
  +
<pre>
 
:set fo+=tc<CR>
 
:set fo-=tc<CR>
  +
</pre>
   
 
strpart() is used for toggling; "sr" is choosing for no good reason; "_" can be any char; <BS><Right> is needed to toggle this action.
In INSERT mode, I would like to
 
   
 
==Comments==
  +
A better toggle command, which doesn't just abuse the sr setting for a toggle switch would be:
  +
<pre>
  +
imap <C-B> :call AutoWrapToggle()<CR>
  +
function! AutoWrapToggle()
  +
if &formatoptions =~ 't'
  +
set fo-=t
  +
else
  +
set fo+=t
  +
endif
  +
endfunction
  +
</pre>
   
 
----
   
 
I found on Unix, we don't need the last part of the map.
1. keep tying without auto-wrap (good for editing vimrc and c)
 
   
 
im <C-B> <C-O>:setl sr! fo<C-R>=strpart("-+",&sr,1)<CR>=tc<CR>
2. wrap long line at will (good for email and text)
 
   
 
----
 
Just found that it is not related Unix or Windows.
   
 
It is a settting of virtualedit. For current vim version (6.1), if we
 
:set virtualedit=insert
   
 
We need the last part in the map, otherwise, we don't need it.
Following is the map I figured out, using &lt;C-B&gt; in this example:
 
   
  +
----
   
  +
To easily turn on and off automatic line wrapping, there are "wrap" and "nowrap" options. For example, wrapping can be enabled thus:
   
  +
set wrap
set sr fo=roqm1 tw=64
 
   
  +
Or to disable, use:
im &lt;C-B&gt; &lt;C-O&gt;:setl sr! fo&lt;C-R&gt;=strpart("-+",&amp;sr,1)&lt;CR&gt;=tc&lt;CR&gt;_&lt;BS&gt;&lt;Right&gt;
 
   
  +
set nowrap
   
  +
You may also need to set `textwidth`, which controls the line size (not just the presentation of the line). To turn of the text width, use:
   
  +
set textwidth=0
By default, it goes without auto-wrap. If I want, I can type
 
   
  +
To set a line to 80 characters, use:
&lt;C-B&gt; to triggle auto-wrap. Another &lt;C-B&gt; toggles back.
 
   
  +
set textwidth=80
   
  +
For more, `help textwidth` or `help wrap`.
 
Basically it toggles two settings:
 
 
 
 
a) :set fo+=tc&lt;CR&gt;
 
 
b) :set fo-=tc&lt;CR&gt;
 
 
 
 
strpart() is used for toggling; "sr" is choosing for no good
 
 
reason; "_" can be any char; &lt;BS&gt;&lt;Right&gt; is needed to *triggle"
 
 
this action.
 
 
 
 
It works for me on W2K. Please tell me if you have better ideas
 
 
to get this job done.
 
 
 
 
Note: We may use 'linebreak', but that still leaves a really
 
 
long line. I would like to keep my &amp;tw.
 
 
 
 
Thanks
 
 
 
 
 
 
 
}}
 
 
== Comments ==
 
 
I found on UNIX, we don't need the last part of the map.
 
 
im &lt;C-B&gt; &lt;C-O&gt;:setl sr! fo&lt;C-R&gt;=strpart("-+",&amp;sr,1)&lt;CR&gt;=tc&lt;CR&gt;
 
 
maxiangjiang--AT--hotmail.com
 
, March 11, 2003 14:33
 
----
 
 
Just found that it is not related UNIX or Windows.
 
It is a settting of virtualedit. For current vim version (6.1), if we
 
:set virtualedit=insert
 
We need the last part in the map, otherwise, we don't need it.
 
 
 
 
maxiangjiang--AT--hotmail.com
 
, March 12, 2003 10:16
 
----
 
<!-- parsed by vimtips.py in 0.479828 seconds-->
 

Revision as of 02:12, 5 November 2014

Tip 441 Printable Monobook Previous Next

created March 11, 2003 · complexity basic · author Xiangjiang Ma · version 6.0


In insert mode, I would like to

  1. Keep typing without auto-wrap (good for editing vimrc and C).
  2. Wrap long lines at will (good for email and text).

Following is the map I figured out, using <C-B>:

set sr fo=roqm1 tw=64
im <C-B> <C-O>:setl sr! fo<C-R>=strpart("-+",&sr,1)<CR>=tc<CR>_<BS><Right>

By default, it goes without auto-wrap. If I want, I can type <C-B> to toggle auto-wrap. Another <C-B> toggles back.

Basically it toggles two settings:

:set fo+=tc<CR>
:set fo-=tc<CR>

strpart() is used for toggling; "sr" is choosing for no good reason; "_" can be any char; <BS><Right> is needed to toggle this action.

Comments

A better toggle command, which doesn't just abuse the sr setting for a toggle switch would be:

imap <C-B> :call AutoWrapToggle()<CR>
function! AutoWrapToggle()
  if &formatoptions =~ 't'
    set fo-=t
  else
    set fo+=t
  endif
endfunction

I found on Unix, we don't need the last part of the map.

im <C-B> <C-O>:setl sr! fo<C-R>=strpart("-+",&sr,1)<CR>=tc<CR>

Just found that it is not related Unix or Windows.

It is a settting of virtualedit. For current vim version (6.1), if we

:set virtualedit=insert

We need the last part in the map, otherwise, we don't need it.


To easily turn on and off automatic line wrapping, there are "wrap" and "nowrap" options. For example, wrapping can be enabled thus:

 set wrap

Or to disable, use:

 set nowrap

You may also need to set `textwidth`, which controls the line size (not just the presentation of the line). To turn of the text width, use:

 set textwidth=0

To set a line to 80 characters, use:

 set textwidth=80

For more, `help textwidth` or `help wrap`.