Skip to main content
Vim Text Editor:

Search and replace with newline in Vim

Summary

Handling newlines in Vim search and replace requires understanding \n and \r. This article explains how to remove, add, and replace newlines using the :s command. Learn to prevent ^M and ^@ character encoding errors. All examples also work with Neovim.

Introduction #

When working with the Vim text editor, you may need to search for and replace newlines. This is useful for tasks such as removing line breaks, adding extra spacing, or formatting text in a specific way. However, handling newlines in Vim can be tricky because the way they are represented differs between search and replacement:

Character encoding: In Vim's search and replace command (:s), when searching \n represents a newline, while \r represents a carriage return (CR, also known as Ctrl-M or ^M).

In the replacement part of the command, \r is used to insert a newline, while \n represents a null byte (0x00 or ^@). Understanding these differences is important.

If you ignore these differences you may end up inserting characters like ^M (Ctrl-M) or ^@ (ASCII null character 0x00), instead of the newline.

Examples #

Below I have put together some examples that explain how searching for newlines and/or replacing with newlines works in Vim.

Searching for a newline and replacing without newline #

If you want to remove newlines and, e.g. join lines together, you can search for \n and replace it with a space or nothing. For example, to join lines into a single paragraph by removing newlines, use:

:%s/\n/ /g

This command searches for newlines (\n) and replaces them with a space. If you want to remove the newlines without adding spaces, use:

:%s/\n//g

Searching for a newline and replacing with two newlines #

If you need to replace a single newline with two newlines to add extra spacing between lines, you can use:

:%s/\n/\r\r/g

Since \r is used for newlines in the replacement section, this command effectively doubles the line spacing by adding an extra newline for each occurrence.

Searching for foo and replacing with a newline #

To search for the word foo and replace it with a newline, use:

:%s/foo/\r/g

This replaces every occurrence of foo with a newline, effectively splitting the text at those points.

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

Search and replace with newline in Vim by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/search-and-replace-with-newline-in-vim">Search and replace with newline in Vim</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the Ditig legal page.

All Topics

Random Quote

“Content precedes design. Design in the absence of content is not design, it's decoration.”

 Jeffery Zeldman American web designer, author, and advocate for web standardsTwitter, - IT quotes