reddit / emacs

Weekly Tips, Tricks, &c. Thread

AutoModerator · 2022-03-16 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.
jimm · 2022-03-16 13:46
The `git grep` git command is great for finding a regex (or a simple string) everywhere in a git repo. I define the following functions and bind the `git-grep` function to `F2`. It will prompt for a regex and search for that in the repo or, if you give it a numeric prefix like `C-u` it will read the current symbol at point (the word under the cursor) and search for that. Results appear in a grep buffer, so you can use `C-g C-n` and `C-g C-p` to navigate from one result to the next. (defun git-root-dir () "Returns the current directory's root Git repo directory, or NIL if the current directory is not in a Git repo." (let ((dir (locate-dominating-file default-directory ".git"))) (when dir (file-name-directory dir)))) (defun git-grep (arg) "Runs 'git grep', starting the search in the current directory's root git repo directory. By default, reads the regex from the minibuffer. With a prefix argument, initializes the search string with the current symbol at point." (interactive "P") (let* ((symbol-at-point (thing-at-point 'symbol)) (regexp (if (and arg (symbol-at-point)) (regexp-quote symbol-at-point) (read-from-minibuffer "Search regexp: " nil nil nil 'grep-find-history))) (default-directory (git-root-dir)) (case-ignore-flag (and (isearch-no-upper-case-p regexp t) "-i")) (cmd (concat "git grep --extended-regexp --line-number --full-name" " --untracked " case-ignore-flag " -- \"" regexp "\"" " | cut -c -240"))) (while (equal "" regexp) (setq regexp (read-from-minibuffer "Search regexp (must not be the empty string): " nil nil nil 'grep-find-history))) (grep-find cmd)))
mnp · 2022-03-16 15:54
On that note, I really like `helm-grepint` when it works. It's like incremental search over a file tree instead of `grep -r`. Its customization is super difficult to locate though, and it often doesn't do what I want, like filter out file types, pick a different root, etc. Anybody else have better luck?
FunctionalFox1312 · 2022-03-16 17:35
`(setq shell-command-switch "-ic")` If you use bash as your shell, through emacs, this instructs it to read any existing startup files (.bashrc, .bash\_aliases, etc). More generally, this variable is for passing command line arguments to your shell on startup (the shell is set by `shell-file-name`), and you can pass Bash [any of these options, including those available to the Set builtin](https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html)
bugamn · 2022-03-16 18:36
This might have been a better solution than redefining environmental variables as I did. This has motivated to read more on the related info pages and improve my setup.
ChristopherHGreen · 2022-03-16 19:08
I queried about lining up c++ end of line comments when using proportional fonts a while ago. I got off my ass and coded something myself for it a month or so ago. I use the display property "(space ( :align-to position))" to set the display position of the end of line comments. Now, my end of line comments are generally lined up well, \_even if\_ I look at source that hasn't tried to align them.
_viz_ · 2022-03-17 02:59
Are you sure you're not redefining vc-git-grep
jimm · 2022-03-17 13:18
Partially, yes. I didn't know about that function but now that I've tried it I like my version better :-) I don't want to be asked for the file name glob pattern or the directory, and I prefer the default directory be the top git directory (that is, the directory at or above the current one --- the one that contains the .git folder).
ramin-honary-xc · 2022-03-17 13:29
I wrote a macro to practice my Elisp, `eval-elisp-in-test-window`. Basically, it is a version of `with-current-buffer` that runs your code in a dedicated test buffer, and in a split-screen, without moving the focus of your cursor away from the Elisp buffer you are working in. So you can make lots of little tweaks and adjustments to your code and execute with `C-M-x` quickly and easily after each edit, no switch back and forth between buffers. (defmacro eval-elisp-in-test-window (&rest body) "This macro lets you play around with Emacs Lisp expressions and see the results in a split screen." (let ((elisp-test-from-window (selected-window))) (switch-to-buffer-other-window (get-buffer-create "*elisp-test-output*")) (condition-case-unless-debug error-from-eval-elisp-in-test-window (let ((result-of-eval-elisp-in-test-window (eval `(progn ,@body)))) (select-window elisp-test-from-window) result-of-eval-elisp-in-test-window) (t (progn (message (format "%S" error-from-eval-elisp-in-test-window)) (select-window elisp-test-from-window) error-from-eval-elisp-in-test-window))))) (setq initial-scratch-message "(eval-elisp-in-test-window\n (erase-buffer)\n \n ;;\n )\n") I setup my `*scratch*` buffer default text with a call to this macro. So my initial `*scratch*` buffer looks like this: (eval-elisp-in-test-window (erase-buffer) ;; ) Quick example: (eval-elisp-in-test-window (erase-buffer) (insert "Hello, world!\n") (search-backward-regexp "Hello,") (replace-match "Welcome to my" t) ;; )
organic-bookworm · 2022-03-17 18:38
Emacs's already got you covered -- there's the `with-current-buffer-window` macro.
oantolin · 2022-03-17 22:42
> Results appear in a grep buffer, so you can use `C-g C-n` and `C-g C-p` to navigate from one result to the next. Those are probably your personal key bindings, the defaults are `M-g M-n` and `M-g M-p`.
jimm · 2022-03-18 02:13
Those are my bindings, too. I wrote the wrong bindings in the comment. Thanks for the correction.
ramin-honary-xc · 2022-03-18 14:06
Cool, thanks! I had actually tried that one but had trouble getting this to work before, but you prompted me to try again and setting the second and third argument to `nil` finally made it do what I wanted. (with-current-buffer-window (get-buffer-create "*elisp-test-output*" nil nil (insert "Hello, world")) Well, that simplifies my macro a lot! Basically it just calls `with-current-buffer-window` with all but the body set to my default. EDIT: I just noticed one big difference -- somehow my version keeps the cursor position after the macro evaluation ends, but `with-current-buffer-window` always repositions the cursor at the top of the buffer. Any clue how to fix this?
organic-bookworm · 2022-03-18 18:31
>`with-current-buffer-window` always repositions the cursor at the top of the buffer Nice catch. Got me searching. The `with-current-buffer-window` is a [temporary display](https://www.gnu.org/software/emacs/manual/html_node/elisp/Temporary-Displays.html) macro. >Temporary displays are used by Lisp programs to put output into a buffer and then present it to the user for perusal rather than for editing. The `with-current-buffer-window` uses the `temp-buffer-window-show` function to display a buffer, set it read-only and explicitly move point to the bob. That being said, I think they've just decided to move point for some consistency. >Any clue how to fix this? You *could* e.g. override `temp-buffer-window-show` with an advice, but that would defeat the benefits of using the macro, right? However, see `save-selected-window` macro -- Emacs still got you *somewhat* covered.
sudhirkhanger · 2022-03-19 01:50
Do you and how do you differentiate between two systems? I began with per OS config and then it seems now I might have to differentiate between two machines on the same OS. Probably using hostname is the best bet. What's the best way to achieve this?
eli-zaretskii · 2022-03-19 08:04
Did you try using `system-name`?
data_dan_ · 2022-03-19 13:39
I used to use a lot of inline latex whenever I needed to represent equations or math symbols in org mode. Then I found `insert-char` which made it easy to drop in the occasional unicode character for e.g. Greek letters, integral symbols, etc. And _then_ I found `org-toggle-pretty-entities` (or `(setq org-pretty-entities t)`), such that I can just type, e.g., `\alpha` and it will display as α, no need to navigate any menus. Huge time-saver after years of using much slower approaches. Anything else I'm missing on how to do this better?
konrad1977 · 2022-03-19 13:44
If you are using M1 (Max|Ultra) Macs and needs your terminal commands in x86 how to do then? Well just add `"env /usr/bin/arch -x86_64"` in front of your commands. With this I can finally compile and start the simulator from Emacs and do \*almost\* all my stuff in Emacs. (*Debugging and adding files to the project is still missing*).
holgerschurig · 2022-03-19 18:51
Nice. I however use consult-git-grep or (more often) consult-ripgrep. See https://github.com/minad/consult#grep-and-find
jokem59 · 2022-03-20 04:27
I've used `sysyem-type` like so: ``` (if (string-equal system-type "darwin") () (when window-system (server-start))) ```
seeminglyugly · 2022-03-21 00:10
Noob here--is there a slight performance hit or additional processing using the Evil plugin compared to native Emacs for editing text?
sudhirkhanger · 2022-03-21 02:09
`system-name` should work because system-type would give OS. I might have two machines with same OS.
sudhirkhanger · 2022-03-21 02:09
Worked.
oantolin · 2022-03-21 03:27
You should definitely check out Carsten Dominik's cdlatex package. Using it you can enter alpha with just backquote `a`.
holgerschurig · 2022-03-21 06:58
Also look at cape, e.g. cape-tex. Cape is plugin-at-point framework, designed for Corfu, but can also work with Company. But it can even work without completion, by selecting things via completing-read, and therefore via ido/helm/ivy/selectrum/vertico. I have`C-c p \` bound to `cape-tex` and then I can just enter partial a TeX string to get the completion of this string into a unicode character. E.g. I enter "sigma", and am at the entry "\Sigma". One return, and I have Σ in my text. With completion framework you can type `\ S i <TAB>` and have the Σ in your text. `C-x 8 RET` is a bit similar, but purely dedicated to Unicode, not to their LaTeX representation.
jimehgeek · 2022-03-21 07:38
Personally I use `helm-ag` for searching whole projects/directories but I’ve configured it to use ripgrep. Main reasons are the way it ends up displaying results along with previewing the file if helm’s follow mode is on. And also because pressing `C-c C-e’ while searching, switches to a result editing mode, basically making the result buffer with all results from all files editable. Combine that with multiple cursors, and it’s a stupidly powerful and interactive way to replace/edit things across whole projects.
jimehgeek · 2022-03-21 07:41
I’d suggest trying [vterm-mode](https://github.com/akermu/emacs-libvterm) if you haven’t already. It loads up up a full normal shell, and is also performant enough to run interactive tools like htop for example.
konrad1977 · 2022-03-21 10:57
Loading packages always add ms to startup time, but editing speed is not noticeable.
T_Verron · 2022-03-23 12:29
You can also do that with auctex (`LaTeX-math-mode`). cdlatex changes the layout a little and add additional layers, but greek letters are definitely covered by auctex.
oantolin · 2022-03-23 13:17
Sure, but we were talking about org mode. I'm not sure how easy it is to use AUCTeX's tools for this in org mode. Cdlatex I know works fine in org mode.
T_Verron · 2022-03-23 14:01
It is a minor mode, it works just fine in any file. Just `M-x LaTeX-math-mode` or add it to the mode hook, and you're good to go.
oantolin · 2022-03-23 14:41
Cool, I didn't know that! Thanks.
Phil-Hudson · 2022-03-24 10:05
I define (programmatically) a suite of conditional-compile macros like this: (defmacro compile-on-foo (&rest forms) "Conditionally compile FORMS only on host foo." (when (string= (downcase system-name) "foo") `(progn ,@forms))) (defmacro compile-under-windows (&rest forms) "Conditionally compile FORMS only under Windows." (when (eq system-type 'windows) `(progn ,@forms))) (defmacro compile-if-emacs-version>=27 (&rest forms) "Conditionally compile FORMS only under version 27 or later." (when (>= emacs-major-version 27) `(progn ,@forms)))