Enclose text

In Windows, enclosing text with delimiters using a keyboard shortcut (see examples below) can be achieved using a AutoHotkey script.1

#Requires AutoHotkey >=v2.0

; Function to enclose selected text
EnclSelText(openChar, closeChar) {
    A_Clipboard := ""
    SendEvent "^c"
    if ClipWait(0.1) {
        cSelected := A_Clipboard
        Send openChar cSelected closeChar
    }
}

; Hotkeys with different enclosures
^)::EnclSelText("(", ")")
^]::EnclSelText("[", "]")
^"::EnclSelText('"', '"')
^'::EnclSelText("'", "'")

In MacOS, the above can be achieved using Automator; steps described below.

  1. Automator → New → Quick Action
  2. Drag action Run Shell Script from Library to the right pane
  3. Right pane settings:
    • Set to “Automatic (text)”
    • Enable “Output replaces selected text”
    • Set /bin/bash
    • Pass input “as arguments”
    • In the box, add echo "($1)"
  4. Save as Enclose parentheses. Gets saved in ~/Library/Services folder.
  5. From System Settings → Keyboard shortcuts → Services → Text → Click on Enclose parentheses, and hit Ctrl0, click Done

This above would now make the action available in any application.

Enclose text in MacOS using Automator. Services menu under Keyboard shortcuts
Enclose text in MacOS using Automator (top); Services under Keyboard shortcuts to add Ctrl0 to Enclose parentheses (bott).

  1. This note is after all about saving keystrokes.