views:

36

answers:

2

I would like to write a command line tool that passes some formatted text to whatever EDITOR the user has set in the environment and then reading the contents back.

How do tools like svn commit and git commit handle this behavior? Is there a standard pattern for doing this?

+1  A: 

hmm, maybe...

  • Create a temporary file with the content you want.
  • Open the file in the $EDITOR.
  • Wait until the editor was closed
  • Check the modification date of the file
    • unchanged: The user has probably aborted the editing process
    • changed: use the content of the modified file
  • Delete the temporary file

If you have a question to one of these tasks, feel free to ask :)

tux21b
This is similar to what I suspected. I was hoping there was series of pipes I could chain to accomplish this but I can try this and refactor later.
stefan.natchev
For what it's worth, tux21b's idea is how git and svn seem to work. The temp file for git is .git/COMMIT_EDITMSG. SVN seems to use svn-commit.tmp.
David Antaramian
looks like it does check for changes, which is less than ideal in my opinion. I think I will take this kind of route instead:http://markmail.org/message/mv5qi54zjwcmbe4g#query:vim%20exit%20status+page:1+mid:xyb4gyhzt3exqcz6+state:results
stefan.natchev
A: 
#!/bin/bash
cat | $EDITOR - &
wait $!
echo "now editor has exited"
profjim
Thanks for the code sample! I did not know about wait $! for background processes
stefan.natchev