# A very simple question: is it possible to search with regex in current file?

**URL:** <https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122>\
**Category:** Help\
**Created:** [November 22, 2022, 3:43pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122 "2022-11-22T15:43:09Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![GLight](https://forum.obsidian.md/user_avatar/forum.obsidian.md/glight/32/4851_2.png) [@GLight](https://forum.obsidian.md/u/GLight)\
**Post date:** [November 22, 2022, 3:43pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/1 "2022-11-22T15:43:10Z")

</div>

A very simple question:  
is it possible to search with regex in current file?

or is there a plugin for this

---

<div class="post-metadata">

**Author:** ![Limezy](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/l/ba9def/32.png) [@Limezy](https://forum.obsidian.md/u/Limezy)\
**Post date:** [November 22, 2022, 4:16pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/2 "2022-11-22T16:16:59Z")

</div>

You can always query that regex with the path: option, using the path of your current file, but that’s a bit hacky.

You could also automate this using the Templater plugin to take the regex as an input and build a query block at the end of the file, computing automatically the current file path I guess ?

---

<div class="post-metadata">

**Author:** ![CawlinTeffid](https://forum.obsidian.md/user_avatar/forum.obsidian.md/cawlinteffid/32/82899_2.png) [@CawlinTeffid](https://forum.obsidian.md/u/CawlinTeffid)\
**Post date:** [November 22, 2022, 9:07pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/3 "2022-11-22T21:07:22Z")

</div>

The regex plugins I know of are focused on replacing; the one I have installed doesn’t have a way to just find. You can search appropriate terms in Community Plugins — the list of relevant ones should be short.

---

<div class="post-metadata">

**Author:** ![ichmoimeyo](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/i/d9b06d/32.png) [@ichmoimeyo](https://forum.obsidian.md/u/ichmoimeyo)\
**Post date:** [November 23, 2022, 7:44am UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/4 "2022-11-23T07:44:52Z")

</div>

[![](https://i.imgur.com/GT2Autv.png) ](https://i.imgur.com/GT2Autv.png)

[![](https://i.imgur.com/7NMidf1.png) ](https://i.imgur.com/7NMidf1.png)

The path is awkward but not difficult to find.

I wish there was something like in dataview …

path:this.file.path

---

<div class="post-metadata">

**Author:** ![obsequious](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/o/4af34b/32.png) [@obsequious](https://forum.obsidian.md/u/obsequious)\
**Post date:** [November 23, 2022, 8:31am UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/5 "2022-11-23T08:31:51Z")

</div>

Check out [GitHub - mrjackphil/obsidian-jump-to-link: Quick jump between links using hotkeys](https://github.com/mrjackphil/obsidian-jump-to-link)

---

<div class="post-metadata">

**Author:** ![shallash](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shallash](https://forum.obsidian.md/u/shallash)\
**Post date:** [November 27, 2022, 7:35am UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/6 "2022-11-27T07:35:51Z")

</div>

Activate Obsidian Vim key bindings from Settings → Editor → Advanced, and search with `/` in Normal Mode?

---

<div class="post-metadata">

**Author:** ![daniel347x](https://forum.obsidian.md/user_avatar/forum.obsidian.md/daniel347x/32/5242_2.png) [@daniel347x](https://forum.obsidian.md/u/daniel347x)\
**Post date:** [February 2, 2023, 4:04pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/7 "2023-02-02T16:04:14Z")

</div>

@shallash That’s a great idea! Only problem is, there’s no ability to set a hotkey to switch in/out of Vim mode, so a long process of moving the mouse and manually opening the Settings dialog to enter/exit Vim mode is required, making it infeasible to use Vim to work around this issue for quick work.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 2, 2023, 4:48pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/8 "2023-02-02T16:48:03Z")

</div>

Would using a embedded query search inserted from a template be an alternative solution?

I’m thinking triggering a hotkey, which then inserts the query codeblock prefilled with the `path` of the current file, and some `/ ... /` to get the regex going?

It’ll of course “mess up” the file, but it’ll allow for regex searches in the current file, too. And the block can be removed again, rather easily.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 2, 2023, 5:11pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/9 "2023-02-02T17:11:01Z")

</div>

Another idea would be to use javascript to pre-populate the search field with the current path, and then trigger a search command. Maybe something from this post (on a different topic) could be used to build such a command.

> [@Is there a way to keep the Outline permanently folded?](https://forum.obsidian.md/t/is-there-a-way-to-keep-the-outline-permanently-folded/53349/5):
>
> Hey Dani, You love UI experiments, so I made you a feature. dove WHAT IT DOES: Automatically folds all top-level outline-headings… when you: open a doc / load Obsidian. change tabs. press the Hot-Key (ctrl g). \*\* DEMO: here is a demo, so you can see. [outlinefolder] … in the video: The first top-level multi-fold in every note-outline is automatic. The next top-level multi-fold in a note is done by the Hot-Key (ctrl g). subheader-unfolds are preserved. \*\* HERE IS THE CODE: // outli…

In there @ZenMoto folds the outline using a command. Maybe it could be adapted/changed into setting the content of the input search element, to something like: `path: /my/current/file / /`, and then open the search prompt?

---

<div class="post-metadata">

**Author:** ![ZenMoto](https://forum.obsidian.md/user_avatar/forum.obsidian.md/zenmoto/32/20312_2.png) [@ZenMoto](https://forum.obsidian.md/u/ZenMoto)\
**Post date:** [February 3, 2023, 4:50pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/10 "2023-02-03T16:50:10Z")

</div>

Sure - maybe this will achieve what you want 🏂

**WHAT IT DOES:**

- opens Search =\> injects “`path: active/note/path //`”
- when you press the Hot-Key (`control g`).

\*\*

**DEMO:** here is a demo, so you can see it working.

![regexSearch](https://forum.obsidian.md/uploads/default/original/3X/7/c/7c130f14ce4dd91453c5ea3d6eb6b1a135553891.gif)

^ the search box is activated / populated by the Hot-Key (`ctrl g`).

- the base regex `//` can be changed.
- the Hot-Key can be changed.

\*\*

**HERE IS THE CODE (JS):**

```auto
// regex search injector => hotkey (control g)
window.onkeydown = function (e) {
      if (e.ctrlKey == true && e.key == "g") {
          e.preventDefault(); // override "control g"      
          leafSelector(); 
          setQuery(); // => run it
      }
} 

function setQuery(){ // inject path to search box
	document.querySelector(".mod-top-left-space [data-type='search']").click();

    let tName = document.querySelector('#active-tab .view-header-title-container.mod-at-start').innerText
        .replace("\n", "") // remove line breaks
        .replace(/\u200B/g, "") // remove zerowidthspaces
        .replace(/\s/g, "") // remove zerowidthspaces

	let sQ = "path: " + tName + " //"; 
    document.querySelector('.mod-top-left-space .search-input-container > input').value = sQ;
}

function leafSelector() { // get tab
    const cut = document.getElementById('active-tab');
    if (cut !== null){cut.removeAttribute('id');}

    var leafArray = document.querySelectorAll('.mod-vertical .workspace-leaf');
    for (var c = 0; c < leafArray.length; c++) {
        let leafo = leafArray[c];
        if (window.getComputedStyle(leafo).display === "none") {continue;} 
        else {leafo.id = "active-tab"; return;}        
    }
}

```

… here is the exact same code on [privatebin](https://privatebin.at/?29c893b9e01bcfb7#Fun1XLeqSmB3ARXSuxXuB8mZiiwx1hhHgF3ryBb7HHYE).

\*\*

**HOW TO ADD IT:**

The “Javascript Init” plugin is great for running JS in Obsidian.

To set it up (first-time only), just go to:

- Preferences \> community plugins \> get “Javascript Init”.
- Preferences \> Javascript Init settings \> paste the JS code in the box.
- Refresh Obsidian [View \> Force Reload]. (And if you modify the code, reload.)

\*\*

@Limezy yeah it’s hacktastic 🙈

Maybe it is similar to your goal @GLight 🌅

---

<div class="post-metadata">

**Author:** ![GLight](https://forum.obsidian.md/user_avatar/forum.obsidian.md/glight/32/4851_2.png) [@GLight](https://forum.obsidian.md/u/GLight)\
**Post date:** [February 4, 2023, 1:59am UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/11 "2023-02-04T01:59:08Z")

</div>

yes

I have been using this workaround to generate dynamical todo list related to the current note.

But this is a little bit too bulky.

I don’t mind using search pane or query command. What the difficulty here is to get current file path and file name conveniently and pass them to the search pane or system clipboard.

Is there a way to pass the content generated by Templater to system clipboard?

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 4, 2023, 2:13am UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/12 "2023-02-04T02:13:28Z")

</div>

> [@GLight](#):
>
> But this is a little bit too bulky

Is it too bulky to hit one hotkey, and the search input is pre-filled, or are you responding to something other than ZenMoto’s response?

---

<div class="post-metadata">

**Author:** ![GLight](https://forum.obsidian.md/user_avatar/forum.obsidian.md/glight/32/4851_2.png) [@GLight](https://forum.obsidian.md/u/GLight)\
**Post date:** [February 4, 2023, 2:31pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/13 "2023-02-04T14:31:04Z")

</div>

Sorry for the ambiguity.  
I mean inserting a block which is generated by the query in the middle of a long note so that one can find the occurrences is too bulky for reading or writing the note, esp. when this query result block is very long.

But I think I can create an empty note and open it in a different pane, and use query templates to make this note a new search pane . Comparing to the built-in search pane, in this new pane, i can see the entire search condition lines even if they are very long and complicated, and I can also use various of different template or snippets.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 4, 2023, 5:24pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/14 "2023-02-04T17:24:27Z")

</div>

The beauty of the solution by ZenMoto is that it utilises the default search pane. It does require some initial setup, but once that is done you can hit the hotkey, and it’ll lock the search to current file and give to regex search in it.

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [May 5, 2023, 5:25pm UTC](https://forum.obsidian.md/t/a-very-simple-question-is-it-possible-to-search-with-regex-in-current-file/48122/15 "2023-05-05T17:25:10Z")

</div>

Closed on May 5, 2023, 5:25pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
