How do I define function for toggling textwidth (and echoing along)?

120 views
Skip to first unread message

Santosh Kumar

unread,
Aug 25, 2012, 5:07:43 AM8/25/12
to v...@vim.org
By default I have textwidth set to 78:
set textwidth=78

By this setting if I type more than 78 characters in a line, those
will be moved to next line.
I want a mapping that will toogle that textwidth to 0 (or say disable
textwidth), so that I can type long sentences in a single line.

Currently I have this mapping:
nnoremap <silent> <leader>tw :exe "set textwidth=" . (&tw ? 0 : 78)<cr>

This does the work but I don't get notified if the work has been done.
All I want is along with toggling the setting I should be notified
(i.e. if textwidth is zero then show textwidth=0 at statusline).

Paul Isambert

unread,
Aug 25, 2012, 8:50:20 AM8/25/12
to vim...@googlegroups.com
Santosh Kumar <sntsh...@gmail.com> a écrit:
Try this:

:nnoremap <silent> tw :exe "set textwidth=" . (&tw ? 0 : 78)<CR> <Bar> :echo "textwidth=" . &tw<CR>

Best,
Paul

Santosh Kumar

unread,
Aug 25, 2012, 9:21:03 AM8/25/12
to vim...@googlegroups.com
Still nothing is shown on the status bar.

Can't we create a function named Toogletw() and call it on this mapping?
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

Simon Ruderich

unread,
Aug 25, 2012, 9:50:09 AM8/25/12
to vim...@googlegroups.com
On Sat, Aug 25, 2012 at 02:37:43PM +0530, Santosh Kumar wrote:
> [snip]
>
> Currently I have this mapping:
> nnoremap <silent> <leader>tw :exe "set textwidth=" . (&tw ? 0 : 78)<cr>
>
> This does the work but I don't get notified if the work has been done.
> All I want is along with toggling the setting I should be notified
> (i.e. if textwidth is zero then show textwidth=0 at statusline).

:help let-option comes useful in these cases. For example:

:nnoremap tw :let &textwidth = (&textwidth ? 0 : 78)<CR>:set textwidth?<CR>

It's a bit shorter and IMHO easier to read.

'formatoption' l (:help fo-table) might also be useful:

l Long lines are not broken in insert mode: When a line was
longer than 'textwidth' when the insert command started,
Vim does not automatically format it.

Regards,
Simon
--
+ privacy is necessary
+ using gnupg http://gnupg.org
+ public key id: 0x92FEFDB7E44C32F9

Paul Isambert

unread,
Aug 25, 2012, 11:48:04 AM8/25/12
to vim...@googlegroups.com
Santosh Kumar <sntsh...@gmail.com> a écrit:
>
> Still nothing is shown on the status bar.
>
> Can't we create a function named Toogletw() and call it on this mapping?

I hadn't read "status-line"; my code prints the width in the command-line.
If you want the value of "textwidth" to be mentioned in the status line, then
change the "statusline" option, and let your mapping simply change that value;
the change will be automatically reflected in the status line. The simpler
solution is (see ":help statusline"):

setlocal statusline=%!&tw

Of course the problem then is what happens if you don't have a status-line?

Best,
Paul

Gary Johnson

unread,
Aug 25, 2012, 12:21:54 PM8/25/12
to vim...@googlegroups.com
On 2012-08-25, Paul Isambert wrote:
> Santosh Kumar a �crit:
You could put a textwidth indication in the ruler, which doesn't
take as much screen space as the statusline, but 'rulerformat'
doesn't give you as much space for customization.

Regards,
Gary

Bee

unread,
Aug 25, 2012, 11:56:25 PM8/25/12
to vim_use
:help colorcolumn

Add this to your .vimrc:

set colorcolumn=+1
hi ColorColumn NONE ctermbg=Cyan

When textwidth=0 there is no colorcolumn,
otherwise it is drawn just after the textwidth.

Bill

Bee

unread,
Aug 25, 2012, 11:39:24 PM8/25/12
to vim...@googlegroups.com
On Aug 25, 2012, at 8:00 PM, vim...@googlegroups.com wrote:
> From: Santosh Kumar <sntsh...@gmail.com>
> Date: Aug 25 02:37PM +0530
> Url: http://groups.google.com/group/vim_use/msg/62cda81d82f8ec51

Andy Wokula

unread,
Aug 30, 2012, 2:02:37 PM8/30/12
to vim...@googlegroups.com
Am 25.08.2012 11:07, schrieb Santosh Kumar:
> By default I have textwidth set to 78:
> set textwidth=78
>
> By this setting if I type more than 78 characters in a line, those
> will be moved to next line.
> I want a mapping that will toogle that textwidth to 0 (or say disable
> textwidth), so that I can type long sentences in a single line.
>
> Currently I have this mapping:
> nnoremap <silent> <leader>tw :exe "set textwidth=" . (&tw ? 0 : 78)<cr>

This one is more verbose, without visual clutter:

:nn <expr> <Leader>tw printf(":setl tw=%d<CR>", &tw==0 ? 78 : 0)

> This does the work but I don't get notified if the work has been done.
> All I want is along with toggling the setting I should be notified
> (i.e. if textwidth is zero then show textwidth=0 at statusline).

An example how to play with the statusline:
:h 'stl

" standard statusline
" set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P

set statusline=%<%f\ %h%m%r%{TwLeft()}%=%-14.(%l,%c%V%)\ %P

" remaining textwidth
func! TwLeft()
let ioff = mode()==#"i" ? 1 : 0
if &tw == 0 || virtcol(".") > &tw + ioff
return ""
else
let left = &tw + ioff - virtcol(".")
return printf('[%d %s left]', left, left==1 ? "char" : "chars")
endif
endfunc

--
Andy
Reply all
Reply to author
Forward
0 new messages