Format Paragraphs to 80 Characters with Vim (No Plugins Needed)
When using the built-in gaqp motion and a custom mapping you can create hard line breaks at a custom text width.
Quick Jump: Demo Video
I’ve technically written over a million words of Markdown between blog posts, documentation and course notes.
One thing that I do all the time is format Markdown paragraphs to 80 characters. Vim lets us do that by default with the
gqap motion. We can do it 1 on paragraph or any selection of text. You can also hook it up to a custom mapping to make this process easier.
" Format paragraph (selected or not) to 80 character lines. nnoremap <Leader>g gqap xnoremap <Leader>g gqa
- 0:08 – Limiting the width of a line is useful
- 0:53 – Demo’ing the behavior that we’re going to get
- 1:16 – Why not do this automatically on save? Well…
- 3:16 – Checking out the mappings in my vimrc and going over gq motions
- 5:58 – Looking at gq’s help menu and adjusting the text width if you want
- 7:24 – Investigating gqap in Vim’s help menu
What’s your favorite gq motion? Let me know below.