Editing Vim

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
[http://www.vim.org/ 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.
[http://www.vim.org/ 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 [http://brainacle.com/how-to-write-vim-plugins-with-python.html Python to write Vim plugins]. If you do some for GNU Octave, please let us know.
If you aren't familiar with VIM script language, you can also use [http://brainacle.com/how-to-write-vim-plugins-with-python.html Python to write VIM plugins]. If you do some for GNU Octave, please let us now.


== Vim as the default editor ==
== VIM as the default editor ==
To set Vim as the default editor launched by the GNU Octave {{Codeline|edit}} command, add one of the following line to your {{Path|~/.octaverc}} file:
To set VIM as the default editor launched by the GNU Octave {{Codeline|edit}} command, add the following line to your {{Path|~/.octaverc}} file:


  EDITOR ("gvim")
  edit mode async


EDITOR ("<terminal> -e 'vim'");
and one of the following:


where {{Codeline|<terminal>}} can be [http://directory.fsf.org/wiki/Gnome-terminal gnome-terminal], [http://directory.fsf.org/wiki/Xterm xterm], or any other terminal of your preference. Please note the {{Codeline|-e}} option is common to the mentioned terminals, change it if necessary.
edit editor "<terminal> -e 'vim %s'"


The {{Codeline|edit}} command defaults to asynchronous communication which creates a new process for the launched editor and then runs it in the background.  If, for some reason, you have modified this setting then you will also need to add
edit editor "gvim %s"


edit mode async
where {{Codeline|<terminal>}} can be [http://directory.fsf.org/wiki/Gnome-terminal gnome-terminal], [http://directory.fsf.org/wiki/Xterm xterm], or any other terminal of your preference. Please note the {{Codeline|-e}} option is common to the mentioned terminals, change it if necessary.
 
to the {{Path|~/.octaverc}} file.
 
To use Vim as the default editor without starting a separate window, add the following lines to your {{Path|~/.octaverc}} file:
 
edit mode sync
edit home .
EDITOR('vim > /dev/tty 2>&1 < /dev/tty %s')


== A better GNU Octave syntax file ==
== A better GNU Octave syntax file ==
For now, Vim does not have a dedicated, officially distributed filetype for GNU Octave. The community agreed the best solution is to use [https://www.vim.org/scripts/script.php?script_id=3600 syntax.vim] by Rik. All the instructions for installing it can be found on the hyperlink.  A companion script for indenting Octave m-files from Rik is available at [https://www.vim.org/scripts/script.php?script_id=6056 indent.vim].
As for now, VIM hasn't a dedicated, officially distributed filetype for GNU Octave. The community agreed the best solution is to use [http://www.vim.org/scripts/script.php?script_id=3600 octave.vim] by Rik. All the instructions for installing it can be found on the hyperlink.


== Accessing GNU Octave info ==
== Accessing GNU Octave info ==
GNU Octave info package can be found in almost all Linux distributions. To install the package under Ubuntu, you can type:
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
  $ sudo apt-get install octave<version>-info


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


  autocmd FileType matlab setlocal keywordprg=info\ octave\ --vi-keys\ --index-search
  autocmd FileType matlab setlocal keywordprg=info\ octave\ --vi-keys\ --index-search
Line 40: Line 30:
Now, when editing a {{Path|*.m}} file, you can type {{Key|K}} in normal mode and the word under the cursor will be searched for in the GNU Octave documentation index. Pressing {{Key|,}} yields the next occurrence.
Now, when editing a {{Path|*.m}} file, you can type {{Key|K}} in normal mode and the word under the cursor will be searched for in the GNU Octave documentation index. Pressing {{Key|,}} yields the next occurrence.


Unfortunately info does not work in dumb terminals. As gVim has only a dumb terminal, a workaround must be done to access info.  Two methods are possible.  First, one can to use a plugin, like [https://github.com/carlobaldassi/ConqueTerm ConqueTerm], which can turn a gVim buffer into a terminal emulator.
'''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}}.
Second, one can use an external terminal emulator, preferably a fast loading one such as xterm, and install it.  To installing xterm under Ubuntu, you can type:
 
$ sudo apt-get install xterm
 
Add the following line to your {{Path|~/.vimrc}} file:
 
autocmd FileType octave setlocal keywordprg=xterm\ -e\ info\ octave\ --vi-keys\ --index-search
 
Now you can type {{Key|K}} in normal mode and a new terminal window will opened and the word under the cursor will be searched for in the GNU Octave documentation index.  You can set a larger font and a specific window geometry for xterm with the following line:
 
autocmd FileType octave setlocal keywordprg=xterm\ -fa\ 'DejaVu\ Sans\ Mono:style=Book'\ -fs\ 12\ -geometry\ 80x50\ -e\ info\ octave\ --vi-keys\ --index-search
 
'''NOTE:''' If using Rik's [http://www.vim.org/scripts/script.php?script_id=3600 syntax.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}}. Place 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.
 
[[Category:Editors]]
Please note that all contributions to Octave may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Octave:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)

Templates used on this page: