reddit / emacs

Weekly Tips, Tricks, &c. Thread

AutoModerator · 2022-04-27 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.
Heikkiket · 2022-04-27 11:14
Just yesterday I found about `nvm.el`. If you're doing JavaScript development and switching between Node versions with Node Version Manager, now you can do the same in Emacs as well. Helps to run Flycheck-checker with the right version of Node.
Tohiko · 2022-04-27 21:36
How do I search for a regex in a string without the multiline flag? The following returns `\nB` while I was expecting `\nD` ``` (let ((s "A\nB\nC\nD")) (string-match "\n[^\n]*$" s) (match-string 0 s)) ```
kovrik · 2022-04-27 22:05
Three questions about pdf-view-mode and pdf-tools: 1. When I have a pdf opened and use trackpad for scrolling, it scrolls through pages like crazy. How can I slow it down and make it scroll them one by one? 2. I would like doom-modeline to show page number indicator. I have the following in my config: (add-hook 'pdf-tools-enabled-hook #'doom-modeline-set-pdf-modeline). But it doesn't seem to work. If I call doom-modeline-set-pdf-modeline function manually then it works for a while, but then suddenly disappears. Anybody knows how to fix it? 3. When desktop.el restores a buffer with a PDF then that PDF is misaligned, not centred properly. I have to minimize Emacs window and then maximize it. Only then it re-aligns it and renders PDF properly. Why is that?
rofic · 2022-04-28 00:20
How does counsel-rg match the string `vitamins` with "vit a min" in the search prompt? I'm trying to get this behavior in Neovim's Telescope plugin with its live_grep. Is "Ivy–regex-plus" responsible for this? I don't understand how--doesn't counsel-rg first display results of rg, so it would only match lines of "vit a min"?
oantolin · 2022-04-28 01:54
`$` matches any end of line, so `\nB` is a perfectly sensible result. To match the end of the string use `\'`, which inside a string needs the backslash escaped, so it's `\\'`.
paretoOptimalDev · 2022-04-28 02:40
Maybe it ors every word?
gravygrowinggreen · 2022-04-28 04:27
Out of curiosity, why is the universal argument C-u equal to 4? Is this just a matter of they had to pick a number for it to equal, and landed on 4?
AffectionateAd8985 · 2022-04-28 04:58
After [dim out gitignore](https://www.reddit.com/r/emacs/comments/u2lf9t/comment/i4n9aoa/?utm_source=share&utm_medium=web2x&context=3) , I can't stop dimming :) ```lisp (let* ((date (rx (? (= 4 digit) "-") (regex "[01][0-9]-[0-3][0-9]"))) (datetime (concat date " [0-2][0-9]:[0-5][0-9]"))) (font-lock-add-keywords 'dired-mode `((,datetime . 'dired-ignored) (,date . 'dired-ignored) (,user-login-name . 'dired-ignored)) t)) ``` This code dim out user & time, time-style can be *iso* or *long-iso*, I have `dired-listing-switches` value "-alGh --time-style=iso"
oantolin · 2022-04-28 13:59
Great question! Also, why is it multiplicative? That is, why was it decided `C-u C-u C-u` means 4 × 4 × 4 = 64, rather than, say, 4 + 4 + 4 = 12?
walseb · 2022-04-28 14:55
I believe ivy sends the search string to ivy regex plus which then sends the resulting regex to rg. Ivy regex plus replaces spaces with ".*", which I personally love. You can insert a double space to get it to match a literal space. You can disable ivy regex plus by setting a variable. You can even replace it with your own function. There should be an entry in the manual about what variable to set.
oantolin · 2022-04-28 16:28
Looking at the source code, what it does is use whatever ivy-re-builder you currently have configured to turn the input into regexps (of which the `ivy–regex-plus` you mentioned is but one of the possibilities) and then it uses the function `counsel--elisp-to-pcre` which applies some simple transformation to attempt to convert each constituent regexp to PCRE syntax and concatenate them together. The end result is that if you have the default settings it will convert "vit a min" to something like `(?=.*vit)(?=.*a)(?=.*min)`. Note that that would match "vit", "a" and "min" in any order! But the ivy text is also set to "vit a min" which may reduce the matches to those in order depending on your current setting of ivy-re-builder.
eli-zaretskii · 2022-04-28 18:28
Because originally prefix argument was used to repeat commands, and those rules make it very easy to move N characters or lines or pages with very few keystrokes.
bhrgunatha · 2022-04-29 06:18
This always trips me up. I think it's because when I first learned regex I learned that `$` is a positional (zero width) match for the end of the input string. That fact has stuck. In Racket: (regexp-match #rx"\n[^\n]*$" "A\nB\nC\nD") '("\nD") (regexp-match #rx"(?m:\n[^\n]*$)" "A\nB\nC\nD") ; multi-line mode '("\nB") In Python: re.search(r"\n[^\n]*$", "A\nB\nC\nD") <re.Match object; span=(5, 7), match='\nD'> re.search(r"\n[^\n]*$", "A\nB\nC\nD", re.MULTILINE) <re.Match object; span=(1, 3), match='\nB'> In emacs (let ((s "A\nB\nC\nD")) (string-match "\n[^\n]*$" s) (match-string 0 s)) "\nB" The bottom line is you need to check the documentation for the underlying regex engine.
bhrgunatha · 2022-04-29 06:24
I've long since accepted the behaviour in ivy (which is a great feature!). I never realised you can match on literal spaces. This is a game changer for me. Thank you for the enlightenment.
walseb · 2022-04-29 07:08
Awesome, there is nothing quite like ivy regex plus out there that I have seen. I have even gotten selectrum to use it which is more reliable but lacks things like swiper, the excellent counsel find file and the order in which ivy sorts buffers which I prefer. So I use both. I gave porting it to vertico a shot as well but it was more difficult. In ivy regex plus you can also easily do single level negation. I believe you put an exclamation mark at the end of your search string then type out what you don't want to occur in your search. The reason it only works once per search is related to how regex can't easily do negation. At least that's the conclusion I came to while looking through the code.
walseb · 2022-04-29 07:32
Actually it shouldn't matter that you can only put in one exclamation mark. You should be able to do a regex 'or' to negate multiple words with one exclamation mark. Although I have never tried that.
radarsat1 · 2022-04-29 07:43
This may sound minor, but does anyone know how to stop vterm from scrolling horizontally? i've found that when I leave a process running that uses a progress bar (python's tqdm to be specific), then when the vterm window is _not_ in focus, the whole vterm buffer scrolls over to the right, I guess because the cursor ends up on the right-hand side of the buffer. But weirdly it _only_ does this when the frame is not in focus. [screenshot here](https://i.imgur.com/QGJKZgV.png)
oantolin · 2022-04-29 14:17
OK, but M-6 M-4 is shorter than C-u C-u C-u. :)
Phil-Hudson · 2022-04-30 09:55
And just for completeness: `\\'` is the zero-width match for end of input string.
eli-zaretskii · 2022-04-30 10:29
Only because you think about numbers. When I press `C-u C-u C-u`, I don't even need to think about the number it will produce, I just know it will be exponentially more than what `C-u C-u` will produce.
oantolin · 2022-04-30 12:49
Fair enough.
NoisyFrequency · 2022-04-30 14:33
Sometimes I want to find all occurrences of a function, word, definition, etc at point so I use `isearch-forward-thing-at-point` which is pretty smart to decide what the "thing" is. I bound it to `C-c s` for ease of use.
crawsphi · 2022-04-30 16:09
FYI this has an easy default binding too: `M-s .`
crawsphi · 2022-04-30 16:12
More discussion on this from a while back: https://www.reddit.com/r/emacs/comments/bsth94/reason_for_universalargument_behavior/
NoisyFrequency · 2022-04-30 16:14
The issue is that I'm not on a Qwerty keyboard and the dot requires an additional key to press.
crawsphi · 2022-04-30 16:15
Fair enough!
[deleted] · 2022-05-02 10:09
[deleted]
_noctuid · 2022-05-03 01:19
You mean this https://emacs.amodernist.com/?
[deleted] · 2022-05-03 01:46
[deleted]
char1zard4 · 2022-05-03 23:41
Just learned that you can change the mode and text of the scratch buffer, no more disabling elisp mode when I want to use it to edit text!
jumpUpHigh · 2022-05-04 03:25
Are there any restrictions if I use emacs from flatpak / flathub when it comes to `~/.emacs.d`or editing files on my local computer or using tramp?
ares623 · 2022-05-04 03:55
Does anyone have a setup of lsp-mode or eglot where they work on different Python projects (each with their own virtualenvs) throughout the day? Do you install a language server in each virtualenv? Or is a single server able to understand different environments? How does lsp-mode/eglot handle multiple server processes?
alastairdb · 2022-05-04 15:03
>Does anyone have a setup of lsp-mode or eglot where they work on different Python projects (each with their own virtualenvs) throughout the day? I do. >Do you install a language server in each virtualenv? Yes, I do. The elements of my solution are: 1. I put the virtualenv at the top level of the project. 2. I use [direnv](https://direnv.net/) to activate the virtualenv 3. I use [envrc](https://github.com/purcell/envrc) to make sure emacs buffers have the virtualenv correctly set from the direnv configuration. 4. I have a cookiecutter template that creates the virtualenv, installs the lsp server and creates the .envrc file. lsp-mode needs no extra configuration. It picks up the correct lsp-server automatically.