I wish to create a temporary file using mktemp
and then edit it's content after creation (using your preferred editor; I used micro
) and then when finished i.e. saved/exited the process it should output path to the file to stdout/pipe/replace in place as argument.
I can do all but print the path of the file created and used. I would not like to rely on editors ability to output path when closing so any editor can be used.
What I was trying.
# creates a file. passes it's path to editor to open it.
# then we can make changes and save. finally quit.
> micro (mktemp)
But output is nothing, I want it to be the original path passed in. How can I do that?
These are example use cases, they don't work as I cannot output the path after saving.
# e.g. a full test case; create tmp file, fill it, read it, find it...
> micro (mktemp) | cat | grep 'find me*!'
# or you want to count the words
> micro (mktemp) | tail | wc
# another way it should work as well! i.e. pros-sub
> cat (micro (mktemp)) | sed 's/red/green/g'
# bonus points (I mean it, 50 extra karma). You might need to restructure the chain
# i.e. ->create tmp, fill it, save it, read it, manipulate it, save it
# back to disk (append/replace)
> cat (micro (mktemp)) | sed ' s/red/green/g' >> <original_file_path>
I'm working in fish
because I'm rosy eyed and am looking for solution in that. However if you know it in bash
already, would love to see it and can be useful. So I'll tag both
In the fish shell, you could define a function, here mt
:
function mt
set -l path (mktemp)
micro $path </dev/tty >/dev/tty
cat $path
end
Now your pipelines can work with mt
:
mt | wc