reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator 路 2022-01-26 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.
pwnedary 路 2022-01-26 09:01
Small Evil tip: With `gj`/`gk` you probably meant to move to the character visually below/above the cursor even if the cursor happened to be stuck to EOL. This fixes that:
(defun reset-curswant (&rest args)
"Unstick the cursor from the end of the line."
(setq temporary-goal-column (current-column)))
(advice-add #'evil-next-visual-line :before #'reset-curswant)
(advice-add #'evil-previous-visual-line :before #'reset-curswant)
Should probably just overwrite the keybindings instead of adding advice, but Emacs makes that harder.
AffectionateAd8985 路 2022-01-26 09:45
`(add-hook 'org-mode-hook (lambda () (org-next-visible-heading 1)))`
Move to first heading when open org files, with `org-use-speed-commands`, I can quick browse org file with only `n/p` keys.
yousufinternet 路 2022-01-26 17:39
I was reading through the org manual, and learnt about two variables `org-agenda-category-icon-alist` and `org-agenda-prefix-format`, the first allows you to set icons for categories (`CATEGORY` property), icons can be images or symbols, this is the code I came up with and the agenda already looks more colorful and clear
(setq org-agenda-category-icon-alist nil)
(setq agenda-categories-alist
'(("WORK" "馃捈") ("SOFTWARE" "馃捇") ("SETUP" "馃惂") ("EMAIL" "鉁夛笍")
("HOME" "馃彔") ("WOOD" "馃") ("FAMILY" "馃應") ("REPORTS" "馃摎")
("INCOME" "馃挵")))
(dolist (icon agenda-categories-alist) (add-to-list 'org-agenda-category-icon-alist
`(,(car icon) ,(cdr icon) nil nil :width (16.) :ascent center)))
(defun format-agenda-prefix () (interactive)
(setcar org-agenda-prefix-format '(agenda . " %-2i %?-12t% s")))
(add-hook 'org-agenda-mode-hook 'format-agenda-prefix)
Of course for the emojis to show up correctly I use this:
(set-fontset-font "fontset-default" 'symbol (font-spec :family "Noto Color Emoji"))
I am sure a lot of you know about these, please share your customization
Kefim_Wod 路 2022-01-26 17:47
This might only apply to a tiny subset of emacs users.
It's specific to doom emacs, in WSL, when using emacs in the terminal, and using the Windows Terminal.
I finally figured out some tiny nagging issue that has occasionally plagued me.
I rarely use emacs in the terminal but when I do it's usually in a WSL environment using Windows Terminal.
For some reason, keybinds have always been funky under those circumstances but in particular `C-<return>` has never worked and it drove me crazy.
I use org-mode for taking notes and `C-<return>` should be invoking `+org/insert-item-below` which I use all the time.
I found out that emacs uses a function called `event-apply-modifier` which reads a key and adds a modifier key to it.
It has one for M, C, Super, etc.
For `Ctrl` it binds `C-x @ c` to `event-apply-control-modifier`.
So for example you would type `C-x @ c &` to enter Ctrl-&.
So what I did was add a keybind command to Windows Terminal that when the input `C-x @ c` is read it sends the keyboard input `Ctrl+<return>` which will be read by emacs correctly and it executes the associated keybind.
Windows Terminal has GUI settings or you can edit the settings.json directly which is necessary for this.
The command is
{
"command":
{
"action": "sendInput",
"input": "\u0018@c\u000d"
},
"keys": "ctrl+enter"
},
I'll try and break it down but most of this was figured out during some feverish googling and my understanding is superficial.
\u0018 is the Unicode character `Cancel` which is the ASCII control code sequence `^X` or in emacs notation `C-x`.
\u000d is the Unicode character `Carriage Return`.
So when I hit `Ctrl+<return>` on my keyboard Windows Terminal sends the input
`Cancel` + `@ c` + `Carriage Return` or `C-x @ c CR` which invokes the `+org/insert-item-below` function.
nicholas_hubbard 路 2022-01-26 22:19
This function clears a comint-mode buffer in a dwim fashion by preserving the current input line and working even if the cursor is above the input line. I have been using it with shell-mode and ielm bound to `C-l` and it works nicely.
(defun my/comint-clear ()
(interactive)
(let ((orig-ln (line-number-at-pos))
(col (current-column))
(cmd (progn (end-of-buffer)
(move-end-of-line nil)
(set-mark (point))
(move-beginning-of-line nil)
(buffer-substring (region-beginning) (region-end))))
(after-ln (line-number-at-pos)))
(delete-region (region-beginning) (region-end))
(comint-clear-buffer)
(insert cmd)
(if (= orig-ln after-ln)
(move-to-column col t)
(move-beginning-of-line nil))))
Hastrack 路 2022-01-27 13:51
Simple function that helps me a lot, I found it a better solution than `deft`
(defun org-roam-rg ()
"Ripgrep into org-roam-directory"
(interactive)
(let ((default-directory org-roam-directory))
(consult-ripgrep)))
No-Command-791 路 2022-01-27 16:25
>`("SETUP" "馃惂")`
I love this, so cute.
Can I ask what `WOOD` is for?
yousufinternet 路 2022-01-27 16:45
I love doing some DIY wood projects from time to time, and I write notes about what I need to buy or do.
rguy84 路 2022-01-27 19:31
I asked two times here if there's a way to copy something from the kill ring from say position 4, grab it for pasting in another tool like outlook. A few of you gave me commands that I could call via `helm-kill-ring-actions`, everything errors out. while fat fingering, I found `C-c C-k` that calls `helm-kill-ring-kill-selection` and does exactly what i want. However adding it to `helm-kill-ring-actions`, throws
helm-execute-selection-action-1: Wrong number of arguments: (0 . 0), 1
The doc says
> Same as `鈥榟elm-kill-selection-and-quit鈥檂 called with a prefix arg.
That function says
> (helm-kill-selection-and-quit ARG)
> Store display value of current selection to kill ring.
With a prefix arg use real value of current selection.
I am unsure what Arg to use nor how to specify it within `actions.`
w0ntfix 路 2022-01-27 20:56
related lines of thought:
- https://github.com/CyberShadow/term-keys
- https://sw.kovidgoyal.net/kitty/keyboard-protocol/
AccurateRendering 路 2022-01-29 14:04
Is there a way to create a c++ function definition skeleton from a function declaration?
I often find my self adding a function to the header then cutting and pasting it, removing the colon, adding braces and putting it in the right class/namespace in the .cpp file - I feel that this might be automatable and that someone has already done it.
[deleted] 路 2022-01-29 20:59
[deleted]
No-Command-791 路 2022-01-30 16:02
(defun pop-kill-ring ()
"Delete the kill ring head."
(interactive) (message (pop kill-ring)))
Incredibly basic but (imo) useful, and writing it made me so happy for some reason.
crawsphi 路 2022-01-30 19:43
You might want to adjust `kill-ring-yank-pointer` as well.
No-Command-791 路 2022-01-30 20:38
Good catch! Thank you.
(defun pop-kill-ring (pointer-at-head)
"Delete the kill ring head."
(interactive (list (eq kill-ring-yank-pointer kill-ring)))
(message (pop kill-ring))
(when pointer-at-head (setq kill-ring-yank-pointer kill-ring)))
T_Verron 路 2022-01-31 10:17
You can check this out: [https://www.emacswiki.org/emacs/ExpandMemberFunctions](https://www.emacswiki.org/emacs/ExpandMemberFunctions)
AccurateRendering 路 2022-01-31 10:30
Great! Thanks!
jokem59 路 2022-02-01 07:46
I've found that in \`cpp-mode\`, when the double quotes operator is overridden, everything following the double quotes in \`operator""\` is shows as if it's quoted (i.e. a string literal).
Does anyone know how to prevent this from happening? More of annoyance because I essentially lose syntax highlighting in the rest of the file.
T_Verron 路 2022-02-01 12:25
Would it work to add `// ""` at the end of the line?
jokem59 路 2022-02-01 20:26
Unfortunately not, the first double quote after the `//` matches the second double quote in `operator""`.
The second double quote after `//` get's lumped into the "string" that's formed by the first double quote in `operator""`.
T_Verron 路 2022-02-02 09:43
Ok. I'm not sure to understand why the second double quote in `operator""` doesn't close the string already, then.
I also can't reproduce the bug.
For instance, if I take the first example from https://en.cppreference.com/w/cpp/language/user_literal and put it in a `c++-mode` buffer, the fontification appears to be correct (and also if I remove the space between `operator` and `""`.
Do you also observe the bug in other modes? Do you use any package specifically for delimiters matching?
No-Command-791 路 2022-02-02 14:23
For me this breaks any capture template containing a newline. Great idea otherwise.
jokem59 路 2022-02-02 20:15
That was my expectation. This is the only mode I've observed it in and there are no delimiter matching packages that I'm currently using.
Interestingly, if I delete that operator, and paste it back in, formatting works as intended.
T_Verron 路 2022-02-02 21:52
Okay, this is a very stupid question, but are you sure it was two double quotes, and not one single quote and one double quote?
If you are sure, next time you see the bug, you can try to see what `C-u C-x =` shows before the first double quote, and also before the second double quote. You want to look for the line `syntax: " which means: string`. If the `syntax` line shows something else for either of them, that's probably the cause for your problem (but you will have to investigate where it comes from).