Notepad++

How to Remove Spaces in Notepad++ Efficiently

How to Remove Spaces in Notepad++ Efficiently

Spaces at the edges of lines go with Edit, Blank Operations. Spaces anywhere else go through the Replace dialog (Ctrl+H), in Normal mode when you want every one gone and in Regular expression mode when you only want repeats collapsed. Pasted text and CSV exports are where this usually comes up, since stray blanks break alignment and imports.

Tutorials from code2care and The Windows Club suggest \s or [\r\n\s]{n,} for the job. The Notepad++ User Manual defines \s as a class that also covers line breaks, so those patterns join lines.

\h matches only horizontal blanks, which makes it the safer pattern for space cleanup.

Which Notepad++ method removes which kind of space?

YouTube player

Blank Operations covers the edges of lines. For everything else you’re in the Replace dialog, using Normal mode to delete every space and Regular expression mode for repeated spaces or anything conditional.

MethodRemovesScopeMain risk
Edit, Blank OperationsLeading and trailing spaces and tabsWhole file, or one stream selectionStrips tabs too
Replace, Normal modeEvery plain spaceDocument, selection, open filesMisses tabs and non-breaking spaces
Replace, Regular expression modeRepeated spaces, line edges, any blank classSame as ReplaceWrong class changes line breaks
Edit, Line OperationsEmpty lines, including blank-only linesEntire documentDeletes lines, not spaces

Blank Operations beats a regex for line edges. No pattern to mistype, no wrong character class to pick.

Replace is the only method that reaches spaces inside a line, and it’s also the one that goes wrong most often. The settings that matter are covered next.

Line Operations only come into play when trimming leaves empty lines behind.

How to remove all spaces in Notepad++ with Find and Replace

YouTube player

Press Ctrl+H, type one space in Find what, leave Replace with empty, keep Search Mode on Normal, and click Replace All.

  1. Open the file and press Ctrl+H (or Search, then Replace).
  2. Click in Find what and type a single space.
  3. Clear the Replace with box.
  4. Set Search Mode to Normal.
  5. Tick Wrap around.
  6. Click Replace All and read the replacement count in the dialog’s status bar.

An empty Replace with box deletes whatever matched, according to the Notepad++ User Manual.

Wrap around decides the range. Ticked, Replace All makes one pass from the top of the file to the bottom. Unticked, it starts at the caret, so every space above the caret stays.

Search Mode also changes what your typed space means.

  • Normal treats everything as literal text, so only the plain space character is deleted.
  • Extended reads \t, \r, \n and \x20 as escapes, which means searching \t removes tabs.
  • Regular expression reads patterns, covered below.

One trap hides in the dialog itself. Open it with more than 1024 characters selected and In selection ticks automatically while the selected text fills Find what. Replace All then reports a single replacement, not the hundreds you expected.

A space copied from a web page can also be a non-breaking space that your typed space never matches. Since v8.9.1, the status bar warns when pasted Find what content holds invisible characters such as non-breaking or zero-width spaces (Notepad++ User Manual).

Replace All in Normal mode is blunt. It deletes the spaces between words too, so run it only when no space should survive, as with IDs or hex strings.

How to trim leading and trailing spaces with Blank Operations

Edit, Blank Operations holds the trim commands, and each one removes spaces and tabs from line edges, not spaces alone.

New to Notepad++ or just need a quick reference? Essential shortcuts, regex for Find & Replace, and advanced editing - including Ctrl+H, Column Mode, and Macro Recorder - is on one page in the Notepad++ Cheat Sheet.

Trim Trailing Space and Trim Leading Space

YouTube player

Trim Trailing Space deletes spaces and tabs after the last visible character on each line, and Trim Leading Space clears whatever sits before the first one.

Trailing blanks are invisible by default. Switch on the whitespace and tab marks under View, Show Symbol to see what will go before you run the command.

Run it from Edit, Blank Operations. There’s no dialog and no confirmation.

Trim Leading and Trailing Spaces in one pass

Trim Leading and Trailing Spaces combines both operations. The trim hits the whole line of a selection, even when the selection covers only the middle words (Notepad++ User Manual).

Scope is where sources disagree. Older forum answers say Blank Operations always process the entire file. The current manual says they default to the whole document, limit themselves to a single stream selection when one exists, and touch only the main selection in a multi-selection.

Column mode is the exception, and nothing is trimmed there. Select whole lines first when only part of the file needs cleaning.

Commands that also change line breaks and tabs

  • EOL to Space
  • Trim Both and EOL to Space
  • TAB to Space
  • Space to TAB (All)
  • Space to TAB (Leading)

EOL to Space replaces line endings with a single space. It works like Join Lines but acts on the entire file, so one click on the wrong menu entry merges every line into one.

