Monday, January 28, 2013

Augmenting Octave with Vim

Integrating the powerful mathematical package, Octave, with the flexible editor, Vim.

Today, I'm sharing some tips to use the vim editor inside the Octave interactive command line.

The octaverc file


The first thing towards customizing Octave is creating an .octaverc file, if you don't have one already. The .octaverc file holds some Octave commands that are executed every time you launch Octave.

We'll work with the .octaverc file that lives in your home directory, which is user specific.
# Create/modify .octaverc in your home folder
vim ~/.octaverc
A good starting .octaverc file looks as follows:

Using vim as editor in Octave


Next, let's set vim as the default editor in Octave. (source of information)
# Add extra configuration
vim ~/.octaverc
Open the .octaverc file and append the following:
Now you can call vim from within Octave, and use it in command line mode, i.e. without opening an (unnecessary) additional window.

As a bonus you get a function template.

Now, let's tweak vim for extra features!

Adding syntax highlighting


We'll add syntax coloring/highlighting to vim. Similar to the .octaverc file, vim also has a configuration file named .vimrc. Before touching the .vimrc file, you'll need to grab a vim syntax file from here (grab the latest version).

Next you'll need to drop that octave.vim file in the following directory: ~/.vim/syntax.
# If you don't have the folders already
mkdir -p ~/.vim/syntax
mv /path/to/downloaded/octave.vim ~/.vim/syntax/octave.vim
Next, you'll need to append the following lines in your .vimrc file.
Let's try editing a file inside Octave again.

Added Octave syntax highlighting for vim.

Execute Octave scripts from within vim


Here is where the magic comes in. With this hack, you'll be able to test the octave script you are editing in vim, inside vim and without leaving vim. In other words, you'll be able to use the F5 key to run your script, just like you do in the Matlab editor.

Without further ado, you'll need to add this to your .vimrc
Let's test the magic.



Executing an Octave script from within vim using the F5 key in normal mode.

I need to inform you that this hack adds two extra lines of code to your script:

"pkg load all" at the beginning of your code. Which grants access to all functions inside the extra Octave packages.

"pause" at the end of your code. Without this you won't be able to see the plots drawn in your script.

When the execution of the script finishes, these two lines are removed.

That's all folks, happy coding!

6 comments:

  1. Thanks! This works great.

    I found that vim in Apple's Terminal doesn't seem to work with the F5 thing, though: No biggie, MacVim does it right and nails it.


    ReplyDelete
  2. Should that be a " character instead of a # for the comment in .vimrc? I.e.:

    " F5 executes the octave script you are editing

    ReplyDelete
  3. I'm having some trouble with the path variable in Octave.

    I put the following in my octaverc file:
    edit editor 'vim > /dev/tty 2>&1 < /dev/tty %s'

    and I edited my system path to include the parent director of vim74.exe

    however, when I restart Octave and type:
    >> edit foo.m

    it reports the following error:
    The system cannot find the path specified

    Anyone have any suggestions?

    ReplyDelete
  4. I'm on windows, and there is no /dev/tty, so that was the problem. The following seems to work:

    edit editor "gvim %s"

    Is there an equivalent for /dev/tty on windows that I should use?

    ReplyDelete
  5. I've enhanced the executing function. With my version the cursor jumps back to the position, where it was before pressing f5:

    " F5 executes the octave script you are editing
    autocmd FileType octave map maggOpkg load allGopause:w:!octave -qf %ddggdd:w`a

    Happy Coding!

    ReplyDelete