# Templater, System Commands, File Lists, Weather, and Git

**URL:** <https://forum.obsidian.md/t/templater-system-commands-file-lists-weather-and-git/36197>\
**Category:** Workflows & Templates\
**Tags:** templater\
**Created:** [April 20, 2022, 1:55pm UTC](https://forum.obsidian.md/t/templater-system-commands-file-lists-weather-and-git/36197 "2022-04-20T13:55:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjineer](https://forum.obsidian.md/user_avatar/forum.obsidian.md/ninjineer/32/29136_2.png) [@ninjineer](https://forum.obsidian.md/u/ninjineer)\
**Post date:** [April 20, 2022, 1:55pm UTC](https://forum.obsidian.md/t/templater-system-commands-file-lists-weather-and-git/36197/1 "2022-04-20T13:55:11Z")

</div>

Today I took a gander at all the forum posts with the Templater tag. Most of them were already closed for replies, but I realized I had some comments that might appply to the following:

- [Templater plugin: What’s the syntax for adding a parameter to a system command user function](https://forum.obsidian.md/t/templater-plugin-whats-the-syntax-for-adding-a-parameter-to-a-system-command-user-function/23162)
- [List of files inside folder](https://forum.obsidian.md/t/list-of-files-inside-folder/24009)
- [Inserting more detailed weather forecasts into a daily note](https://forum.obsidian.md/t/inserting-more-detailed-weather-forecasts-into-a-daily-note/24610/3)

As to the question about how to pass a parameter to a User System Command Function in Templater, I don’t think it is possible. However, I stopped using them in favor a User Script Function that allows me to build the command programmatically before passing it to the function to execute.

```javascript
// In: cmd = a complete command to run in the command line shell. It is up 
// to the user to make certain the command is properly formatted 
// for their platform
// Out: an object with two properties:
// out = the trimmed content the command sent to standard out.
// err = the trimmed content the command sent to standard error.
async function sh(cmd)
{
   const { promisify } = require('util');
   const exec = promisify(require('child_process').exec);
   const result = await exec(cmd);
   return {out: result.stdout.trim(), err: result.stderr.trim()};
}
module.exports = sh;

```

With the full power of the terminal (command line) available it opens up a lot of templating opportunities. For instance, one complex template I created built a table of contents using `head` to extract the first few lines of notes and `wc` to include their word counts. While that might be a bit complex to present here, I offer some concrete examples of the function in action below.

For the first example, the following template builds a simple list of the markdown files found at the root level of a vault:

```javascript
<%*
const root = app.vault.adapter.getBasePath();
const escSpace = root.replace(/(\s)/g, '\\$1'); //escape the space characters
const list = await tp.user.sh(`ls ${escSpace}/*.md`);
const files = list.out.split('\n');
files.forEach(file => {tR += ` + ${file}\n`;});
-%>

```

sample output

```markdown
   + /Users/ninjineer/Documents/Test/2022.calendar.md
   + /Users/ninjineer/Documents/Test/Book.md
   + /Users/ninjineer/Documents/Test/Counting words.md
   + /Users/ninjineer/Documents/Test/New-Day.md
   + /Users/ninjineer/Documents/Test/TVShow.md
   + /Users/ninjineer/Documents/Test/Terrible Idea 2.md
   + /Users/ninjineer/Documents/Test/Terrible Idea First.md

```

The above assumes you’re using MacOS or Linux. For Windows, the same template should look something like the following (note this code is untested as I do not have Obsidian running on a Windows computer):

```javascript
<%*
const root = app.vault.adapter.getBasePath();
const list = await tp.user.sh(`dir "${root}\\*.md" /B`);
const files = list.out.split('\r\n');
files.forEach(file => {tR += ` + ${file}\r\n`;});
-%>

```

With a little more effort, once could also turn that file list into a list of links, like for a MOC:

```javascript
<%*
const root = app.vault.adapter.getBasePath();
const escSpace = root.replace(/(\s)/g, '\\$1');
const list = await tp.user.sh(`ls ${escSpace}/*.md`);
const files = list.out.split('\n');
files.forEach(file => {
   const parts = file.split('/');
   const name = parts[parts.length - 1].slice(0,-3)
   tR += ` + [[${name}]]\n`;
});
-%>

```

sample output

```markdown
   + [[2022.calendar]]
   + [[Book]]
   + [[Counting words]]
   + [[New-Day]]
   + [[TVShow]]
   + [[Terrible Idea 2]]
   + [[Terrible Idea First]]

```

Another example comes from the post about inserting weather forcasts. The template for doing that in my location looks like:

````javascript
<%*
const w = await tp.user.sh(`curl "https://wttr.in/Pohnpei?TuF0"`);
tR += '```\n' + w.out + '\n```\n';
-%>

````

sample output

```markdown
Weather report: Pohnpei

                Overcast
       .--. +78(86) °F     
    .-( ). ← 2 mph        
   ( ___.__ )__) 14 mi          
                0.1 in

```

I’ll leave you with one final example that occurred to me as I wrote this post. Since my vault is also a git repository, I periodically have to jump over to the terminal and commit my changes. With this `sh` function, I can use a template to automate this task:

```javascript
<%*
let msg = await tp.system.prompt("Git Commit Message","");
if (msg != null && msg.length > 1)
{
   const root = app.vault.adapter.getBasePath();
   const escSpace = root.replace(/(\s)/g, '\\$1');
   const c = await tp.user.sh(`cd ${escSpace} && git add --all && git commit -m "${msg}"`)
   console.log(c.out);
}
-%>

```

Note, I directed the output from the command to the console, but I could have just as easily funneled it into a note, if I wanted to keep a log of such commits for some reason.

I hope these examples trigger fresh idea in others. Thanks.

---

<div class="post-metadata">

**Author:** ![apogorzelska](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@apogorzelska](https://forum.obsidian.md/u/apogorzelska)\
**Post date:** [April 23, 2022, 5:18am UTC](https://forum.obsidian.md/t/templater-system-commands-file-lists-weather-and-git/36197/2 "2022-04-23T05:18:08Z")

</div>

Your solution is really cool, I’m definitely bookmarking it for possible future use. Thank you!

> As to the question about how to pass a parameter to a User System Command Function in Templater, I don’t think it is possible.

It is! Sorry, I forgot to post the solution in the forum thread before it got automatically closed. [Here is the answer in Obsidian Discord](https://discord.com/channels/686053708261228577/840286238928797736/881208393136762971). In short: you use the `$parameter` syntax on Linux and `%parameter%` syntax on Windows, and pass the parameter value into the function within the template like this: `tp.user.function({ parameter: value })`.

Here is actual example of a User System Command Function I named `getYoutubeMeta` used for getting Youtube video metadata through [youtube-dl](https://github.com/ytdl-org/youtube-dl):

Linux:

```auto
youtube-dl $ytLink --skip-download --dump-json

```

Windows:

```auto
youtube-dl %ytLink% --skip-download --dump-json

```

And that’s how you then call it within a simple example of a note template, together with parsing the resulting JSON to get two example bits of video information (title and channel) into the frontmatter:

```auto
<%* let link = await tp.system.prompt("Link");
let youtubeMeta = JSON.parse(await tp.user.getYoutubeMeta({ ytLink: link }));
let title = youtubeMeta.title;
let channel = youtubeMeta.channel;
-%>

---
title: "<% title %>"
channel: "<% channel %>"
---

```

(The actual `getYoutubeMeta` function call bit is `tp.user.getYoutubeMeta({ ytLink: link })` in case a newbie reading this gets confused. You can get the full list of metadata youtube-dl has access to [here](https://github.com/ytdl-org/youtube-dl#output-template).)

---

<div class="post-metadata">

**Author:** ![TUp1](https://forum.obsidian.md/user_avatar/forum.obsidian.md/tup1/32/33795_2.png) [@TUp1](https://forum.obsidian.md/u/TUp1)\
**Post date:** [August 18, 2022, 11:13pm UTC](https://forum.obsidian.md/t/templater-system-commands-file-lists-weather-and-git/36197/3 "2022-08-18T23:13:16Z")

</div>

This is such a brilliant solution. Thank you so much. Will surely be using it 😄

---

<div class="post-metadata">

**Author:** ![RankkaApina](https://forum.obsidian.md/user_avatar/forum.obsidian.md/rankkaapina/32/8034_2.png) [@RankkaApina](https://forum.obsidian.md/u/RankkaApina)\
**Post date:** [November 14, 2025, 2:54pm UTC](https://forum.obsidian.md/t/templater-system-commands-file-lists-weather-and-git/36197/4 "2025-11-14T14:54:18Z")

</div>

I know this thread is old, but I wanted to thank you for your `sh` function. I was trying to do something completely different and it made it so easy! And it even works from Windows: I wanted to call WSL, because all my codes and installed coding tools are in WSL while my obsidian vault is on the Windows side. This is an amazing script!!!