The two Space to TAB commands run in the opposite direction from the goal here. Skip them unless you want to convert spaces into tabs.

How to remove extra spaces with regular expressions

YouTube player

Switch Search Mode to Regular expression, and [ ]{2,} collapses repeated spaces into one while ^\h+ and \h+$ strip the line edges.

Replace multiple spaces with a single space

YouTube player
  1. Press Ctrl+H and set Search Mode to Regular expression.
  2. Type [ ]{2,} in Find what, with one space between the brackets.
  3. Type one space in Replace with.
  4. Click Find Next once, then click Replace All.

The {2,} quantifier matches two or more copies of the element before it. The brackets keep the space visible in the box.

The Find Next click isn’t optional. The manual notes that “Invalid regular expression” appears only on Find Next, while other buttons just report no match.

Don’t swap in \h to catch more. The manual defines \h as the blank class [\t\x20\xA0], so \h{2,} also collapses tabs and non-breaking spaces. Two tabs in a row (an empty column) turn into one space and break tab-separated data.

Trim line starts and ends with a pattern

YouTube player
PatternMatchesReplace withUse for
[ ]{2,}Two or more spaces in a rowOne spaceGaps between words
^\h+Blanks at line startNothingLeading trim
\h+$Blanks at line endNothingTrailing trim
^\h+\h+$Blanks at both endsNothingTrim both, inside a selection
\sSpaces, tabs, CR, LF and moreNot advisedLine breaks change with it

The last row matters because several tutorials (code2care, The Windows Club) recommend \s or [\r\n\s]{n,} for space cleanup. The manual defines \s as the full space class, which lists \n and \r, so those patterns act on line endings too. \h stops at the end of a line.

For a selection-only trim, tick In selection and run ^\h+|\h+$. The result matches the Trim Leading and Trailing Spaces command, with the scope under your control.

The engine is the Boost regex library, v1.90 as of Notepad++ v8.9.1 (Notepad++ User Manual). Patterns tested on regex websites that claim PCRE can behave differently here.

How to remove empty lines left after trimming spaces

YouTube player

When trimming leaves gaps, run Edit, Line Operations, Remove Empty Lines (Containing Blank characters). It deletes lines with no characters and lines that hold only spaces or tabs.

MethodDeletesLeaves
Remove Empty LinesLines with zero charactersLines holding spaces or tabs
Remove Empty Lines (Containing Blank characters)Empty and blank-only linesNothing blank
Regex ^\h*\R with In selectionEmpty and blank-only lines in the selectionA last line with no line break

Both menu commands work on the entire document, according to the Notepad++ User Manual. Neither respects a selection.

Forum moderators fill that gap with a pattern. Set Regular expression mode, tick In selection, type ^\h*\R in Find what, and leave Replace with empty.

\R matches any line break sequence, so the same pattern covers CRLF and LF files.

Order matters if you trim first. Trim Leading and Trailing Spaces turns a blank-only line into a truly empty one, so the plain Remove Empty Lines command then finishes the job.

guy038 on the Notepad++ Community has posted a few variants worth knowing.

  • ^\R+ removes truly empty lines only
  • ^(\h*\R)+ removes empty and blank-only lines
  • ^\h*\R|\R\h*\z also catches a final line that has no line break after it
  • \R\R\K\R+ keeps one blank line as a paragraph separator and deletes the surplus

The final line escapes the simple patterns because they end in \R, and a last line without a break gives \R nothing to match.

How to apply the cleanup to a selection, all open files or a folder

YouTube player

A highlighted block needs In selection ticked. Open tabs get Replace All in All Opened Documents, and a whole folder goes through the Find in Files tab (Ctrl+Shift+F).

Selection only

  • With In selection ticked, Count, Replace All and Mark All stay inside the highlighted text.
  • Find Next and the single Replace button still run on the whole document.

Click Count before Replace All. The status bar reports the number of matches inside the selection before anything changes.

All open documents

Replace All in All Opened Documents repeats the replacement in every file buffer open in Notepad++, per the manual. Close the tabs you do not want touched.

Undo works per tab. Forum threads confirm there is no single global undo, so each changed document needs its own Ctrl+Z.

The button has an optional confirmation prompt. In v8.9.8.1 and earlier it sits in Settings, Preferences, Searching, and the manual lists a checkbox beside the button as new in v8.9.9.

Turn the prompt on. One stray click otherwise rewrites every open file.

A whole folder

FieldWhat it takesExample
FiltersSpace-separated wildcards*.txt *.csv
ExclusionA leading ! on a filter!*.bin *.*
DirectoryThe folder to processA full folder path
In all sub-foldersRecurses into child foldersTick for nested exports
In hidden foldersAdds hidden sub-foldersTick only when needed

