SHARE
Facebook X Pinterest WhatsApp

Want Vim to Share Newlines?

Written By
thumbnail Juliet Kemp
Juliet Kemp
Sep 1, 2020
ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

In a recent article I wrote about using regular expressions in editors, I mentioned the fact that by default, Emacs will match newlines when using a complemented character set, and vim won’t. A helpful reader, Tim Chase, contacted me to share a way around this.

Tip of the Trade: Want vim to share newlines? You’re only two characters away.

It’s pleasingly straightforward: All you do is to add _ in front of the character class you want to match. This will then match the character class plus newline. So to search for a word character:

/w

/ (from command mode) is the search command in vim, and w indicates a word character. So, to search for a word character or newline:

/_w

Note that you don’t have to use backslash again for the w. In effect, _w is treated as a single character class definition.

The example I used in my article was looking for a single-line quote, for which you can use

/"[^"]+"

This searches for a quote mark, at least one (+) of any character

except a quote mark (the complemented character class [^CHAR]
means “any character except CHAR), and then another quote mark. To extend that to multiple lines, all you need do is add _ in front of that complemented character class:

/"_[^"]+"

Straightforward and very useful. It’s nice as well to have the option of including or not including newlines. Thanks to Tim for the heads-up! Yet another example of however long you have been using a Linux tool, you can always learn new things about it.

Have you found any unusual workarounds in Linux or Unix tools you regularly use? Please comment below and let me know.

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of “Linux System Administration Recipes: A Problem-Solution Approach” (Apress, 2009).

Follow ServerWatch on Twitter

thumbnail Juliet Kemp

Juliet Kemp is a ServerWatch contributor.

Recommended for you...

What Is a Container? Understanding Containerization
What Is a Print Server? | How It Works and What It Does
Nisar Ahmad
Dec 8, 2023
6 Best Linux Virtualization Software for 2024
What Is a Network Policy Server (NPS)? | Essential Guide
ServerWatch Logo

ServerWatch is a top resource on servers. Explore the latest news, reviews and guides for server administrators now.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.