Fork me on GitHub
#emacs
<
2023-05-10
>
borkdude12:05:59

Dear emacs friends! I want to projectile-replace something in my project which is called defined-by* but when I try to replace this, projectile also tries to replace defined-by without a star. Note that I did not use projectile-replace-regexp . Should I escape the star or so?

ericdallo12:05:56

doom uses ripgrep for find and replace which is insanely fast and allows escaping like you want, never used projectile for that though. Maybe try escaping the star \*

borkdude12:05:42

I tried escaping the star, but did not work. Writing a bb script now

borkdude12:05:51

#!/usr/bin/env bb

(require '[babashka.cli :as cli]
         '[babashka.fs :as fs]
         '[clojure.string :as str])

(let [{:keys [opts args]} (cli/parse-args *command-line-args*)
      {:keys [y]} opts
      [root match replacement] args
      files (fs/glob root "**")]
  (doseq [file files
          :when (not (fs/directory? file))]
    (loop [changed? false
           s (slurp (fs/file file))
           from-idx 0]
      (if-let [idx (str/index-of s match from-idx)]
        (do (println "====" (str file))
            (println (subs s (max (- idx 10) 0) (min (+ idx (+ (count match) 10)) (count s))))
            (when-not y (println "Replace? (Y)"))
            (if (or y (= "Y" (read-line)))
              (recur true (str/replace-first s match replacement) idx)
              (recur changed? s idx)))
        (when changed? (spit (fs/file file) s))))))

😂 3
babashka 2
ericdallo12:05:48

That's why I like doom 😂

minikomi12:05:42

I tend to use an in-project rg , +vertico/embark-export-write and then use a regex or iedit-mode multiple cursors on all the matches

🪄 2
👍 1
pppaul17:05:39

I use helm-AG, which let's me edit and save the search results, multiple cursors helps too. if there are too many things to replace then command line sd (like sed) is pretty good

borkdude17:05:57

Thanks for the suggestions all. My bb script did the job this time, but I'll look into more sophisticated tools next time projectile-replace fails me :)

Ed12:05:16

I like wgrep ... It makes the grep results buffer editable so you can use query-replace, macros, etc and then save the edits into all the buffers you've changed.

🤯 2
ericdallo13:05:31

Yeah, it's really useful, doom uses something similar, it's the only way to find and replace visually like other editors that I know

Ed14:05:14

And Emacs will run whatever command you tell it to look up files, so long as it outputs text in the right format ... So you can use grasp or whatever to generate the matches ;)

👍 2
Benjamin13:05:32

I do consult-grep -> emark export -> wgrep

👍 2