Find and replace white space with tabs
-
Hi everybody I’m looking for help for this issue.
I have a txt file with more than 7000 lines with this content:
10 FIRST FOO 1001 SECOND FOO 100101 THIRD FOO 10010101 FOURTH FOO 10010102 EVEN MORE FOO 10010103 STILL FOOING 11 BAR 1102 SECOND BAR 110201 THIRD BAR 11020101 FOURTH BAR 11020102 EVEN MORE FOURTH BAR
and I need it to become like this:
10 FIRST FOO 1001 SECOND FOO 100101 THIRD FOO 10010101 FOURTH FOO 10010102 EVEN MORE FOO 10010103 STILL FOOING 11 BAR 1102 SECOND BAR 110201 THIRD BAR 11020101 FOURTH BAR 11020102 EVEN MORE FOURTH BAR
I thought that i can try to search for white space in third column 3, 5, 7 and 9 and replace it with three tabs , but when i run the search with regular expression like
^.{2}\s
to find the blank in column 3 and then go to replace it, the editor replace all the characters from the start of the line, how can i replace only the character (or the whitespace) in column 3 (and 5, 7, 9)?
Is there maybe another way to accomplish such result?
TIA everybody for your attention.
-
Do:
Find:
^\S+
Replace:$0
and then add bunch of spaces after the0
Search mode: Regular expressionYou’ll obtain something like this:
10 FIRST FOO 1001 SECOND FOO 100101 THIRD FOO 10010101 FOURTH FOO 10010102 EVEN MORE FOO 10010103 STILL FOOING 11 BAR 1102 SECOND BAR 110201 THIRD BAR 11020101 FOURTH BAR 11020102 EVEN MORE FOURTH BAR
Then put your caret at some reasonable spot in the big bunch of spaces on line 1, and press Alt+Shift+b (requires N++ 8.5 or later). Note the column you are in from the N++ status bar.
Move to the last line of the file, and get to the same column. Press Alt+Shift+b again.
You’ll obtain:
Finally, press and hold Ctrl while you tap and release the Delete key, to get:
From there you can use the Backspace key or the space bar to reduce or increase the amount of space on all of the lines simultaneously.
-
Alan, sorry, but does not work for me …
also
Alt+Shift+b
does not provide any events …
i have latest 8.5.2 np++ version -
sorry, but does not work for me
My guess is that you didn’t notice the “then add a bunch of spaces after the 0”. Your replacement needs to be a long piece of text – the literal
$0
then a bunch of spaces.Since spaces at the end of a regex replacement can be hard to see, I like using
\x20
(which is regex speak for “ASCII HEX 20” which is the space character) when I post in the forum, like:$0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
also Alt+Shift+b does not provide any events …
Yes it does. You just need to do two of them.
012345 67890A BCDEFG HIJKLM
If you have your cursor between
0
and1
, then hitAlt+Shift+B
, then move your cursor to between theL
and theM
and hitAlt+Shift+B
, then you will have a rectangle selection from the1
to theL
.After the first
Alt+Shift+B
, if you looked at the Edit menu, you would see a checkmark next to Begin/End Select in Column Mode, indicating that you’d done the begin but not yet done the end.
-
@namx3249 said in Find and replace white space with tabs:
Alan, sorry, but does not work for me …
Maybe there is some misinterpretation of what you actually have, and/or what you want.
You said something about “three tabs” – do you really want to end up with tab characters in your file?
-
@Thomas-Allister said in Find and replace white space with tabs:
I thought that i can try to search for white space in third column 3, 5, 7 and 9 and replace it with three tabs , but when i run the search with regular expression like
^.{2}\s
to find the blank in column 3 and then go to replace it, the editor replace all the characters from the start of the line, how can i replace only the character (or the whitespace) in column 3 (and 5, 7, 9)?
You want to use capture expressions, so that you include the original text in the replacement:
Find: ^(.{2})\s
Replace: \1\tThe parentheses create a capture expression. They are numbered from 1 to 9 from the left (counting by where the opening parenthesis occurs, in case parentheses are nested). Capture expressions get more complicated, but that’s the simple version.
It looks like you could do all your lines at once with:
Find: ^(\d+)\s
Replace: \1\t -
@PeterJones
thanks for you clarification. now i’ve understand. and all work fine for me tooYou said something about “three tabs” – do you really want to end up with tab characters in your file?
really i don’t understand what you’re referring to…
-
@namx3249 said in Find and replace white space with tabs:
really i don’t understand what you’re referring to…
Then it isn’t important, as long as everything is now working fine for you.
-
so, the final solution is:
find:^\S+
replace:$0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
put caret on blank space in a space between text in the first line, holdAlt+Shift+B
(Edit menu - Begin/End Select in Column Mode), with arrow key put on the last line, hold againAlt+Shift+B
then withCtrl + Del key
all text on right side is aligned to the cursor. donesimply and interesting. thanks folks for this new trick
-
Just be careful not to do Ctrl+Delete in this situation:
If you do, it will remove
FIRST
on line 1 andBAR
on line 7.Always have at least one space character on each line to the right of the “tall skinny” caret.