Copy Regex-Safe Path
Adds one command: Copy Regex-Safe Path.
It copies a regex-safe form of the active or selected file's path, meant to be
pasted into a test runner filter such as the Vitest CLI.
Two things happen to the path, and you control both:
- Chosen characters get a backslash in front of them.
- Optionally, it is shortened to the filename plus a number of parent
directories.
Why
A test runner filter is a regex, not a path. SvelteKit route directories such
as (authed) and [slug] are valid regex syntax, so an
unescaped path matches zero files and no error is reported.
# matches nothing, silently
pnpm vitest run 'src/routes/(authed)/[slug]/page.test.ts'
# matches the file
pnpm vitest run 'src/routes/\(authed\)/\[slug\]/page.test.ts'
By default the whole workspace-relative path is copied, escaped. A full path is
often longer than a test filter needs, so parentDirectories can trim it to the
filename plus a few parents — usually enough to pick out one file even when
several tests share a filename.
Settings
| Setting |
Default |
What it does |
copyRegexSafePath.parentDirectories |
-1 |
Parent directories kept in front of the filename. -1 is the whole workspace-relative path. 2 keeps two parents. 0 is the filename only. |
copyRegexSafePath.escapeCharacters |
()[] |
Characters that get a backslash. Set it to .*+?^${}()\|[]\ for the full regex metacharacter set, or leave it empty to escape nothing. |
Both settings can be set per folder, which is useful in a monorepo.
Characters in escapeCharacters are matched literally, one at a time. Order
does not matter and no character can break the escaping.
Examples
Starting from this path:
apps/example.com/services/tests/api-service.test.ts
parentDirectories |
Result |
-1 (default) |
the whole path, as shown above |
2 |
services/tests/api-service.test.ts |
0 |
api-service.test.ts |
Starting from a SvelteKit route test:
src/routes/(authed)/[slug]/page.test.ts
escapeCharacters |
Result |
()[] (default) |
\(authed\)/\[slug\]/page.test.ts |
.*+?^${}()\|[]\ |
\(authed\)/\[slug\]/page\.test\.ts |
| empty |
(authed)/[slug]/page.test.ts |
Periods are left alone by default. In a regex . matches any character, which
includes a literal ., so an unescaped period still matches the intended file
and is easier to read.
How to use
- Right click a file in the Explorer, an editor tab, or inside an editor.
- Select several files in the Explorer first to copy them all, one per line.
- Or run the command from the Command Palette, which uses the active editor.
The copied text appears in the status bar for three seconds.
No keyboard shortcut is bound by default, to avoid colliding with one you
already use. To add your own, open Preferences: Open Keyboard Shortcuts and
search for Copy Regex-Safe Path.
Quote it in the shell so the backslashes survive. Single quotes are safest,
because zsh and bash pass everything inside them through untouched:
pnpm vitest run '\(authed\)/\[slug\]/page.test.ts'
Install
Search for Copy Regex-Safe Path in the Extensions view, or install it from
the Visual Studio Marketplace.
From the command line:
code --install-extension kdaisho.copy-regex-safe-path
From source
pnpm dlx @vscode/vsce package --no-dependencies
Then run Extensions: Install from VSIX... from the Command Palette.