Editing Text in-stream

02 June 2026

Updated: 02 June 2026

I’ve recently been finding sed really handy but sometimes I want to edit something bigger in-stream - maybe just to make some small manual fixes before dumping dumping a string out to some other command. I’ve been using pbcopy a lot for this kind of thing but I wanted a neater way to do it, and here’s my solution

Terminal window
1
# Edit file in stream by writing to a file, opening the editor and printing back to stdout
2
#
3
# File will be stored in a temp file in the current directory and deleted once edit is complete
4
#
5
# Example edit
6
# > bat package.json | f edit-stream json | save updated.json
7
#
8
def "f edit-stream" [
9
# extension to use when writing the file. Useful to get syntax highlighting
10
ext: string = "txt",
11
# prefix to be used for tempfile
12
prefix: string = "ed-",
13
# if the temp file should be retained after editing
14
--keep,
15
] {
16
let f = $"($prefix)(date now | format date '%+').($ext)"
17
18
$in | save $f
19
nu -c $"($env.EDITOR) ($f)"
20
21
if not $keep {
22
bat $f
23
}
24
}
25
26
alias fed = f edit-stream

This is a Nushell function which makes it possible to do just that, and in practice could look something like this:

Terminal window
1
bat package.json # open a file using `bat`
2
| f edit-stream json # open the file in the configured $EDITOR
3
| save updated.json # save this to some other output

By default this also uses bat to print the output to give syntax highlighting to users but will also pass the plain text on if being passed to another command