Hard Wrap Selection in Vim

This is a handy little trick to hard wrap a selection of text to a specific column number. I usually use this when I’m rewriting a Git commit message which I may have edited a bit and lost the automatic hardwrapping Vim enables for Git commit messages.

Here’s an example message.

Change `entries` command to only list by default

`entries` will not prompt for an ID to edit after displaying the list, `--edit`, or `-e` can now be appended to prompt for an ID to edit in the
default editor.

Tell Vim the textwidth you want to hardwrap to:

:set textwidth=72

Then select the text you wish to wrap, and type the following key sequence.

gq$

You’ll then get a perfectly hardwrapped message as can be seen below:

Change `entries` command to only list by default

`entries` will not prompt for an ID to edit after displaying the list,
`--edit`, or `-e` can now be appended to prompt for an ID to edit in the
default editor.

About

I'm a technology professional who's been passionate about computers since my Grandad introduced me to an Intel 386 back in the 90s when I was a kid. Those moments inspired a passion within for technology, and I've been playing around with anything with a circuit board ever since. Whenever I have a moment you can probably find me working on something computer-related, and this is where I like to write about those moments.