How to wrap lines and words in vim

Vim provides various options to increase text comprehensibility when creating or editing text files. It offers all the editing options that a modern word processor provides 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 location or object. In Vim, text wrapping is beneficial because it confines the text to the margins of the window so you don't have to do horizontal scrolling.

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

Types of wrapping in Vim

Before learning 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.

soft cover Vim creates the illusion of text wrapping around the inside of a window, but in reality, the text remains one long horizontal line of words.

hard cover The Vim editor is configured to insert a new line after a specific line width.

How to wrap lines and words in Vim via soft wrap

Text in Vim can be viewed with default soft wrap, but words and lines are split at the edge of the terminal window.

Or simply enable line numbers; One paragraph will be considered one line; This is because the text is soft wrapped.

Soft wrap is enabled by default in the Vim editor:

If it is not then use this :set rap Command in normal mode.

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

Use Vim editor to break lines without splitting words :set linebreak Permission:

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

If line breaks still don't work after enabling you may need to disable them using list :set nolist Permission:

Creating a command for soft wrap

You can always create a custom command to execute all the commands above at once; To set a soft wrap of the current document. To open vimrc file:

Use the given syntax to create a command:

Permission wrap up set wrap line breaks nolist

Comment: User-defined commands always start with a capital letter.

Now, just typing :srap The command in the Vim editor will set soft wrap.

How to Wrap Lines and Word in Vim via Hard Wrap

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

Or use the shortcut:

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

We can also use legacy commands (vi compatible) to set hard wrap in Vim editor :set wrapmargin Which starts from the right side of the window.

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

textwidth And wrap margin The commands in the Vim editor are equivalent. You can make hardwrap permanent by entering the command in it vimrc file.

How to reformat lines

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

Go in picture pressing mode V Select the key and next lines.

After selecting use :gq To reformat unformatted rows.

If you want to format a paragraph use :gq}. To know more about QG use command :help gq In Vim editor.

Navigating wrapped lines

The default key bindings for navigation in Vim are h, j, k, and l; But these keys are for line navigation and not wrapped line navigation. you have to add Yes Keys with traditional navigation keys.

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

For me, it's fine to have separate keys for both normal and wrapped navigation. But you can always map the same keys for wrapped navigation by placing the following commands vimrc file:

Disabling soft and hard wraps in Vim

These wrap settings can be disabled at any time; See instructions below to turn off wrap settings:

Disable Soft Wrap

To disable type :set nowrap Command in Vim editor.

This command will disable soft wrap and linebreaks together.

Disable Hard Wrap

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


If you have made any permanent change by adding this command to vimrc file then simply remove it and save the file using shift+z Or :wq,


Wrapping is a method by which text is made more understandable in Vim as it wraps the text around the edges of the window effectively dividing words and lines. Soft wrap is enabled by default, you just need to activate line breaks. Whereas for hard wrap you have to set the maximum limit of characters for automatic wrapping.

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

Add comment

By Ranjan