Sorting in Emacs
susam.net
susam.net
You can sort the table using M-x org-table-sort-lines.
(Org mode tables, while simple plain text, are very readable. Anybody not using Emacs is going to find them tedious to edit though.)
True, though they tend not to be very fast when they grow. (I have a table with almost 2000 rows and editing it lags a bit.)
However, I've not had 2000 lines tables yet. Perhaps a good trick is to divide them into multiple ones? Perhaps one for each category of things, if you have any different categories.
In Git, except for when adding new files, I always use `git add -p` to stage changes. And it's definitely the number one thing I do with Magic in terms of frequency. I can confirm that if you're an `add -p` kinda person, you'll love Magit.
Until this thread I didn't know it could also do regions.
But I hear good things.
[“zoo”, “cow”]
Would become [“cow”, “zoo”]
Any tips on how to do that? Ideally using just built in functions…I usually insert newlines between the elements, use a line-based sort, then recombine the lines. That works but is more tedious than I’d like. I’ve never taken the time to wrap that logic it in a function because it’s infrequent and it seems like it’s always slightly different.
(defun sort-symbols (reverse beg end)
"Sort symbols in region alphabetically, in REVERSE if negative.
See `sort-words'."
(interactive "*P\nr")
(sort-regexp-fields reverse "\\(\\sw\\|\\s_\\)+" "\\&" beg end))
[1]: https://www.emacswiki.org/emacs/SortWordsFascinating to think how that doesn't mean it's bad advice. That is, unless your threat model includes targeted attacks by three-letter-scale adversaries — and then you maybe just don't open untrusted files in Emacs or use advice from strangers on HN.
Using Python's eval with empty globals and locals dicts (consult the docs) could make this safer, though I've only ever had to use that for defending against accidents rather than malice. And now this quick thing is becoming increasingly complicated, and maybe you'd be better off engineering it properly.
python-shell-send-statement, it's built into Emacs's python mode.
(defun my-sort-list ()
(interactive)
(when (not (looking-at (rx "[")))
(search-backward "[" nil t))
(when-let* ((line (thing-at-point 'sexp t))
(items (split-string (substring line 1 -1) (rx ",") t (rx blank)))
(sorted (string-join (sort items #'string-lessp) ", ")))
(kill-sexp)
(insert "[" sorted "]")))
I don't think I am using any non-builtin function here, though I think a few exceptions like s.el, f.el and dash.el can make things much easier. (let ((list (treesit-thing-at-point "list" 'nested))
(query "(list (string (string_content) @item))"))
(cl-loop for (_ . item) in (treesit-query-capture list query)
collect (treesit-node-text item t))) C-u M-| jq -c sortIt feels great when you can use narrow-to-region² to perfect a complex address or write a custom function with all your editor's power at your fingertips. With the sad, but obvious, drawback that the changes aren't linked in your disjoint shell and editor history.
¹ https://manpages.debian.org/jessie/moreutils/vipe.1.en.html
² Nowadays, that is more likely https://github.com/chrisbra/NrrwRgn for me.