1

Within VIM, I can execute Octave scripts via this mapping:

map <F6> :w<CR>:!run-octave -q % <CR>

However, as I recently discovered, Octave doesn't show any plots unless there is a "pause" command at the end of a script.

How can I map F6 so this pause command is added automatically each time I invoke the key?

Thanks.

Thor
  • 45,082
  • 11
  • 119
  • 130
Gökhan Sever
  • 8,004
  • 13
  • 36
  • 38

1 Answers1

4

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.

Community
  • 1
  • 1
Thor
  • 45,082
  • 11
  • 119
  • 130
  • The requested solution is good enough for me. It's indeed very handy especially going through 100-150 files in one sit. – Gökhan Sever Mar 06 '13 at 16:12