When you’re using Vim to read log files or write blog posts, one problem that you may come up against is wrapping long lines of text. Vim, being the powerful editor that it is, can handle this in multiple ways. Today, I’d like to talk about two of those options. One changes the way that text is displayed in the buffer. The other changes the way the text is formatted in the file.
Option 1: Wrap
If you are concerned with the way long lines are displayed in Vim, then the
wrap setting is for you. It’s turned on by default. When a long line of text reaches the edge of the window, Vim will continue displaying the text on the next line in the same way that Microsoft Word or any other rich text editor would. I find this setting useful when reading log files. Below is an example of how to change it.
Option 2: TextWidth
If you are concerned with the way long lines are formatted in the buffer, you can use the
textwidth setting. It’s set to 0 by default. When set to a specific character limit, such as 80, Vim will force any text exceeding that limit onto a new line in the buffer. I find this setting useful when writing a long article, such as a blog post. Below is an example of how to change it.
Bonus: Reformatting All Long Lines In A File
What if you’re working with a file that has a lot of long lines and you’d like to reformat them all to a specific number of characters? This simplest way to do this is with the
gq command. When used in conjunction with the
gq will reformat the selected lines to the desired length. Below is an example of how you can reformat all of the lines in a file.
Find Out More
Vim provides many other settings that can be used in conjunction with
textwidth. To find out more, you can reference Vim’s help documentation by entering
:help wrap or
:help textwidth on Vim’s command line.