reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator · 2022-12-21 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.
agg_sig_me · 2022-12-21 11:30
So I've been using vterm for a while and really like it, however a few emacs things seem to not work...for example I can't access previous items in the kill ring. And yesterday I wanted to write 12 0s, so I used C-u 12 C-u 0, and it just gave me one 0. Is there something I'm missing about why these standard emacs things aren't available?
hubisan-one · 2022-12-21 15:57
Have this handy function that
* selects a point in a buffer with a function (mostly using avy functions),
* then uses an evil operation like 'dd' on it or any function and
* finally returns to the point before calling the function
​
;; Examples
;; Uses evil avy to select a line to delete and then restores point.
(my-operate-on-point #'evil-avy-goto-line "dd")
;; Select a word with avy and delete it and then restore point.
(my-operate-on-point #'evil-avy-goto-word-or-subword-1 "dw")
(defun my-operate-on-point (fun keys-or-fun &optional state)
"Call a function FUN to jump to a point and simulate keys or call a function.
KEYS-OR-FUN is either a string with keys to simulate (to press) or a function to
call. Returns back to the starting position afterwards.
Switches to evil-state STATE and restores the current state afterwards. STATE
defaults to `normal'.
Use `apply-partially' if you want to bind arguments to FUN or KEYS-OR-FUN if
using a function or use a lambda."
(let* ((current-state evil-state)
(state (or state 'normal))
(marker (point-marker)))
(save-selected-window
(save-excursion
(unwind-protect
(progn
;; Switch to state.
(funcall (intern-soft (concat "evil-" (symbol-name state) "-state")))
(funcall fun)
;; Only call the function if point has moved. This is needed cos not
;; every function return nil if aborted.
(when (not (equal marker (point-marker)))
(cond ((stringp keys-or-fun) (execute-kbd-macro (kbd keys-or-fun)))
((functionp keys-or-fun) (funcall keys-or-fun))
(t (user-error
"%s" "keys-or-fun has to be a string or a function"))))))))
;; Restore previous evil-state in any case.
(funcall (intern-soft
(concat "evil-" (symbol-name current-state) "-state")))))
It requires evil but could easily be changed to not depend on it.
luiggi_oasis · 2022-12-21 16:33
What's the deal with all these completion framework I keep hearing about? Vertical elm ivy company and whatnot.
I think I have company in my init.el but I'm not even sure I'm actually using it (maybe I am and I'm just unaware). But why are they everywhere? I see them mentioned in at least every any two emacs threads.
eleven_cupfuls · 2022-12-21 18:12
They offer different ways of selecting an item (often a command to run, but also file names, text to insert, or other things) from a list. Some of them have extra features to allow you to do something special with the selection.
I don't think there is one called "elm"; you may mean "Helm". Company is a bit different than the others; it is for completing text in a buffer, rather than in the minibuffer (where command-type input goes).
Have a read through this and see if it answers some of your questions: https://www.masteringemacs.org/article/understanding-minibuffer-completion It doesn't cover specific external packages, but it explains what they are for and how they all work.
luiggi_oasis · 2022-12-21 18:32
Thank you for this! I think my most complex use of minibuffer completion is writing a keyword, possibly with an asterisk, and pressing TAB. My use case is so simple, maybe that's why I couldn't quite get why all these alternatives would be so popular.
agumonkey · 2022-12-22 13:15
Do you guys know a builtin or small snippet to open
/foo/bar/filename:L<line> directly to the line ?
natrys · 2022-12-22 18:14
If you follow org-link format (e.g. `[[/foo/bar/file::10]]`) then `org-open-at-point` will work anywhere. But it's simple to roll your own:
(defun my-open-file-at-point (location)
(interactive (list (thing-at-point 'filename t)))
(pcase (split-string location ":")
(`(,(and (pred file-exists-p) file)
,(and (pred (string-match-p "^[[:digit:]]+$")) line))
(progn
(find-file file)
(goto-char (point-min))
(forward-line (1- (string-to-number line)))))
(`(,(and (pred file-exists-p) file)) (find-file file))
(_ (error "Not a file"))))
agumonkey · 2022-12-22 18:57
thanks, that's way better than what I hacked. I'm surprised there's no builtin way, the filename[sep line][sep column] trick is used nearly everywhere for ages
[deleted] · 2022-12-25 15:28
[deleted]
StrangeAstronomer · 2022-12-25 22:56
**elfeed-dashboard filter**
Thought I'd share (partly in the hope of a code-review).
When I'm reading the **BBC**'s RSS feed, I'm not interested in the sports items so I have this item on my elfeed dashboard:
#+KEYMAP: b | bh/elfeed-dashboard-untag-by-regex "bbc" "/sport"
and this in my config.el:
(defun bh/elfeed-dashboard-untag-by-regex( feed regex )
"Untag items matching REGEX from FEED and view the result"
(elfeed-dashboard-query (concat "+unread +" feed " " regex))
(mark-whole-buffer) ;; FIXME - we're not supposed to use this - but what?
(elfeed-search-untag-all-unread)
(elfeed-dashboard-query (concat "+unread +" feed)))
republic_of_mao · 2022-12-26 20:41
You might be able to rewrite that as advice for find-file.
See [https://scripter.co/emacs-lisp-advice-combinators/#advice-combinators--around](https://scripter.co/emacs-lisp-advice-combinators/#advice-combinators--around). Parse for the "::#" before the call, strip it to keep tramp happy, then forward-line to the desired line after opening. Maybe I'll write this later in the week...
agumonkey · 2022-12-26 20:55
cool idea :)
gonna try
chmouelb · 2022-12-27 20:59
that's an interesting idea!
I have been using something like this for a while:
(defun my-avy-just-copy-line (arg)
(interactive "p")
(let ((initial-window (selected-window)))
(avy-with my-avy-just-copy-line
(let* ((start (avy--line))
(str (buffer-substring-no-properties
start
(save-excursion
(goto-char start)
(move-end-of-line arg)
(point)))))
(select-window initial-window)
(kill-new str)))))
(defun my-avy-copy-word (arg)
(interactive "p")
(save-excursion
(call-interactively 'avy-goto-symbol-1)
(let ((symbol (thing-at-point 'symbol)))
(when symbol
(kill-new symbol)
(message "\"%s\" has been copied" symbol))))))
not sure which ways is better, I want to be able to copy a "symbol" which is easy in "elisp" with (thing-at-point 'symbol) not sure how to do that in evil/vi the `yW` doesn't do exactly the same
just food for thoughts, thanks for sharing!
hubisan-one · 2022-12-27 22:10
I like your functions.
Evil has `yio` to copy a symbol:
(my-operate-on-point
(lambda () (call-interactively #'avy-goto-symbol-1))
"yio")
I would love to make a more generic version of that function, but I don't have any time on my hands.
chmouelb · 2022-12-27 22:51
nice one! thanks for the tip about `yio`
republic_of_mao · 2022-12-28 06:25
try this: https://www.emacswiki.org/emacs/FindFile#h5o-4