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?
--
Just my 0.00000002 million dollars worth,
Shawn
The key to success is being too stupid to realize you can fail.
:nmap <C-P> :let save_tw=&tw<CR>:set tw=60<CR>gqap<CR>:let &tw=save_tw<CR>
saves the current setting for textwidth, and actually restores it to the
previous value.
Regards,
Peter Palm
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
It depends what you call "more elegant". Personally I don't like <CR> in
the middle of a mapping. You can use
:map <C-P> :set tw=60 <Bar> exe 'normal gqap' <Bar> set tw=0
or
function ReformatPara(width)
let save_tw = &tw
let &tw = a:width
normal gqap
let &tw = save_tw
endfunction
map <C-P> :call ReformatPara(60)<CR>
Best regards,
Tony.
--
If you stick a stock of liquor in your locker,
It is slick to stick a lock upon your stock.
Or some joker who is slicker,
Will trick you of your liquor,
If you fail to lock your liquor with a lock.
Can you expand on that? What's wrong with <CR> in a mapping?
I sense there's a subtlety here that might help me (and others)
better understand how to use vim effectively.
I don't think it's "wrong", it's just that I "don't like" it. I feel
that avoiding it makes the mapping more understandable. In the case of
ex-commands, you can use the bar character | (or <Bar> in a mapping) to
concatenate several ex-commands on one line, so that's one possibility
(except for the few commands which see the bar as part of their
argument, see ":help :bar", but in that case exe-wrapping can be used);
if the {rhs} is very complex with many commands, it's usually more
readable to define a function (with one ex-command per line, or
":normal" to use a Normal command), and then call the function in the
{rhs} of the mapping.
<CR> at the end of a mapping is perfectly OK, e.g. to terminate an
ex-command (or a string of <Bar>-separated ex-commands).
Best regards,
Tony.
--
"My particular problem is with registry entries, which seem to just
accumulate
like plastic coffee cups..." -- Paul Moore