This may not be the best way to handle running Octave scripts from Vim, see below for an alternative.
The requested solution
To accomplish what you want, define the following function somewhere:
function! RunOctave()
let save_cursor = getpos('.')
call append(line('$'), "pause")
write
execute "!run-octave -q " . expand('%')
$delete _
write
call setpos('.', save_cursor)
endfunction
And define a mapping to call it:
map <F6> :w<CR>:call RunOctave<CR>
A better approach
I started out by running Octave similar to the above, but as I tend to use slow computers this is not a good solution for me. I have instead configured Vim to remote-control an instance of Octave.
This can be done through a terminal multiplexer, similar to what I suggested for Matlab here, e.g.:
tmux new-session -s octave "octave -q"
For optimal flexibility, I want to be able to evaluate single lines in octave from Vim:
imap <buffer> <F1> <Esc>:!tmux send-keys -t octave "<C-r>=getline('.')<CR>"<C-r>=nr2char(13)<CR><CR>a
nmap <buffer> <F1> :!tmux send-keys -t octave "<C-r>=getline('.')<CR>"<C-r>=nr2char(13)<CR><CR>
Evaluate visual selections:
vmap <buffer> <F2> call ExecSelection()<CR><CR>
Which depends on the following function:
let s:octave_selection = '/var/tmp/vim_octave_selection.m'
function! ExecSelection()
if line(".") == line("'<'")
exec ".write! " . s:octave_selection
else
exec ".write! >> " . s:octave_selection
endif
if line(".") == line("'>")
exec "!tmux send-keys -t octave 'run " . s:octave_selection . "'" . nr2char(13)
endif
endfunction
And last, but not least, evaluate the whole file:
imap <buffer> <F3> <Esc>:!tmux send-keys -t octave "<C-r>=expand('%:t:r')<CR>"<C-r>=nr2char(13)<CR><CR>a
nmap <buffer> <F3> :!tmux send-keys -t octave "<C-r>=expand('%:t:r')<CR>"<C-r>=nr2char(13)<CR><CR>
This last mapping requires that the current working directory for the buffer is the same as that of the Octave script.