4

I set up this nifty custom function to help me use Vim for word processing:

func! WordProcessorMode()
    setlocal formatoptions=ant
    setlocal textwidth=60
    setlocal wrapmargin=0
endfu
com! WP call WordProcessorMode()

However, when I use this mode, I find that unless I leave an empty line between two paragraphs, the two adjacent lines are automatically joined together, e.g.:

New Hampshire Spanks the Elites
Panic Sets In On Clinton Campaign
Some Sage Advice for Hillary Clinton

becomes

New Hampshire Spanks the Elites Panic Sets in on Clinton Campaign Some Sage Advice for Hillary Clinton

Of course, most of the time it's good practice to format your prose tidily, but sometimes (with numbered lists and whatnot) I would like to have the freedom to insert adjacent non-collapsed lines. Is it possible to alter my function to prevent this sort of behavior, or alternatively suspend it temporarily somehow so I can insert a numbered list?

1 Answer 1

3

I also use vim for a lot of word processing/prose, and I format as I go, rather than having a catch-all command that formats all your text at once. By manually formatting as I go, I implicitly solve the problem you describe. I'm not sure if this fits your workflow, but I'll describe it below:

I use this mapping:

" Easy line wrapping/formatting
imap gq <C-G>u<ESC>gw}

Which I invoke whenever I want to leave insert mode after typing out text that I wish to format.

This mapping performs the following actions:

<C-G>u Manually create an undo-block, so we can unformat the text if we don't like it;

<ESC> Leave insert mode;

gw} Format to the end of the paragraph. You can change } to whatever motion you prefer. (I format as I go along, so I only format from my current position to the end of the paragraph, which works well if I assume the previous text in the paragraph is formatted correctly.)

So as you're typing out

New Hampshire Spanks the Elites
Panic Sets In On Clinton Campaign
Some Sage Advice for Hillary Clinton

You simply don't use the mapping to leave insert mode. If you're typing out something that you DO want to format however; use the mapping to leave insert mode, and everything's done for you.

EG a long series of text input:

Streaming video is becoming a larger and more important proportion of internet traffic with each passing year. Video data is projected to take up 80\% of consumer internet traffic in 2019, with over 70\% of that being delivered via commercial Content Delivery Networks

gets formatted into the following when I leave insert mode:

Streaming video is becoming a larger and more important proportion of
internet traffic with each passing year. Video data is projected to take up
80\% of consumer internet traffic in 2019, with over 70\% of that being
delivered via commercial Content Delivery Networks

And if I want to make an edit to some point of the paragraph:

Streaming video is REALLY COOL AND I LIKE IT SO MUCH AND SO DOES EVERYONE ELSE AND THAT'S WHY IT'S becoming a larger and more important proportion of
internet traffic with each passing year. Video data is projected to take up
80\% of consumer internet traffic in 2019, with over 70\% of that being
delivered via commercial Content Delivery Networks

This gets nicely formatted into the following as well, when I leave insert mode:

Streaming video is REALLY COOL AND I LIKE IT SO MUCH AND SO DOES EVERYONE
ELSE AND THAT'S WHY IT'S becoming a larger and more important proportion of
internet traffic with each passing year. Video data is projected to take up
80\% of consumer internet traffic in 2019, with over 70\% of that being
delivered via commercial Content Delivery Networks
2
  • Thank you for that suggestion. Perhaps I will go down that route. Preferably, though, I'd like something that would let me focus more on the text and have to do less formatting (minimal though this is; I'll concede that).
    – Eirik S.
    Feb 12, 2016 at 10:27
  • Yeah, this was about as unintrusive as I could get it, while still allowing me to type in tables/specially formatted text as I like. I figured if you have to leave insert mode anyways, might as well hook a mapping onto leaving insert mode. Feb 13, 2016 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.