On Wednesday 03 December 2008 9:35 am, Mr. Shawn H. Corey wrote:
> 
> Hi,
> 
> I have the following map:
> 
> :map <C-P>    :set textwidth=60<CR>gqap<CR>:set textwidth=0<CR>
> 
> Is the a more elegant way to do this?

peter palm's suggestion is a bit more elegant -- here's what
i do:

nmap <silent> <F1> :call F1_formatter("70")<CR>
imap <silent> <F1> <C-O>:call F1_formatter("70")<CR>
nmap <S-F1> :call F1_toggle_width("70")<CR>
imap <S-F1> <C-O>:call F1_toggle_width("70")<CR>

function! F1_formatter(cp)
    let s:save_tw = &tw
    let &tw = a:cp
    silent normal gq}
    let &tw = s:save_tw
endfunction

function! F1_toggle_width(w)
    if &tw == 0
        let &tw = a:w
    else
        let &tw = 0
    endif
    set tw?
endfunction

then i can do things like this:

nnoremap <Leader>m :source /home/scott/.vim/mail.vim<CR>

where mail.vim contains:

"  mail mode settings
setlocal tw=60
setlocal ft=mail
nmap <silent> <F1> :call F1_formatter("60")<CR>
imap <silent> <F1> <C-O>:call F1_formatter("60")<CR>
nmap <S-F1> :call F1_toggle_width("60")<CR>
imap <S-F1> <C-O>:call F1_toggle_width("60")<CR>
nmap <F8> :s/^/> /<CR>:silent noh<CR>j
imap <ESC><F8> :s/^/> /<CR>:noh<CR>ja
nmap <S-F8> 2xj

what this buys me is the ability to have different F1
formatters defined for different session types just by
remapping the F1 keys

in the most generic case, i have tw=0 as the default in my
.vimrc -- who wants spurious line breaks added to their
code, or whatever -- when i want to format, F1 formats for
me 

to be honest i'm not sure i need the toggle_width function,
since the formatter functions set width anyway, but i found
it comforting to be able to set textwidth and toggle it
handily

anyway, i haven't used F1 for help in decades

hth,

sc



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to