reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator · 2022-05-11 08:15
This is a thread for smaller, miscellaneous items that might not warrant a full post on their own.
[See this search for previous "Weekly Tips, Tricks, &c." Threads.](https://www.reddit.com/r/emacs/search?q=Weekly+tips&restrict_sr=on&sort=new&t=all)
Don't feel constrained in regards to what you post, just keep your post vaguely, generally on the topic of emacs.
pathemata · 2022-05-11 13:12
Something amazin that I have been using recently is `ripgrep-all` as the `consult-ripgrep` command to search in pdfs.
It is amazing with the `orderless` dispatchers to control the search filtering.
I use `!` to exclude a string and `=` to match exactly.
Also amazing with `embark-collect` which allows collapsing features.
Or within the collect buffer use `consult-line` to further filter.
And even open the pdf.
nicholas_hubbard · 2022-05-11 17:10
This code lets you toggle between two buffers in a window. I adapted the code from evil-mode's `evil-alternate-buffer`.
(defun my/alternate-buffer (&optional window)
"Return the last buffer WINDOW has displayed other than
the current one"
(let* ((prev-buffers (window-prev-buffers))
(head (car prev-buffers)))
(if (eq (car head) (window-buffer window))
(cadr prev-buffers)
head)))
(defun my/switch-to-windows-last-buffer ()
(interactive)
"Switch to current windows last open buffer"
(let ((prev-place (my/alternate-buffer)))
(when prev-place
(switch-to-buffer (car prev-place))
(goto-char (car (last prev-place))))))
oantolin · 2022-05-11 17:22
Ooh, I'd never heard of ripgrep-all, that sounds fantastic, thanks for mentioning it! I've wanted ripgrep inside compressed files for a while and PDF's would be a very welcome bonus.
Some tips: try consult-focus-lines in the embark collect buffer if you want longer lasting filtering that consult-line; also I recently added revert-like functionality to Embark collect buffers, pressing `g` reruns the command you got the candidates from but leaves you in the minibuffer so you can tweak the input if you want before re-collecting( same for embark export buffers).
trae · 2022-05-11 17:26
I'm having a bit of a challenge with `auto-revert-mode`.
I use my iPhone to add tasks to my `inbox.org`, which is then synced via dropbox to my Mac (Emacs 28.1).
It doesn't seem to work. When I go to my inbox on Mac and try to do things to the buffer, emacs tells me that something has changed. But there are no local changes to the buffer, why hasn't it auto-reverted?
This is what I got in my config:
(global-auto-revert-mode 1)
as far as I can tell, the mode is enabled. Any idea what's going on?
nnreddit-user · 2022-05-11 18:27
The built-in `filenotify.el` module doesn't quite rise to the admittedly tough challenge of wrangling the broad array of kernel-specific notify mechanisms. I'll retry turning on `global-auto-revert-mode` every time I upgrade my Linux distro, but to date, always turn it back off. I don't know why I believe it'll suddenly start working -- it's not like filenotify.el witnessed a substantive change in several years.
mmarshall540 · 2022-05-11 22:24
Here's a little solution to a minor complaint about `isearch` that I think is fairly common.
# The Problem
TLDR: See [this old StackExchange question](https://emacs.stackexchange.com/questions/32373/go-to-start-of-search-string-after-ret)
Often, I'll want to go to a precise location in a buffer to correct a typo. If that location is behind point (as it usually is if I notice the typo shortly after entering it), then `isearch-backward` works great. You press "C-r" and type some characters starting with your target until the location becomes the current result. As soon as the cursor jumps to your target location, you can use an editing command like "C-t", which exits isearch automatically for you. Or you can press "C-m" to exit isearch-mode and then start inserting text at that location. Wonderful!
But it's a little less perfect when your target is *after* point. This is because with `isearch-forward`, point lands at the end of your search result, instead of the beginning as it does with `isearch-backward`.
You could type your search starting with some arbitrary number of characters in front of the target. But then you have to decide in advance how many characters to use, and if you don't use enough, there might be too many results. And then if you have to keep typing past your target character to sufficiently narrow the results, point won't land where you want it.
Another strategy would be to do the same thing as when searching backwards, start with the target character and just type characters until your target location is selected. But now point is at the other end of the result, instead of where you want it. "M-b" would exit and move point to the beginning of the word, but that assumes there are no word boundaries in your search result, and besides, your actual target won't always be the beginning of a word. Often it will be somewhere in the middle of a word.
Or you could use C-r to move point to the beginning of the result. But then you still have to press C-m to exit the search and start inserting text at that location. And that's 2 key-presses just to finish your search, not to mention all the key-presses you did to start.
So we can fix this.
# My Solution
(defun my/isearch-bor-exit ()
"Ensure point is at beginning of isearch result, then exit."
(interactive)
(when (< isearch-other-end (point))
(goto-char isearch-other-end))
(call-interactively 'isearch-exit))
(define-key isearch-mode-map ?\M-m 'my/isearch-bor-exit)
So now if I'm using isearch to get to a precise location (as opposed to just searching for a word), I can type beginning with the character at the target location, stop typing after it becomes part of the result, and press "M-m" to exit the search and make my correction. No more having to stop and think about whether I'm going backwards or forwards and whether I'll have to move point back before exiting isearch.
# The Conclusion
A minor problem with a fairly simple solution, but it made me happy to stop and solve it.
I think I'll be using isearch now for a lot of things that I used to use Avy for. But Avy is still great for a bunch of reasons. Not the least of which is the `avy-isearch` command which comes in handy when there are a lot of isearch results that would require too much typing to narrow down. (And it always puts point at the beginning of the result, never at the end).
trae · 2022-05-11 22:50
The thing is, Emacs knows that the file has been changed on disk! It warns me right away if I try anything. Why isn't reverting it?
pathemata · 2022-05-12 05:43
Thanks for the tips. First time I've heard of `consult-focus`, it's great. I saw the reverting feature as well and is very useful, thanks!
h-m-b-tu · 2022-05-12 07:56
I do the same with
```
(defun back-and-forth-buffer ()
(interactive)
(switch-to-buffer (other-buffer (current-buffer))))
```
oantolin · 2022-05-12 09:32
I made a small mistake: the function is called consult-focus-lines, as you probably figured out. I've corrected the comment I made above.
SlowValue · 2022-05-12 11:08
I scratched a similar itch by writing two functions, called `isearch-exit-at-front` and `isearch-exit-at-end`. Then bound them to `<return> ... exit-at-front` and `C-<return> ... exit-at-end`. Now I always end with point at an deterministic location, no matter if I was searching forward or backward.
(defun isearch-exit-at-front ()
"Exit isearch, at the front of search match."
(interactive)
(isearch-exit)
(when isearch-forward
(goto-char isearch-other-end)))
nicholas_hubbard · 2022-05-12 13:26
Nice that is simpler and better named! Im gonna switch to this.
No-Command-791 · 2022-05-12 18:37
Kill the `*Calendar*` buffer after use (if, like me, you're persnickety about buffer clutter):
(defun org-kill-calendar (arg &rest _args)
(unless (equal arg '(4)) (kill-buffer "*Calendar*")))
(dolist (fn '(org--deadline-or-schedule org-time-stamp))
(advice-add fn :after #'org-kill-calendar))
Edit: I realized that this fails to kill the buffer when using the prefix argument to include the time in inserted timestamps. Improved version:
(defun org-kill-calendar (&rest _args)
(when (get-buffer "*Calendar*") (kill-buffer "*Calendar*")))
github-alphapapa · 2022-05-12 19:11
I'd guess that Emacs checks whether the file has been modified when you try to modify it, so it doesn't "know" until then.
FWIW, I've used `auto-revert-mode` for my Org files for years, and it's always worked for me, but I use Linux, so your using MacOS might be the problem (i.e. Emacs's support for MacOS).
_viz_ · 2022-05-13 10:44
Here's a command that uses multi-occur to search across marked files in dired.
(defun vz/dired-do-multi-occur (regexp &optional nlines)
"Search for REGEXP in the marked files.
This uses `multi-occur' to do the job. See its docstring for the
meaning of NLINES.
See `dired-do-find-regexp' and `dired-do-search'."
(interactive (occur-read-primary-args))
(let ((files (dired-get-marked-files))
bufs)
(dolist (f files) (push (dired--find-file #'find-file-noselect f) bufs))
(multi-occur bufs regexp nlines)))
As referenced in the docstring, there are two more functions (to my knowledge) that uses xref and fileloop to do the same job. I chose to use multi-occur because of occur-edit-mode.
EDIT: This command should probably check if the file is changed on disk before searching for the regexp but that it is not a common occurrence (no pun intended) for me so I haven't bothered to do it.
EDIT(2): You might also the following command useful,
(defun vz/occur-save-buffers (&optional no-confirm)
"Save all the buffers `occur' searched in.
If the optional argument NO-CONFIRM is non-nil, then don't ask
the user for confirmation when saving the buffers."
(interactive)
(let ((occur-buffers
(vz/filter-map (lambda (b)
(if (overlayp b)
(overlay-buffer b)
b))
(caddr occur-revert-arguments))))
(save-some-buffers no-confirm
(lambda () (member (current-buffer) occur-buffers))))
(occur-cease-edit))
Phil-Hudson · 2022-05-13 11:03
I have the behavior you want without advising either function, but I can't remember what I did to get it. I suspect there's a user option.
dmartincy · 2022-05-13 11:23
There is a very old mode in vanilla Emacs to switch or interact with buffers in an "electric" way: M-x electric-buffer-list. It's interesting to see that people explored some of the same ideas of recent completion packages a few decades ago. :-)
Phil-Hudson · 2022-05-13 12:00
Thanks for this, trying to copy it into my init and I'm finding function `dired--find-file` is not loaded. It doesn't appear to be defined anywhere in the Emacs source tree or in any package I have installed. Running Emacs 27.2.
Phil-Hudson · 2022-05-13 12:03
`vz/filter-map` is obviously one of yours, please add.
No-Command-791 · 2022-05-13 13:07
Intriguing! Let me know if you end up remembering :-)
_viz_ · 2022-05-13 15:34
>Thanks for this, trying to copy it into my init and I'm finding
>function dired--find-file is not loaded. It doesn't appear to be
>defined anywhere in the Emacs source tree or in any package I have
>installed. Running Emacs 27.2.
Maybe it was defined in Emacs 28 or above? I don't have a clone of
the Emacs repo with the entire history right now so I can't check. It is definitely not autoloaded on my end either but I
have the command under a `with-eval-after-load` block.
EDIT: It looks you can replace it with jsut find-file-noselect. I used this function since I thought it handled revertion of buffer from a quick glance.
>vz/filter-map is obviously one of yours, please add.
Didn't notice that, sry. Here you go,
(defun vz/filter-map (pred list)
"Equivalent to (seq-filter #'identity (seq-map PRED LIST))."
(let (res)
(mapc (lambda (x)
(when-let ((x* (funcall pred x)))
(push x* res)))
list)
(setq res (nreverse res))
res))
Phil-Hudson · 2022-05-13 21:20
Thanks for following up. Given what you say, here's my version:
;; Based on
;; https://www.reddit.com/r/emacs/comments/un4wf8/comment/i8fox6s/?utm_source=share&utm_medium=web2x&context=3
(defun ph/dired-do-multi-occur (regexp &optional nlines)
"Search for REGEXP in the marked files.
Uses `multi-occur' to do the job. See its docstring for the
meaning of NLINES.
See `dired-do-find-regexp' and `dired-do-search'."
(interactive (occur-read-primary-args))
(multi-occur
(mapcar #'find-file-noselect (dired-get-marked-files))
regexp nlines))
oantolin · 2022-05-14 10:59
I use electric-buffer-list but cannot figure out which ideas of recent completion packages you mean.
holgerschurig · 2022-05-14 11:18
I use something similar, bound to F6 in my case:
(defun my-switch-to-buffer ()
"If there is only one window displayed, swap it with previous buffer.
If there are two windows displayed, act like \"C-x o\"."
(interactive)
(if (one-window-p t)
(switch-to-buffer (other-buffer (current-buffer) 1))
(other-window -1)))
The behavior is that normally it toggles the buffer in my whole window between what I last used. But when I split my screen, then it toggles between them.
Now, sometimes I want to get something earlier than just "last used" buffer, e.g. the 2nd-last-used. For this I use nswbuff.el:
(after! nswbuff
(setq nswbuff-display-intermediate-buffers t
nswbuff-exclude-buffer-regexps '("^ .*" "^\\*.*\\*")))
(map! "S-<f5>" #'nswbuff-switch-to-previous-buffer
"S-<f6>" #'nswbuff-switch-to-next-buffer)
(If you don't use Doom, you can use eval-after-load and etc, or you can also use-package with :bind and :custom clauses)
holgerschurig · 2022-05-14 11:21
It's definitely in Emacs 28.1
(defun dired--find-file (find-file-function file)
"Call FIND-FILE-FUNCTION on FILE, but bind some relevant variables."
;; Bind `find-file-run-dired' so that the command works on directories
;; too, independent of the user's setting.
(let ((find-file-run-dired t)
;; This binding prevents problems with preserving point in
;; windows displaying Dired buffers, because reverting a Dired
;; buffer empties it, which changes the places where the
;; markers used by switch-to-buffer-preserve-window-point
;; point.
(switch-to-buffer-preserve-window-point
(if dired-auto-revert-buffer
nil
switch-to-buffer-preserve-window-point)))
(funcall find-file-function file)))
_viz_ · 2022-05-14 15:43
I guess in a sense it is like ivy, the only non-default completion system I've used, in that it shows a preview of all the "candidates" and there are also actions that you can perform on said " candidates." Tho, it has been too long since I last used electric-buffer-list so my recollection is poor.
oantolin · 2022-05-14 17:16
Displaying a static list of things you can act on sounds more like dired than ivy to me, but you may be right anyway. 🤷
agumonkey · 2022-05-14 18:35
tiny functions/packages I found this week:
- cycle-spacing: kills space around point, or leave one space. Naively I use M-d or M-z but cycle-spacing is "more proper"
- string-inflection.el: to cycle between camel|snake|...|case. Very useful in python/js/html codebase
take care yall
where_Is_My_Towel · 2022-05-14 18:56
I'm looking for some advice on making org-mode's babel play nice with octave and python
​
I want to be able to run a block like:
`#+begin_src`
`octaveA = [1 2 3 4]`
`B = [1 3 4 5]`
`plot(A,B)`
`#+end_src`
and get the resulting plot as an image I can inline toggle. Looking at the current docs I can't quite figure out how to make that work (\[:session octave :output file\] as header arguments don't get me where I need to be) . I'm trying to mimic a literate programming environment provided by matlab's live editor. Advice?
Phil-Hudson · 2022-05-14 21:40
That's the answer then. Thanks for posting.
takeonrules · 2022-05-15 01:24
I'm able install `ripgrep-all` and set this for `consult-ripgrep`. I search for a phrase in a PDF and `consult` renders the text and the page number. However, when I select the result, it doesn't go to that PDF page. Any thoughts?
pathemata · 2022-05-15 08:07
this would be a nice feature.
sudhirkhanger · 2022-05-17 05:07
1. Is there a cheat sheet that you guys use to help your markdown friends quickly convert their markdown notes to Org Mode?
2. How can non-emacsers use Org Mode online?
A friend wants to contribute to my github repo which is in org-mode.
organic-bookworm · 2022-05-17 09:41
>Is there a cheat sheet that you guys use to help your markdown friends quickly convert their markdown notes to Org Mode?
You could use [pandoc](https://pandoc.org/try/?text=&from=markdown&to=org&standalone=0).
>How can non-emacsers use Org Mode online?
When I looked that up last time (1 year ago), [organice](https://organice.200ok.ch/sample) seemed the most prominent (in terms of supported features) app.
And github also supports org-mode markup files.
sudhirkhanger · 2022-05-17 16:46
Thank you.