reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator · 2022-04-06 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.
desquared · 2022-04-06 11:16
How long before emacs 28 is available in Ubuntu? (Say, Ubuntu 21.10 -- not the LTS stream.)
Or, what's a good way to install emacs outside of the standard repo? Is there...a PPA? A Snap? Flatpak? I can't keep track of all that stuff these days, I was raised in the ancient days when things were just deb/apt repositories...
[deleted] · 2022-04-06 11:22
[removed]
T_Verron · 2022-04-06 11:36
In the past I was using this repository [https://launchpad.net/\~kelleyk/+archive/ubuntu/emacs](https://launchpad.net/~kelleyk/+archive/ubuntu/emacs) which has emacs compiled with modules and widgets. But it doesn't have 28 yet.
PriorOutcome · 2022-04-06 13:31
I wanted to be able to expand yasnippets within other yasnippets (so here tab would jump to the next position instead of trying to expand snippet), surprisingly all I had to do was:
`:bind ("C-<tab>" . yas-expand)`
So C-<tab> expands a snippet within a snippet, and everything just worked as I'd hoped. Once I'm done with the nested expansion <TAB> just moves on to the outer one. \*shrug\*
shaqfooVA · 2022-04-06 16:39
How does one use a specific mode's yasnippet completions inside that mode's org-babel source block?
For example, how do I use python snippets inside `jupyter-python` source blocks?
jcmkk3 · 2022-04-06 17:06
One option would be poly mode. https://github.com/polymode/polymode
kromobrn · 2022-04-06 19:55
Is there a way to count the number of VISIBLE lines in the buffer?
In org mode I have some \[collapsed\] headlines I want to edit. I record a macro editing in the first one + going to the next. Now I want to execute it N-1 more times for the left headings. I couldn't figure out an simple way to get this number of headings left. `count-words-region` include all collapsed lines, which is not useful in my case.
kromobrn · 2022-04-06 20:49
This behavior can be set as the default one with `yas-triggers-in-field` if I'm not mistaken
rguy84 · 2022-04-06 21:14
In org, I know you can do C-c C-x d to add a drawer. Other than writing a function, is there a way to set up non-property drawers so their names can be easily reused?
PriorOutcome · 2022-04-06 21:59
you're right, I thought there might be an easier way (:
you might get an expansion when you don't want it, but I think it's the way to go, thanks!
TeMPOraL_PL · 2022-04-06 22:48
`shortdoc` - one of the new things in Emacs 28.1 - is great for maintaining your own "cheat sheets" of Elisp functions as you discover them. For example, eval this in your Emacs session:
(define-short-documentation-group my-datetime
"Querying current time"
(current-time
:eval (current-time))
(float-time
:eval (float-time))
"Time formats"
(time-convert
:eval (time-convert (current-time))
:eval (time-convert (current-time) 'list)
:eval (time-convert (current-time) 100000)
:eval (time-convert (current-time) 200000)
:eval (time-convert (current-time) t)
:eval (time-convert (current-time) 'integer))
(float-time
:eval (float-time (current-time))))
After this, the `my-datetime` group will show as an option in `M-x shortdoc-display-group`. Additionally, if you view help for any of the functions mentioned above, the Help buffer will refer back to the `my-datetime` shortdoc group!
The example used here is a cheatsheet I just started writing for myself, as I've been researching the built-in time functions. For additional instructions on use, see `define-short-documentation-group` macro. For use examples, jump to the source of `define-short-documentation-group` and scroll down a bit - the default shortdoc groups are defined there.
licjon · 2022-04-07 00:51
EDIT: improved this by using zshrc instead to avoid minor error with bash. Wrote a function that runs a function in my bashrc that makes and runs a C program on my current buffer (a C program). I bound it to SPC-c.
`(defun crun ()`
`(interactive)`
`(setq command (concat "crun " (file-name-base (buffer-file-name))))`
`(shell-command command))`
To use your zshrc in emacs, you needs this in your init file:
`(setenv "SHELL" "/bin/zsh")`
`(setq shell-file-name "/bin/zsh")`
`(setq shell-command-switch "-ic")`
`(use-package exec-path-from-shell`
`:ensure t`
`:if (memq window-system '(mac ns x))`
`:config`
`(setq exec-path-from-shell-variables '("PATH" "GOPATH"))`
`(exec-path-from-shell-initialize))`
This is the function in my zshrc (and zshenv just in case):
`crun () {make $1 && ./$1;}`
oantolin · 2022-04-07 01:01
Use the numeric argument 0 instead of figuring out what N-1 is. 0 means run until error, and trying to move to a non existing heading will produce the error.
SasukeUchiha231 · 2022-04-07 04:47
Try using org-edit-src-block, inside the org babel block. It opens a new buffer with the major mode depending on the language. It's bound to C-c ' by default.
kromobrn · 2022-04-07 05:19
Interesting! I'm using evil-mode with its macros and this don't seem to work with neither 0 or the universal-arg (which means infinite count according to the docs). But apparently I can just use a very large number and it does the trick, at least with the buffer narrowed to the headlines only.
But regardless of that, several times I wanted to know the number of headlines and would end up counting manually in my head. Now I learned and can just run `(length (org-map-entries t))`.
Phil-Hudson · 2022-04-07 12:12
What do you mean by "reused"?
rguy84 · 2022-04-07 12:37
I am not interested in org-roam, so i want to make a drawer called related or something. I occasionally have a few meetings or tasks that reoccur. I found that making a link for a past headline and drop it into the one I am currently working on is helpful, for example a quarterly meeting. Instead typing `C-c C-x d RELATED`, i was wondering if there was a way for emacs to default to related.
abhibansal53 · 2022-04-07 15:27
Exactly what I was looking for, thanks for making this. Will try on weekend.
ntrysii · 2022-04-07 22:05
how to unpack cons into a function arguments
for example (evil-delete (beg . end))
to be (evil-delete (unpack (beg . end)))
[deleted] · 2022-04-07 23:48
[deleted]
[deleted] · 2022-04-08 07:28
[deleted]
Phil-Hudson · 2022-04-08 08:49
I see. Seems like a good idea, and looking at the source code for `org-insert-drawer`, I don't see any convenient way to do it currently. The only relatively simple thing I can think of that you might try is a keyboard macro.
Phil-Hudson · 2022-04-08 09:00
Using the destructuring capabilities of `cl-defun`, and assuming `evil-delete` a) takes two arguments, each a number or marker, and b) just deletes the given region in a buffer (I don't use evil):
(cl-defun my-evil-delete ((beg . end))
(evil-delete beg end))
(my-evil-delete '(0 . 88)) ; deletes from 0 to 88
rguy84 · 2022-04-08 11:19
I haven't tried to save one yet.
rguy84 · 2022-04-08 11:19
I haven't tried to save one yet.
RobThorpe · 2022-04-08 14:07
I'm having problems with `M-x run-python`on Windows. It works on Emacs 27.2, but on Emacs 28.1 it errors out with the following:
Python was not found; run without arguments to install from the Microsoft
Store, or disable this shortcut from Settings > Manage App Execution Aliases.
Process Python exited abnormally with code 49
Has anyone seen this problem, or does anyone have a fix for it?
ntrysii · 2022-04-08 14:54
thanks but i was hoping for something like a function, in this case i would just use let instead.
Phil-Hudson · 2022-04-08 15:25
That *is* a function!
As regards `let`, are you familiar with `-let` from package dash.el? It lets you do similar destructuring:
(-let (((beg . end) '(0 . 88)))
(evil-delete beg end))
ntrysii · 2022-04-08 20:11
sorry for that, i know it is a function but i requires redefining all of my operators which is something i dont want to do.
but the other one with (-let ) is much better than normal let, Thanks.
papercatlol · 2022-04-08 21:31
Somewhat related: I personally don't like <TAB> being hijacked by yasnippet, so I unbound it and use *hippie-expand* with *yas-hippie-try-expand* to trigger snippet expansion, which also works for nested snippets.
papercatlol · 2022-04-08 21:40
FYI emacs has a *compile* command, which can run whatever script you want and allows you to navigate between compilation errors.
Compile the program including the current buffer. Default: run make.
Runs COMMAND, a shell command, in a separate process asynchronously
with output going to the buffer *compilation*.
You can then use the command C-x ` to find the next error message
and move to the source code that caused it.
If optional second arg COMINT is t the buffer will be in Comint mode with
compilation-shell-minor-mode.
Phil-Hudson · 2022-04-08 21:48
And I should apologize in my turn, for not realizing that of course you meant a function that takes a cons cell as input and returns a list suitable for passing as the final argument to `apply`:
(apply #'evil-delete (the-missing-function '(0 . 88)))
Phil-Hudson · 2022-04-09 00:41
(cl-defun cons-to-list ((beg . end))
(list beg end))
Bodertz · 2022-04-09 01:28
/u/rguy84
(defun my-org-insert-drawer (&optional arg drawer)
(interactive "P")
(cond
((or arg drawer) (org-insert-drawer arg drawer))
(t (let* ((drawer-1 (read-from-minibuffer "Drawer (RELATED): "))
(drawer (if (equal drawer-1 "") "RELATED" drawer-1)))
(org-insert-drawer arg drawer)))))
I think this function might work as well.
hirep14316 · 2022-04-09 12:16
Zerkaa doesn’t explode. I did it! 😄
by_wicker · 2022-04-09 22:38
Frankly, I have reverted to even earlier days and build from source. See [EmacsWiki](https://www.emacswiki.org/emacs/BuildingEmacs).
For build dependencies, e.g.:
sudo apt install build-essential
sudo sed -i 's/# deb-src/deb-src/g' /etc/apt/sources.list
sudo apt update
sudo apt build-dep --yes emacs
For 20.04 for native JSON and native-comp I also did:
sudo apt install --yes libjansson-dev libgccjit-9-dev
And configure with `./configure --with-native-compliation`, but perhaps that's now default(?).
And then I recommend following the Emacswiki "Alternative installation methods" to use `gnu stow`. Much easier if you ever want to revert or switch between versions.
I started building from source to get unreleased features but of course you can just as well build the `emacs-28.1` branch.
Phil-Hudson · 2022-04-10 07:45
Is there any difference in user option `exec-path`?
Phil-Hudson · 2022-04-10 07:48
Alternatively:
(require 'dash)
(apply #'evil-delete (-cons* '(0 . 88)))
I think this is the best answer to your ioriginal question.
AuroraDraco · 2022-04-10 12:08
This is one of the major reasons I dislike Ubuntu. It will probably take a while to get in the apt repos
takeonrules · 2022-04-10 15:33
I use Prot's [pulsar](https://lists.sr.ht/~protesilaos/pulsar) package. A nice lightweight package for "pulsing" the current line. I wanted to extend this to quickly show me where I "left my mark"
Today, I made a slight modification:
(defun jf/pulse (parg)
"Pulse the current line.
If PARG (given as universal prefix), pulse between `point' and `mark'."
(interactive "P")
(if (car parg)
(pulsar--pulse nil nil (point) (mark))
I bound the above to `C-x l` (as per past bindings).
MarkofCorn · 2022-04-11 05:21
Thought they were pushing snaps now too as the default installation method
rguy84 · 2022-04-11 12:19
Nifty, thanks. Does the line below check if there's a drawer already or does it just insert RELATED, if i don't type anything at the prompt?
(drawer (if (equal drawer-1 "") "RELATED" drawer-1)))
Also, to make it add nicely, I have the following from something I found when looking for making `id`s for captures:
(unless (org-before-first-heading-p)
I am not sure where that would be placed in your function.
Bodertz · 2022-04-11 16:00
>Nifty, thanks. Does the line below check if there's a drawer already or does it just insert RELATED, if i don't type anything at the prompt?
The latter. It sets the variable `drawer` to "RELATED" if you didn't type anything at the prompt, or to whatever you typed at the prompt otherwise. Then, in the final line, whatever `drawer` is is passed as an input to `org-insert-drawer` to tell that function what the name of the drawer should be. `org-insert-drawer` does all of the actual work of making the drawer.
>Also, to make it add nicely, I have the following from something I found when looking for making `id`s for captures:
> (unless (org-before-first-heading-p)
I don't really use Org much, so it's unclear to me what you want that to do. Do you want nothing to happen if point is before the first heading? Do you want it to show an error? Or jump to after the first heading?
>I am not sure where that would be placed in your function.
Surrounding the `cond` form, probably. That would make the function not do anything if before the first heading.
(defun my-org-insert-drawer (&optional arg drawer)
(interactive "P")
(unless (org-before-first-heading-p)
(cond
((or arg drawer) (org-insert-drawer arg drawer))
(t (let* ((drawer-1 (read-from-minibuffer "Drawer (RELATED): "))
(drawer (if (equal drawer-1 "") "RELATED" drawer-1)))
(org-insert-drawer arg drawer))))))
RobThorpe · 2022-04-11 16:26
The correct directory for python is there in `exec-path`. It looks like that's not the problem.
rguy84 · 2022-04-11 17:46
I just tried to use the default function, and if the point was in a headline, it would drop down beneath it, like inserting the PROPERTY drawer. I just have to be mindful when calling it.
>Do you want nothing to happen if point is before the first heading
If my org file is like
* something
SCHEDULED:
some text& here
The point is where & is, ideally the drawer would inserted in a new line between "SCHEDULED" and "some text." I thought the `unless` snippet I pasted would do that.
Bodertz · 2022-04-11 19:14
Would you want "some text" to be in the drawer or outside of it? If you want it outside, you could get that behaviour by moving point to the beginning of the line before `org-insert-drawer` is called:
(defun my-org-insert-drawer (&optional arg drawer)
(interactive "P")
(cond
((or arg drawer) (org-insert-drawer arg drawer))
(t (let* ((drawer-1 (read-from-minibuffer "Drawer (RELATED): "))
(drawer (if (equal drawer-1 "") "RELATED" drawer-1)))
(beginning-of-line)
(org-insert-drawer arg drawer)))))
If you want the text included, that would be more work.
rguy84 · 2022-04-11 20:30
I'd want some text outside the drawer.
Bodertz · 2022-04-11 20:33
Does the function I wrote above work for that?
rguy84 · 2022-04-12 13:01
works great
third_dude · 2022-04-12 18:45
Im a new user coming from vim/terminal. I always feel like emacs is awkward to jump into for quick edits. With vim/terminal its like cd <some dir> vim <something> ggjjisome-text:q execute command, done.
With emacs its launch emacs. C-x C-f <somefilename>, C-x 2, C-x o, M-x shell, C-x o, C-f, C-f, C-n <some-text>, C-x C-s C-x o, M-p RET. Its just kind of clumsy. Any tips on making this process feel more like terminal/vim approach?
deltille · 2022-04-12 19:15
Make a script that runs an emacsclient with the target file and desired config and put it in your path. I named mine 'emac', set it executable and then set it as my EDITOR. It looks a bit like:
#!/bin/sh
if [[ -z "$@" ]]; then
emacsclient -a '' -c
else
emacsclient -a '' -c $@
fi
Caveat, my notes say I was having issues with its blocking behavior in TTY sessions, experiment with 'man emacsclient' and the --no-wait option if it comes up. Of course, a similar script will also work on plain 'emacs' (not emacsclient) if you have it configured to start up fast. Omit "-a ''" in that case, as that is there to start the emacs daemon if it isn't running
...I also don't recall why the 'if' is there precisely, but I trust past me's judgment that missing it was causing issues. Maybe someone else here has a cleaner script
Also, if you're coming from vim, any reason you aren't using evil? Want to learn the native keybinds first?
third_dude · 2022-04-12 19:21
It’s not about the keybindings I actually like the eMacs ones more. It’s the quick entry and exit from the terminal that I love from vim. So many programs are designed to run in the terminal that the eMacs shell fees a bit awkward by comparison.
I will try your script though
deltille · 2022-04-12 19:31
That makes sense. That script solved the quick exit/entry for me so I hope it helps you as well. Note that it will open a GUI window, if you want it to force a TTY window I believe that's on --tty flag.
[deleted] · 2022-04-20 12:12
[deleted]
third_dude · 2022-04-20 17:25
This is actually much smoother but it still makes me feel like I’m working in the terminal rather than in eMacs majorly
[deleted] · 2022-04-20 21:01
[deleted]