# f()



You all, of course, know about the `fc` command. From bash's help system:

> fc: fc [-e ename] [-nlr] [first] [last] or fc -s [pat=rep] [cmd]
>
> fc is used to list or edit and re-execute commands from the history list.
> FIRST and LAST can be numbers specifying the range, or FIRST can be a
> string, which means the most recent command beginning with that
> string.

Now I had the following problem: you have a file with shell commands in
it. Next you want to select a few lines from this file to be executed in
your running shell. A way to do this is:

 * edit the file with `vi`
 * save this edited file under a different name, say `$file`
 * execute it: `. $file` 

But this is cumbersome *and* I wanted the executed lines to be added
to my shell's history. So that is why I created the function: `f()`:

 * edit a file, exec what's left
 * you are finished editing
 * add what is executed to the history (fc -R)

It needs to be a shell function, because as an external executable you
cannot alter your current shell history.

Synopsis: `f filename`

This is the body of the function:

    f() {
	if [[ ! -f $1 ]]; then return 1; fi

	copy=$(mktemp ${TMPPREFIX:-/tmp/shell}.XXXXXXX)
	if cp $1 $copy; then
	    if ${EDTIOR:-vi} $copy; then
		$SHELL $copy
		# add to hist
		fc -R ${copy}
		rm -f $copy
	    fi
       else
	    return 1
	fi
    }

How does it work?

 * Check if we have an argument
 * If so, copy the file to a temporary file
 * Edit this temporary file
 * Execute the contents when the editor did a normal exit
 * Add the results to the current history (`fc -R`)
 * Remove the temporary file

