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!


  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.

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

    " F5 executes the octave script you are editing

  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?

  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?

  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!

  6. During to long search I have perfect blog. Your post is very reachable to all I love your blog. Why you not update about boom lift & Industrial Aluminium Scaffolding.

  7. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.

  8. Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is fantastic, let alone the content!

    3D Animation services in Atlanta
    Cheap Rate Chatbot Development Service
    Cheap Rate Mobile app development in Coimbatore

  9. Wow, Great information and this is very useful for us.

    Aluminium scaffolding hire

  10. AE Clicks is one of the top website development company in dubai. With a team of highly skilled web developers and web designers, we have successfully completed over 300+ projects across various industries.