vim

How to Wrap Lines and Words in Vim

Vim provides various options to enhance text comprehensibility when creating or editing text files. It provides all the editing options that a modern word processor offers such as changing font size, case, and even text wrapping.

Text wrapping is a feature in text document editing applications that allows you to wrap text within or around a space or object. In Vim, text wrapping is advantageous because it makes the text confined to the margin of the window so you don’t have to make horizontal scrolling.

In this guide, I am going to explore how to wrap lines and words in Vim editor, and how to make text more readable.

Types of Wrapping in Vim

Before learning about how to wrap lines and words in Vim editor, I would like you to understand the difference between the two types of wrapping in Vim.

The soft wrap in Vim creates an illusion of making the text wrap inside the window, but in reality, the text remains a long horizontal line of words.

The hard wrap in the Vim editor is configured to insert a new line after a specific line width.

How to Wrap Lines and Words in Vim through Soft Wrap

The text in Vim can be seen with default soft wrap, but the words and lines are split around the edge of the terminal window.

Or just enable the line number; a paragraph will be considered as a line; this is because the text is soft-wrapped.

The soft wrap in the Vim editor is enabled by default:

If it is not then use the :set wrap command in the NORMAL mode.

:set wrap

However, the default wrap split the lines and words at the window edges.

To make the Vim editor break lines without splitting the words use :set linebreak command:

:set linebreak

The soft wrap has been set in the Vim editor and now lines and words will be wrapped.

If the line break does not work even after enabling it then you may need to disable the list using :set nolist command:

:set nolist

Creating a command for Soft Wrap

You can always create a custom command to execute all the above-given commands in one go; to set the soft wrap of the current document. For open vimrc file:

vim ~/.vimrc

Use the given syntax to create the command:

command Swrap set wrap linebreak nolist

Note: The user-defined command always starts with the capital letter.

Now, simply typing the :Swrap command in the Vim editor will set up the soft wrap.

How to Wrap Lines and Word in Vim through Hard Wrap

In hard wrap, if the text width is set to 50 characters then the line will break after 50 characters because it is the maximum width of the automatic wrapping. To set the hard wrap :set textwidth=50 command will be used with a number.

:set textwidth=50

Or use the shortcut:

:set tw=50

Now, a new line (\n) will be inserted when the set limit of 50 characters is reached:

We can also use the legacy command (vi compatible) to set the hard wrap in the Vim editor and that is :set wrapmargin which starts from the right side of the window.

:set wrapmargin=30

If the wrapmargin is set to 30, then it means the EOL will be inserted when the lines will the 30 characters away from the right side of the window.

The textwidth and wrapmargin commands are equivalent in the Vim editor. You can make the hardwrap permanent by inserting the command in the vimrc file.

How to Reformat the Lines

If you have some unwrapped text in your file, you can use :gq command to format the text in the defined format.

Get into the VISUAL mode by pressing the v key and next select the lines.

After selecting the use :gq to reformate the unformatted lines.

:gq

If you want to format the paragraph use :gq}. To learn more about the qg command use :help gq in the Vim editor.

Navigating the Wrapped Lines

The default key bindings for navigation in Vim are h, j, k, and l; but these keys are for line navigations not wrapped line navigations. You will need to add g key with the traditional navigation keys.

gj Navigating down by 1 line
gk Navigating up by 1 line
g^ / g0 Navigating the beginning of the line
g$ Navigating to the end of the line

For me, it is okay to have distinct keys for both normal and wrapped navigations. But you can always map the same keys for wrapped navigation by placing the following commands in the vimrc file:

noremap j gj

noremap k gk


Disabling Soft and Hard Wraps in Vim

These wrap settings can be disabled at any time; see the instructions given below to set the wrap settings off:

Disable Soft Wrap

To disable the type the :set nowrap command in the Vim editor.

:set nowrap

This command will simultaneously disable the soft wrap and the linebreak.

Disable Hard Wrap

To disable the hard wrap just set the text width equal to 0.

:set textwidth=0

Or:

:set tw=0

If you have made a permanent change by adding this command to the vimrc file then simply remove it and save the file using Shift+zz or :wq.

Conclusion

Wrapping is a method by which text is made more comprehensible in Vim as it wraps the text around the sides of the Window effectively splitting the words and lines. The soft wrap is enabled by default, you just need to activate the line break. While for hard wrap you need to set the maximum limit of characters for automatic wrapping.

The choice between soft and hard wrap is entirely a matter of personal preference. Personally, I prefer to use soft wrap for programming while hard wrap for creating the document.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.