How can I remove lines that begin with spaces in Notepad++?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over
--
Chapters
00:00 How Can I Remove Lines That Begin With Spaces In Notepad++?
00:45 Accepted Answer Score 44
01:27 Answer 2 Score 18
02:16 Answer 3 Score 4
02:40 Answer 4 Score 4
03:25 Answer 5 Score 2
03:54 Thank you
--
Full question
https://superuser.com/questions/1562674/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#notepad++ #regex
#avk47
ACCEPTED ANSWER
Score 44
- Ctrl+H
- Find what:
^\h+.*$\R?
- Replace with:
LEAVE EMPTY
- CHECK Wrap around
- CHECK Regular expression
- UNCHECK
. matches newline
- Replace all
Explanation:
^ # beginning of line
\h+ # 1 or more horizontal spaces
.* # 0 or more any character but newline
$ # end of line
\R? # optional linebreak
Screenshot (before):
Screenshot (after):
ANSWER 2
Score 18
It's possible with a pure regex solution (see @Toto's answer) but perhaps the following solution, using bookmarks, is easier to grasp:
Open the Find dialog
Go to the Mark tab, search for
^
(that's 2 characters: a caret indicating 'the beginning of the line' and a space) and enable the 'Bookmark line' option. I assume your Search Mode is already set to Regular expression. Hit the 'Mark All' button.Delete all bookmarked lines (Search → Bookmarks → Remove bookmarked lines)
ANSWER 3
Score 4
You can do this:
Hit Ctrl+H for find and replace.
Check mark regular expression.
Use the Regex
^\s+.*
in the Find box.Keep Replace box blank.
Click Replace All.
ANSWER 4
Score 4
Another solution which would seem to apply to your example would be to Select All
(Ctrl-A), then go to Edit
>Line Operations
>Sort Lines [pick one of them that best applies]
, and this will group all the lines beginning with spaces together in the sort, and you can then easily just select and delete them. This only works in this case because the lines you want to keep seem to be numerically sorted already. Obviously this might not apply if you don't want to change the sorting of the other lines.
Personally, I have Sort Lines Lexicographically Ascending
and Sort Lines as Integers Ascending
assigned to hotkeys because I use them so frequently for just this sort of thing.
ANSWER 5
Score 2
I know this a Notepad question, but I can’t resist.
In vim, you could do
:global/^\s/delete
Which says to delete all lines starting with space.
You can script for automation like
printf '%s\n' 'global/^\s/delete' 'write' 'quit' | vim -es --clean file
Alternatively, with sed if you need to process a stream:
sed '/^[[:space:]]/d'