Skip to content

Instantly share code, notes, and snippets.

@ckunte
Created May 25, 2025 12:47
Show Gist options
  • Save ckunte/688c48f7d003ad14ec5a17b07f9617c4 to your computer and use it in GitHub Desktop.
Save ckunte/688c48f7d003ad14ec5a17b07f9617c4 to your computer and use it in GitHub Desktop.
Enclose text in parentheses

Enclose text

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

  • Ctrl " : adds double quotes to the phrase
  • Ctrl ' : adds single quotes -- do --
  • Ctrl ] : adds square brackets -- do --
  • Ctrl ) : adds parentheses -- do --
#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("'", "'")

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 Ctrl 0, click Done

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment