I am writing my .tex files in vim and have automatic linebreaks at 80 characters. I would like the linebreak to automatically perform a concatenation J if there is text below it, so that I don't get the following output:

This line gets line-broken. This line gets line-broken. This line gets
This line is short and could be concatenated.

If, after this concatenation, another linebreak is required, the function is called recursively.

Is there a simple method for doing this?

  • 1
    You can try :help fo-a but I don't think it will be what you really want, esp for TeX.
    – D. Ben Knoble
    Commented Aug 24, 2022 at 14:31

2 Answers 2


A solution to review the paragraph you are in is:



  • gq reformat them according to textwidth
  • ip current paragraph

If you want that to be made for all paragraph while they are edited a solution, as mentioned by @Ben, is to have a within formatoptions

setlocal formatoptions+=a
  • Wow, that works really well. How would I make that into an automatic script that gets called when the line gets too long? Commented Aug 24, 2022 at 14:49
  • 2
    isn't this vipJgqq? easier to type, IMO (edit: gqip should just work)
    – D. Ben Knoble
    Commented Aug 24, 2022 at 14:54
  • @ness_boy if you want that for every paragraph the solution of @Ben is good for you setlocal fo+=a. But there are probably some TeX paragraph that should not be reformated. Commented Aug 24, 2022 at 15:03
  • I see what you're saying. Thanks for the help! Commented Aug 24, 2022 at 15:06

I ended up making the following macros that are working well for me:

function RecursiveLinebreak()
    call feedkeys("\gq")
    call feedkeys("\ip")

autocmd FileType tex nmap <buffer> <C-A> :set fo+=a <CR>
autocmd FileType tex nmap <buffer> <C-S> :set fo-=a <CR>
autocmd FileType tex nmap <buffer> <C-J> :call RecursiveLinebreak() <CR>

These three commands perform the following tasks:

  • Enable automatic paragraph re-shaping
  • Disable automatic paragraph re-shaping
  • Restructure the current paragraph the cursor is on

Keeping the accepted answer though, as it gives an easy way to accomplish the task.

  • Isn't your function equivalent to normal! gqip?
    – D. Ben Knoble
    Commented Aug 30, 2022 at 14:20

Your Answer

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

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