Skip to content
The Chat Archive
llama-4-maverick-17b-128e-instruct· Meta LMArena Human Preference 140K

What's the correct dataview syntax in Obsidian to get all the lines that have a specific…

  1. User

    What's the correct dataview syntax in Obsidian to get all the lines that have a specific #tag that also reference a specific page (like [[Python]])?

  2. Assistant

    To achieve the desired outcome in Obsidian using Dataview, you need to construct a query that filters pages based on two conditions: the presence of a specific #tag and a link to a specific page (e.g., [[Python]]). Dataview allows you to query your vault's data using a SQL-like syntax.

    Here's a basic syntax to get you started. Let's say you're looking for lines that contain #example and reference [[Python]].

    For Inline Queries (Dataview Inline)

    If you're using an inline query, the syntax might look something like this:

    LIST WHERE contains(tags, "#example") AND contains(file.outlinks, [[Python]])
    

    Or, for a more specific search within the text (not just outlinks), you might need to directly search the file content:

    LIST WHERE contains(file.tags, "#example") AND contains(file.text, "[[Python]]")
    

    However, directly searching file.text is not a standard or recommended approach as file.text is not a standard field that Dataview directly exposes or recommends for querying in this manner.

    For Full Dataview Queries

    For a more complex or detailed query, you might use the Dataview Query Language in a code block like so:

    TABLE file.name, file.tags, file.outlinks
    WHERE contains(file.tags, "#example") AND contains(file.outlinks, [[Python]])
    

    Explanation

    • LIST or TABLE specifies the output format. LIST will give you a list of matching files or results, while TABLE allows you to specify columns to display, making it more informative.
    • WHERE clause is used to filter the results.
    • contains(file.tags, "#example") checks if the file has the #example tag. Note that file.tags returns a list of tags without the # symbol, so you should technically use contains(file.tags, "example").
    • contains(file.outlinks, [[Python]]) checks if the file links to the [[Python]] page. file.outlinks is a list of links going out from the file.

    Correcting the Syntax for Tags

    When checking for tags, remember that Dataview treats tags without the # symbol. So, if you're looking for #example, you should query for "example".

    Final Example

    LIST
    WHERE contains(file.tags, "example") AND contains(file.outlinks, [[Python]])
    

    Or, using TABLE:

    TABLE file.name, file.tags, file.outlinks
    WHERE contains(file.tags, "example") AND contains(file.outlinks, [[Python]])
    

    This should give you a list or table of files that both contain the #example tag and link to the [[Python]] page. Make sure to adjust the tag and link according to your needs.

  3. User

    I actually want to get the line that contains that tag and outlink, not the information about the other file that contains these.

  4. Assistant

    To get the specific lines within your notes that contain both a certain #tag (e.g., #example) and a link to a specific page (e.g., [[Python]]), you can use the DataviewJS query. Dataview's standard query language is powerful, but it operates at the file level. For line-level queries, DataviewJS is more suitable.

    Here's an example of how you can achieve this using DataviewJS:

    const pages = dv.pages();
    const targetTag = "example"; // Without the '#'
    const targetLink = "Python"; // The name of the page you're linking to
    
    for (let page of pages) {
      const fileContent = await dv.io.load(page.file.path);
      const lines = fileContent.split('\n');
      
      for (let i = 0; i < lines.length; i++) {
        if (lines[i].includes(`#${targetTag}`) && lines[i].includes(`[[${targetLink}]]`)) {
          dv.el("p", `${page.file.name}:${i+1} - ${lines[i].trim()}`);
        }
      }
    }
    

    How It Works

    1. Loading Pages: dv.pages() loads all pages in your vault.
    2. Specifying Targets: You define the targetTag and targetLink variables. For tags, you omit the #, and for links, you use the page name as it appears in the link.
    3. Iterating Through Pages and Lines: The script iterates through each page, loads its content, splits it into lines, and checks each line for the presence of both the specified tag and link.
    4. Displaying Results: When a line matches both conditions, it is displayed, showing the file name, line number, and the line's content.

    This DataviewJS query provides a more granular search, allowing you to find specific lines within your notes that match your criteria. Make sure you're using the correct DataviewJS syntax and that your Obsidian setup supports it.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.