How could VIM (script?) insert ^L (page break) and /r on an empty line, automatically after every 30 Lines OR 1800 characters while writing? Any ideas?

1 Answer 1


Your idea might not work how you want it to, because if you go back and earlier parts of your file, your page breaks are going to end up in the wrong places.

I think a better idea would be to set up an autocommand that resets the page breaks whenever you save the file.

Here's a quick one that sets them every 30 lines whenever you save a file with a .txt extension. If you also want it to work for every 1800 characters you're going to need to write a more iterative function that steps through the file keeping track of characters and and newlines and places the page breaks in the appropriate places.

function! ResetPageBreaks() abort
  " Remove existing page breaks
  execute "g/^\<C-L>$/d"
  " Add a page break every 30 lines
  %s/^/\= !(line('.') % 30) ? "\<C-L>\r" : ''

augroup PageBreaks
  autocmd BufWritePre *.txt call ResetPageBreaks()
augroup END
  • Rich, it works! The idea is to have 30 lines on one page. Although the page could take more - 40 to 45 lines - using only 30 lines give enough space to add a few more lines on every page. (Reason: (:ha) prints always same layout of pages which helps in correcting from hardcopy.)
    – Thomas D
    Commented Nov 20, 2018 at 21:13
  • @ThomasD If the answer works for you, you should accept the answer (click on the check mark beside the answer to toggle it from greyed out to filled in).
    – Ralf
    Commented Apr 2, 2019 at 8:23

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.