Replace in Files asks for confirmation, but files that are not open in a tab cannot be undone, as long-time forum regulars warn. Copy the folder first.

Run Find All before the replacement. It lists every hit in the Search results window, and for files open in Notepad++ it reads the unsaved editor buffer, not the copy on disk (Notepad++ User Manual).

Why do spaces survive, or lines merge, after Replace All?

Spaces survive because the character is not a plain space, and lines merge because the pattern includes line breaks. The Show Symbol marks tell you which case you have.

SymptomLikely causeFix
Lines merge, or the file collapses to one linePattern contains \s or \r\nCtrl+Z, rerun with \h
Spaces remain after Replace AllNon-breaking space, tab, or Wrap around offSearch \xA0 in Regular expression mode
\n\n finds nothingCRLF file, breaks are two charactersUse \R
Gaps stay between words after trimmingBlank Operations touch line edges onlyReplace [ ]{2,} with one space

Find out what the character is

  • Show Space and Tab puts a colored dot on each space and an arrow on each tab.
  • Show End of Line displays CR, LF or CR LF in small boxes.
  • Show Non-Printing Characters labels invisible ones with boxes like NBSP and ZWSP.
  • Show All Characters switches everything on at once (it’s the pilcrow button on the toolbar).

All four sit under View, Show Symbol. The Show Non-Printing Characters option, and with it the NBSP box, needs Notepad++ v8.5 or newer (Notepad++ Community).

The manual’s non-printing table covers dozens of characters, including the zero-width space (U+200B) and the ideographic space (U+3000). \h reaches neither, so search them as \x{200B} or \x{3000}.

One more disagreement to know about. A forum reply claims \s matches all 17 Unicode space characters, while the manual’s class listing for \s shows 10. An explicit \x{} search avoids relying on either.

Fix non-breaking spaces and tabs

Forum regulars split on the fix. A search for \xA0 hits only the non-breaking space, while \h also takes tabs and plain spaces, which one moderator calls risky because it widens the match past what you wanted.

  1. Press Ctrl+H and set Search Mode to Regular expression.
  2. Type \xA0 in Find what.
  3. Leave Replace with empty to delete them, or type \x20 to swap each one for a plain space.
  4. Click Replace All.

Tabs need a different tool. TAB to Space fills each tab up to the next tab stop, set by Tab Size for the file’s language (Notepad++ User Manual), so one tab becomes a different number of spaces depending on its column.

For a fixed count, switch to Extended mode, search \t, and type the spaces in Replace with.

Line endings that break patterns

A CRLF file stores every line break as two characters.

The status bar shows Windows (CR LF), Unix (LF) or Macintosh (CR), as the manual’s Editing chapter describes. In a CRLF file, \n\n never matches two blank lines in a row, because a CR sits between the two LF characters.

The safer habit is to write \R for any line break. It matches \r\n, \n and \r alike.

Mixed endings inside one file are a separate problem. Edit, EOL Conversion converts them all to one type before you run the pattern.

FAQ About Removing Spaces in Notepad++

Can Notepad++ trim trailing spaces automatically when I save?

Not through a preference. The Macro menu does hold a built-in Trim Trailing and save entry, which the Shortcut Mapper lists under Macros, so you can assign it a shortcut and save with that.

How do I replace spaces with underscores instead of deleting them?

Press Ctrl+H, type one space in Find what, type _ in Replace with, keep Search Mode on Normal, and click Replace All.

Run Trim Leading and Trailing Spaces first, or the line edges turn into underscores too.

How can I remove only the first space on each line?

Switch to Regular expression mode, search ^\h, and leave Replace with empty. The pattern matches a single blank at the line start, where ^\h+ would take them all.

Use ^\x20 instead if tabs and non-breaking spaces must stay.

Which method shrinks the gaps between columns without breaking alignment?

Column mode does it. Hold Alt+Shift with the arrow keys to select the blank block between columns, then press Delete.

Every row loses the same width, so the columns stay aligned. Forum answers add that this works for spaces, or for tabs that line up predictably.

Checking a Whitespace Cleanup Before You Save

A cleanup is safe to save once you’ve looked at the result with the whitespace marks on, run Find All on the pattern you removed, and kept a copy of the original.

The visual check goes first because merged lines and lost indentation show up at a glance. A Find All with zero hits proves only that the pattern is gone, not that nothing else changed.

Saving the copy last costs one extra file on disk per cleanup. That is cheaper than a lost original.

The copy matters because Notepad++ has no undo after a reload from disk, a gap the developers have chosen not to close, according to Notepad++ Community threads. A bad save followed by a reload cannot be reversed.

Position verified against the Notepad++ User Manual in November 2026.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.