Vim: Difference between revisions

From Octave
Jump to navigation Jump to search
m (moved VIM to Vim: cosmetic reasons)
No edit summary
Line 31: Line 31:


'''OBS:''' If using the Rik's [http://www.vim.org/scripts/script.php?script_id=3600 octave.vim] syntax, replace {{Codeline|matlab}} by {{Codeline|octave}}.
'''OBS:''' If using the Rik's [http://www.vim.org/scripts/script.php?script_id=3600 octave.vim] syntax, replace {{Codeline|matlab}} by {{Codeline|octave}}.
== Jumping between control statements ==
GNU Octave has a richer set of closing tags ({{Codeline|endif}},{{Codeline|endfor}},...) but for compatibility with MATLAB most users avoid them. This sometimes makes the code hard to follow and one possible workaround is to enable the [http://www.vim.org/scripts/script.php?script_id=39 matchit.vim] plugin for jump between matching control statements. Although the plugin is distributed with Vim, it's disabled by default (see {{Codeline|:help matchit-install}}). To enable it, add the following lines to your {{Path|~/.vimrc}} file:
set nocompatible
filetype plugin on
runtime macros/matchit.vim
Now that's enabled, one needs to specify the matching pairs for the GNU Octave language. The less broken solution i've found by [http://vim.1045645.n5.nabble.com/Matchit-and-identical-closing-tags-tt1145032.html#a1145034 Jake Wasserman]:
let s:conditionalEnd = '\(([^()]*\)\@!\<end\>\([^()]*)\)\@!'
autocmd FileType octave let b:match_words = '\<if\>\|\<while\>\|\<for\>\|\<switch\>:' .
        \ s:conditionalEnd . ',\<if\>:\<elseif\>:\<else\>:' . s:conditionalEnd
It allows to jump (quasi-)correctly even in the presence of array indexing with {{Codeline|end}}. Put the cursor on an {{Codeline|if}} keyword for example and press {{Key|%}}, it'll move to the corresponding {{Codeline|elseif}}, {{Codeline|else}}, {{Codeline|end}} keywords.
Any improvements on the {{Codeline|b:match_words}} variable are welcome.

Revision as of 20:24, 11 November 2012

Vim the editor is one of the most famous text editors in the hacker programming universe. It has a plenty of extensible (script-based) capabilities and very comfortable shortcuts that makes the programmer the fastest gun of the old west. (I can imagine Vim in front of Emacs on a desert city, Vim's shot is much faster!). In this page you'll learn some tips to better adapt Vim for GNU Octave programming.

If you aren't familiar with Vim script language, you can also use Python to write Vim plugins. If you do some for GNU Octave, please let us now.

Vim as the default editor

To set Vim as the default editor launched by the GNU Octave edit command, add the following line to your ~/.octaverc file:

edit mode async

and one of the following:

edit editor "<terminal> -e 'vim %s'"
edit editor "gvim %s"

where <terminal> can be gnome-terminal, xterm, or any other terminal of your preference. Please note the -e option is common to the mentioned terminals, change it if necessary.

A better GNU Octave syntax file

As for now, Vim hasn't a dedicated, officially distributed filetype for GNU Octave. The community agreed the best solution is to use octave.vim by Rik. All the instructions for installing it can be found on the hyperlink.

Accessing GNU Octave info

GNU Octave info package can be found in almost all Linux distributions. For installing it under Ubuntu, you can type:

$ sudo apt-get install octave<version>-info

where <version> must be substituted by the appropriate string. Add the following line to your ~/.vimrc file:

autocmd FileType matlab setlocal keywordprg=info\ octave\ --vi-keys\ --index-search

Now, when editing a *.m file, you can type K in normal mode and the word under the cursor will be searched for in the GNU Octave documentation index. Pressing , yields the next occurrence.

OBS: If using the Rik's octave.vim syntax, replace matlab by octave.

Jumping between control statements

GNU Octave has a richer set of closing tags (endif,endfor,...) but for compatibility with MATLAB most users avoid them. This sometimes makes the code hard to follow and one possible workaround is to enable the matchit.vim plugin for jump between matching control statements. Although the plugin is distributed with Vim, it's disabled by default (see :help matchit-install). To enable it, add the following lines to your ~/.vimrc file:

set nocompatible
filetype plugin on
runtime macros/matchit.vim

Now that's enabled, one needs to specify the matching pairs for the GNU Octave language. The less broken solution i've found by Jake Wasserman:

let s:conditionalEnd = '\(([^()]*\)\@!\<end\>\([^()]*)\)\@!'
autocmd FileType octave let b:match_words = '\<if\>\|\<while\>\|\<for\>\|\<switch\>:' .
       \ s:conditionalEnd . ',\<if\>:\<elseif\>:\<else\>:' . s:conditionalEnd

It allows to jump (quasi-)correctly even in the presence of array indexing with end. Put the cursor on an if keyword for example and press %, it'll move to the corresponding elseif, else, end keywords.

Any improvements on the b:match_words variable are welcome.