I don't want VIM to automatically wrap text, so setting textwidth
is not a solution to this problem.
This comment gives a useful outline, but I'm not really sure how to go about doing this. My first naive, sanity-check attempt is:
function FormatExpr()
normal gw
endfunction
set formatexpr=FormatExpr()
... Which I would expect to just invoke gw
on a paragraph I select, but the text is left totally unchanged. In fact, it doesn't seem like any alterations I make to formatexpr
have any affect on gq
. :help formatexpr
says:
Expression which is evaluated to format a range of lines for the |gq| operator or automatic formatting (see 'formatoptions'). When this option is empty 'formatprg' is used.
But doesn't give me an idea of what that expression is supposed to be. I've also tried, for example %s/foo/bar/g
, to see if gq
will replace all instances of "foo" with "bar", but that didn't work either.
Ideally, I would like something like this to work:
function FormatExpr()
let stored_textwidth = textwidth
set textwidth = 100
normal gq
set textwidth = stored_textwidth
endfunction
I suspect this is a trivial problem—but I'm pretty ignorant regarding things like formatexpr
.