Using fmt with Vim

I just discovered a nice tool called fmt. According to the man page, it formats paragraphs in a text document for better readability.

I write a lot of markdown with lines that are too long to be readable because formatting lines manually distracts me from the actual writing task. That’s where an automatic tool like fmt comes in.

Of course, you can point entire files at it but since I use Hugo with shortcodes and frontmatter and I don’t want fmt to mess those up, I like to control which lines it formats. That’s why I use it in Vim.

How to use it in Vim

In Vim, you simply select the paragraph you want to format with vap and then you do :!fmt. It instantaneously replaces the paragraph with its formatted equivalent.

But there’s a better way! You can set the formatprg variable using an autocmd for example:

autocmd FileType markdown setlocal formatprg=fmt

Now you can use the gq action to format lines or entire documents.