How to configure vim to interact with interactive command line shells?

view story

http://unix.stackexchange.com – From time to time I want to use vim as scratch pad for commands that I would like to send to a command line shell like psql (Postgres), ghci (Haskell programming language), octave ('calculator'), gnuplot (plot) etc. The advantages would be that you could put comments next to command lines, directly document your session, incrementally develop command lines, test examples ad-hoc in manuals etc. Pro features I would like to use: send a selection to a shell, send e.g. the next 10 lines to a shell, display the output of a shell command into a vim output buffer, into a vim yank-register, directly (HowTos)