Guides Matching Newlines in Vim

Matching Newlines in Vim




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

Latest Posts

KVM vs VMware Hypervisor Comparison 2020

The KVM vs VMware hypervisor comparison is a classic debate. Hypervisors have increased in usability and power over the last several years as more...

Compare HP’s iLo & Dell’s iDRAC Server Management Tools

Most servers shipped from the major manufacturers today come with some type of out-of-band management tool or baseboard management controller (BMC). Two of the...

Get-MsolUser PowerShell Attributes & Properties

This article has been updated for 2020. Please note that WAAD was retired in 2018, but the cmdlets listed in this article are still...

Microsoft Azure PowerShell Scripts and Commands

Using PowerShell scripts and commands for quickly executing tasks in Windows operating systems offers a number of benefits over traditional scripting languages, such as...

Microsoft Hyper V Review

Microsoft Hyper-V: The Bottom line Microsoft Hyper-V lagged behind VMware's virtualization tool, one of the most popular tools in the space, when it was first...

Related Stories