Shell commands plugin

Hi all,

Shell commands lets you define custom shell commands in settings and then run them via hotkeys or command palette. You can use it to open external applications or for automated processing of files. A few built-in variables are supported, so you can supplement your commands with for example current file path/folder, current date/time, selected text, clipboard content etc.

Examples

A few example commands (for Windows*):

  • start git-gui: Open a graphical Git client for committing changes in your vault to a repository.
  • start cmd: A quick way to run other commands that you have not defined in the settings.
  • mkdir {{date:YYYY-MM}}: Create a monthly folder like 2021-08.
  • echo {{clipboard}} >> NewNote.md: Create a new file and paste content there from clipboard.
  • start https://duckduckgo.com/?q={{selection}}: Search the web using text you have selected.

The plugin does not predefine any commands, those are only example ideas.

*) Supported platforms are Windows, Linux, and Mac. You need to customise the given examples if you want to use them on Linux or Mac. Mobile is not supported. I’d like to investigate if it’s possible to run shell commands in the mobile app. Currently, it’s really uncertain to me (help is welcome :slight_smile: ).

Note that for the sake of simplicity, there is no escaping done for variable values. If you have a command and a quoted string parameter like mycommand "{{clipboard}}", it might break if your clipboard content contains " quote characters, because those are inserted into the command as-is. Your command might end up looking like this: mycommand "Text pasted from clipboard that contains a " character."

Still being tested

This plugin in still in its 0.x era, meaning that there might be things that do not work as intended or things that could be improved. But the basic functionality that I wanted it to have is already there. I’d like to receive feedback about this plugin. If you have ideas for new features, new variables, improvements or fixes, you can freely write them to:

  • Shell commands’ GitHub issues - the easiest for me to work on these ideas.
  • In this topic - in case you don’t have a GitHub account or if it’s just general feedback or questions how to use this plugin.

I’m open to all discussion about how to change and improve this plugin in the future.

A word of warning

Be careful with system commands! Only use commands that you know and trust. If you are copy pasting commands from the internet or from files written by other people, you need to understand precisely what those commands do! Otherwise, you might lose your files, or screw up your system! This plugin doesn’t come with any kind of warranty.

P.S.

I also noticed another plugin that has the ability to run shell commands: Text Expander Plugin. It lets you write shell commands in your MarkDown files and run them from there, which might be better suited for some situations, such as scripts that are long or that need to be previewed every time before running them. My plugin works best for simple, short commands, even though you can run multiple commands by concatenating them with & in windows or && in Linux and Mac.

5 Likes

I just released version 0.3.0. During my opening post the latest version was 0.1.0, so I’ll add here a short summary of notable updates between 0.1.0 and 0.3.0:

3 Likes

I’ve continued developing the plugin during the past few months, so I’ll write a status update here.

Current release version: 0.9.0
Beta test version: 0.10.0-beta.1

Notable changes between 0.3.0 and 0.9.0

  • Possibly backwards incompatible change: {{variable}} values are escaped when using PowerShell or Bash/Dash/Zsh (no escaping when using CMD). Check that your variables work correctly after this upgrade! This change should improve the safety of variables, but it’s experimental - although so far it has been working very well! :slightly_smiling_face:
  • You can now select a shell where your shell commands will be executed in. Supported shells:
    • For Windows: CMD, PowerShell 5, PowerShell Core
    • For Linux and macOS: Bash, Dash, Zsh
  • Shell command output can now be directed to:
    • A notification balloon
    • Currently open note (top, bottom or caret position)
    • Status bar
    • Clipboard
  • New variables:
    • {{caret_position}}
    • {{file_extension}}
    • {{tags}}
    • {{yaml_value}}
  • Autocompletion menu for typing variables quickly in the settings. You can also add your custom list of e.g. commands or whatever keywords.
  • Operating system specific versions of shell commands: If you sync your vault (and SC settings file) between multiple devices that use different OSes, you can write multiple versions of a shell command, and SC will execute a version suitable for the current OS. E.g. if you have shell command that lists files in the vault’s root, for Windows you can write dir, and for macOS/Linux you can write ls.
  • You can choose not to display error messages for certain exit codes.
  • Shell commands can be set to ask a confirmation before execution.

More details are available in changelog.

New features in 0.10.0 (edit 2022-02-06: Released now)

So far the Shell commands plugin has offered a way to execute shell commands via the command palette and hotkeys. The version 0.10.0 also allows you to define events that execute your shell commands automatically.

The first set of events include:

  • After Obsidian starts
  • Before Obsidian quits
  • After switching the active pane
  • Every n seconds
  • You can make your shell commands appear in menus, too:
    • File navigation menu
    • Folder navigation menu
    • Editor menu

Cheers! :sunglasses:

4 Likes

A shorter update on notable new features.

0.11.0 - 2022-02-26

  • Output channel Open a file can be used to receive a file name/path as output from a shell command, and open the file in Obsidian. It supports opening an existing or a new file. It also allows placing carets and selecting text in the opened note, based on line/column coordinates.
  • Less prominent changes

0.12.0 - 2022-05-07

  • When a shell command is about to be executed, a prompt modal can be opened up to ask for a user to input text values that will be used as part of the shell command.
  • Less prominent changes