Very often, I write markdown in VIM, and there will be paragraphs in those markdown. To help my editing, I setup my Vim to wrap a line on 80 chars. It works nice if I just keep typing, but the problem is if I need to do some correction, it becomes very annoying. demo(taken from wikipedia first order logic):The adjective first-order distinguishes first-order logic from higher-order logic in which there are predicates having predicates or functions as arguments. In first-order theories, predicates are often associated with sets. In interpreted higher-order theories, predicates may be interpreted as sets of sets.So far so good. But when I revise the article, I might decide to add something in the middle, say:The adjective first-order distinguishes first-order logic from higher-order logic in which there are predicates having predicates or functions as arguments,or in which one or both of predicate quantifiers or function quantifiers are permitted.In first-order theories, predicates are often associated with sets. In interpreted higher-ordertheories, predicates may be interpreted as sets of sets.notice line 3 is the one I want to wrap. If I do it in VIM, I will need to manually join the lines and rewrap the whole paragraph.Anyone got idea how to make VIM do it automatically?
automatically rewrap lines when writing markdown in VIM
formatting;filetype markdown;word processing
Even simpler: the a flag for 'formatoptions' enables automatic formatting of paragraphs whenever text is inserted or deleted. See :help fo-table for details on 'formatoptions' flags and :help autoformat.:set formatoptions+=agq and gw will format the lines that the following motion moves over.Formatting is done with one of three methods:                1. If 'formatexpr' is not empty the expression is                   evaluated.  This can differ for each buffer.                2. If 'formatprg' is not empty an external program                   is used.                3. Otherwise formatting is done internally.                In the third case the 'textwidth' option controls the                length of each formatted lineThe difference between the two, is that gq will leave the cursor on the first non-blank of the last formatted line. gw will put the cursor back where it started.You can easily manually rewrap the paragraph your cursor is currently in with gwap, or the entire document with gggwG, though that will move your cursor thanks to the leading gg.With an autocommand, you can have formatting happen automatically. Here's an example that formats the current paragraph when leaving insert mode:augroup myformatting    autocmd!    autocmd InsertLeave * normal gwap<CR>augroup ENDThere are other autocommand triggers that you may find work better for you. You can explore the options under :help autocmd-events. The most relevant ones are probably under the various subheading.