What's the best way to replace certain characters in a String with new lines?
I tried with the usual "vim-style" (https://stackoverflow.com/questions/71323/how-to-replace-a-character-by-a-newline-in-vim) replace but \r
just produces ^M
in the source buffer.
E.g. I have a stacktrace as a single-line string and want to split it into multiple lines
a.clj: 25 b.clj: 58 b.clj: 365 d.clj: 51 java.lang.Exception: ...
# should become:
a.clj: 25
b.clj: 58
b.clj: 365
d.clj: 51
java.lang.Exception: ...
I triedI wonder if iedit or multiple cursors would be useful for this... Will check when back at a computer
I ended up using sed
like this:
echo "a.clj: 25 b.clj: 58 b.clj: 365 d.clj: 51 java.lang.Exception: ..." | sed -E $'s/(: [0-9]+)/\1\\\n/g' | tee >(pbcopy)
Still would be useful to know how to do it in emacs.Using iedit:
SPC v v
with as many v's needed to select the region
SPC n r
to narrow to the region (so I can select spaces just from that region
v
on one of the spaces to select the space character as a pattern
SPC s e
to create iedit cursors on each space, n
to jump to the next cursor and TAB
to toggle the iedit cursor, removing every alternate cursor.
i
for insert mode and RET
to create new lines
ESC
to leave insert mode, ESC
to leave iedit mode
It sounds a lot, but its very quick to do if used to using iedit and narrowing.
I didn't quite get the thing with n
and TAB
- couldn't make it work.
I can split it that way with simply replacing every space but there can be 3(?) spaces in each row (one of them is ommited from the example so there are only two)
I basically need to split on digits/regex, I think.
Multiple cursors is simpler
v
to visually select the first space where you want to cut the line
C-n
to place a cursor and jump to the next space
C-t
to skip placing a cursor at that position and jump to the next space
Then alternate C-t
to skip a cursor at the space and C-n
to place a cursor at the place
i
and RET
when you have all the cursors needed
If there is a varing number of spaces, simply C-t
until you get to the place where a space should have a return charater and press C-n
Oh and when finished, g r q
to kill all the cursors and go back to just one.
I did it with SPC SPC replace-string
, then the character i want to replace, then C-q C-j
as replacement. Most likely not the most elegant way, but did the trick.
C-o
instead of C-q C-j
works, too.
I created a quick video as an example of using iedit and multiple cursors https://youtu.be/SCEQtSSb7Tc I shoud do more of these and join them up into a little demo of these really useful tools
Replacing with \n instead of \r works for me.
:s/;/\n/g