• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Comprehensive IT Guides for Pros and Enthusiasts

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home » Regex tricks – Remove leading and trailing white spaces with Notepad++ and Vim

Regex tricks – Remove leading and trailing white spaces with Notepad++ and Vim

August 1, 2021 - by Zsolt Agoston - last edited on August 8, 2021

To remove leading and training white spaces with regular expressions we have two options.

  • Remove only the horizontal whitespace characters: spaces and tabs
  • All whitespaces, including all vertical line breaking characters, in this case the empty lines are removed from the text as well.

To remove only the horizontal spaces, we use the \h token that represents the space and tab characters. To remove all whitespaces we make use of the \s regex token which symbolizes all of the horizontal and vertical line breaking characters: [\r\n\t\f\v ]

The two expressions we work with are:

# If only spaces and tabs need to be removed
^\h*(.*?)\h*$

# If all whitespaces (line breaks too)
^\s*(.*?)\s*$

Removing whitespaces with Notepad++

Open the text file with leading and/or trailing whitespaces, then press [Ctrl+H] or use the Search/Replace... button in the top menu bar to bring up the Replace dialog.

There put the expression to the 'Find what:' field, and \1 to the 'Replace with:' text box.

Regex tricks – Remove leading and trailing white spaces with Notepad++ and Vim

Make sure the search mode is set to 'Regular expression'.
The 'Wrap around' checkbox is selected by default, ensuring that the whole document is being searched, not only the lines after the current cursor position.

Hit Replace All

Regex tricks – Remove leading and trailing white spaces with Notepad++ and Vim

Removing whitespaces with Vim

The Visual IMproved editor only recognizes spaces and tabs as whitespaces (\s). If you want to remove empty lines with Vim, please check my other guide here.

:%s:^\s*\(.\{-}\)\s*$:\1:

 

Regex tricks – Remove leading and trailing white spaces with Notepad++ and Vim

Reader Interactions

Comments Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • Removing whitespaces with Notepad++
  • Removing whitespaces with Vim

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}