@penumbral-labs/pi-copy-code
v0.2.0
Published
A pi extension for ergonomic copying of assistant code blocks.
Maintainers
Readme
pi-copy-code
A pi package for copying fenced code blocks from assistant messages without terminal selection padding.
pi-copy-code adds one command and one shortcut:
/copy-codectrl+alt+c
It reads the latest assistant message, extracts fenced code blocks, and copies the raw code text to your clipboard. If there is more than one block, it opens a small two-pane picker with a live preview.
Features
- Copy raw fenced code instead of rendered terminal cells.
- Preserve whitespace-sensitive YAML, shell, Python, and heredoc indentation.
- Copy one block immediately when there is only one block.
- Choose between multiple blocks with a preview picker.
- Copy all blocks at once from the picker.
- Edit a selected block in your external editor before copying.
- Clipboard fallback order:
- native clipboard command when available (
pbcopy,wl-copy,xclip,xsel,clip.exe) - OSC 52 terminal clipboard sequence when native clipboard commands are unavailable
- native clipboard command when available (
Install
From npm:
pi install npm:@penumbral-labs/pi-copy-codeFrom GitHub:
pi install git:github.com/penumbral-labs/pi-copy-codeOr from a local checkout:
cd /path/to/pi-copy-code
pi install "$(pwd)"Then reload pi:
/reloadFor a one-off run without installing:
pi -e npm:@penumbral-labs/pi-copy-codeor, from GitHub:
pi -e git:github.com/penumbral-labs/pi-copy-codeor, from a local checkout:
pi -e "$(pwd)"Usage
Copy code from the latest assistant message:
/copy-codeor press:
ctrl+alt+cEdit before copying:
/copy-code editWhen multiple blocks are available, the picker opens:
↑/↓orj/k— move selectionenter— run the default action/copy-code: copy/copy-code edit: edit, then copy
e— edit selected block, then copyescorq— cancel
The first picker item is All code blocks, which copies all blocks separated by blank lines.
External editor setup
Edit mode opens your external editor directly using $VISUAL, then $EDITOR.
For NeoVim:
export VISUAL=nvim
# or
export EDITOR=nvimIf neither variable is set, /copy-code edit shows a warning and cancels.
What counts as a code block?
pi-copy-code extracts fenced markdown blocks from the latest assistant message:
```bash
echo hello
```
~~~python
print("hello")
~~~Indented markdown code blocks are not currently extracted.
Package shape
The package uses pi's package manifest in package.json:
{
"keywords": ["pi-package"],
"pi": {
"extensions": ["./extensions/copy-code/index.ts"]
}
}Core pi packages are peer dependencies, per pi package guidance.
Development
Run tests:
npm testCheck publish contents:
npm pack --dry-runTest fixture
Ask pi to emit multiple fenced blocks, then run /copy-code:
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
script.sh: |
if [ -n "$FOO" ]; then
echo "$FOO"
fifrom dataclasses import dataclass
@dataclass
class Config:
host: str
port: int = 8080
@property
def url(self) -> str:
return f"http://{self.host}:{self.port}"