Fork me on GitHub
#spacemacs
<
2020-12-22
>
jumar11:12:02

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 tried

practicalli-johnny11:12:18

I wonder if iedit or multiple cursors would be useful for this... Will check when back at a computer

jumar12:12:51

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.

practicalli-johnny12:12:24

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.

jumar12:12:51

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.

practicalli-johnny12:12:08

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

practicalli-johnny12:12:58

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

practicalli-johnny12:12:56

Oh and when finished, g r q to kill all the cursors and go back to just one.

spfeiffer13:12:12

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.

spfeiffer13:12:29

C-o instead of C-q C-j works, too.

practicalli-johnny15:12:15

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