reddit / emacs
Is there a convenient way to reverse lines in a region (like tac command does)?
brightlystar · 2022-09-09 10:32
Is there an easy way to reverse lines in a region? Like if I have a buffer with this text
a
b
c
d
e
and I select a region consisting of the middle 3 lines and do the reverse lines thing, I should get a buffer like this
a
d
c
b
e
Possible?
[deleted] · 2022-09-09 10:45
[deleted]
lindyhopdreams · 2022-09-09 10:51
M-x reverse-region
[deleted] · 2022-09-09 10:51
[deleted]
redguardtoo · 2022-09-09 13:25
See `sort-lines` doc,
```
sort-lines is an autoloaded, interactive and natively compiled
function defined in sort.el.
Signature
(sort-lines REVERSE BEG END)
Documentation
Sort lines in region alphabetically; REVERSE non-nil means descending order.
Interactively, REVERSE is the prefix argument, and BEG and END are the region.
Called from a program, there are three arguments:
REVERSE (non-nil means reverse order), BEG and END (region to sort).
The variable sort-fold-case determines whether alphabetic case affects
the sort order.
```
pwnedary · 2022-09-09 14:31
If using Evil: `'<,'>g/^/m'<-`
jplindstrom · 2022-09-09 15:36
Relatedly, the other day I had to sort the contents of a single line string that had comma separated values, like so:
```
jkl,ghi,abc,def
```
And I had to muck about recording a macro to replace `,` with newlines etc. etc. so I could use `sort-lines`.
Is there perchance a function for what I needed?
Illiamen · 2022-09-09 15:55
Try `query-replace` (`M-%`) and `query-replace-regexp` (`C-M-%`). Use `C-q C-j` to insert the newline in the minibuffer. You can higlight a region and use `!` to replace all matches in the region. Otherwise, it starts replacing at point.
jplindstrom · 2022-09-09 16:00
Yeah I already did it that way, I'm asking about an existing Emacs function to sort columns of a row or similar.
(which I did search for at some length, but didn't find anything)
Shout-out to the tip about `C-q C-j` though, that's a very unintuitive way to search-and-replace newlines, so good to highlight.
jplindstrom · 2022-09-09 16:22
Should have Ducked it. Just found `sort-symbols` which seems to do the trick!
https://www.emacswiki.org/emacs/SortWords
Kwisacks · 2022-09-09 18:07
or just `'<,'>sort!`
Evil's fidelity is impressive. I guess Emacs already had all the infrastructure in place (look at the `reverse-region` and `sort-lines` commandsin the other comments.
harizvi · 2022-09-09 22:12
For similar scenarios I've found shell-command-on-region by itself, and with prefix arg very handy.
In this case, it will be a simple C-u M-| tac <enter>
By itself, the output goes to a temp buffer, so you can build the command that does what you need. You can also use a series of pipes (just like you would in a shell). For example, you can use the following to add a line number: tac | awk '{print NR, $0}'
Once your command is final, you can use the prefix arg to replace the region with the output.
Similarly, the question above about sorting comma-separated fields can be done using a pipeline of: tr ',' '\\n'| sort | tr '\\n' ','
It was the first Emacs magic I learnt those decades ago, and have used it hundreds of times over the years :-)
Patrick-Poitras · 2022-09-10 02:31
M-x butterfly moment