If you are lucky, your sysadmins have already arranged everything so that Emacs automatically goes into Octave mode whenever you visit an Octave code file as characterized by its extension .m. If not, proceed as follows.
(autoload 'octave-mode "octave-mod" nil t) (setq auto-mode-alist (cons '("\\.m$" . octave-mode) auto-mode-alist))
(add-hook 'octave-mode-hook (lambda () (abbrev-mode 1) (auto-fill-mode 1) (if (eq window-system 'x) (font-lock-mode 1))))
See the Emacs manual for more information about how to customize Font-lock mode.
In Octave mode, the following special Emacs commands can be used in addition to the standard Emacs commands.
Describe the features of Octave mode.
Reindent the current Octave line, insert a newline and indent the new
octave-reindent-then-newline-and-indent). An abbrev before
point is expanded if
abbrev-mode is non-
Indents current Octave line based on its contents and on previous
Insert an “electric” semicolon (
octave-auto-indent is non-
nil, reindent the current line.
octave-auto-newline is non-
nil, automagically insert a
newline and indent the new line.
Start entering an abbreviation (
octave-abbrev-start). If Abbrev
mode is turned on, typing `C-h or `? lists all abbrevs.
Any other key combination is executed normally. Note that all Octave
abbrevs start with a grave accent.
Break line at point and insert continuation marker and alignment
Perform completion on Octave symbol preceding point, comparing that
symbol against Octave’s reserved words and built-in variables
Move backward to the beginning of a function
With prefix argument N, do it that many times if N is
positive; otherwise, move forward to the N-th following beginning
of a function.
Move forward to the end of a function (
With prefix argument N, do it that many times if N is
positive; otherwise, move back to the N-th preceding end of a
Puts point at beginning and mark at the end of the current Octave
function, i.e., the one containing point or following point
Properly indents the Octave function which contains point
If there is no comment already on this line, create a code-level comment
(started by two comment characters) if the line is empty, or an in-line
comment (started by one comment character) otherwise
Point is left after the start of the comment which is properly aligned.
Puts the comment character ‘#’ (more precisely, the string value of
octave-comment-start) at the beginning of every line in the
octave-comment-region). With just C-u prefix
argument, uncomment each line in the region. A numeric prefix argument
N means use N comment characters.
Uncomments every line in the region (
Move one line of Octave code backward, skipping empty and comment lines
octave-previous-code-line). With numeric prefix argument
N, move that many code lines backward (forward if N is
Move one line of Octave code forward, skipping empty and comment lines
octave-next-code-line). With numeric prefix argument N,
move that many code lines forward (backward if N is negative).
Move to the ‘real’ beginning of the current line
octave-beginning-of-line). If point is in an empty or comment
line, simply go to its beginning; otherwise, move backwards to the
beginning of the first code line which is not inside a continuation
statement, i.e., which does not follow a code line ending in ‘...’
or ‘\’, or is inside an open parenthesis list.
Move to the ‘real’ end of the current line (
If point is in a code line, move forward to the end of the first Octave
code line which does not end in ‘...’ or ‘\’ or is inside an
open parenthesis list. Otherwise, simply go to the end of the current
Move forward across one balanced begin-end block of Octave code
octave-forward-block). With numeric prefix argument N,
move forward across n such blocks (backward if N is
Move back across one balanced begin-end block of Octave code
octave-backward-block). With numeric prefix argument N,
move backward across N such blocks (forward if N is
Move forward down one begin-end block level of Octave code
octave-down-block). With numeric prefix argument, do it that
many times; a negative argument means move backward, but still go down
Move backward out of one begin-end block level of Octave code
octave-backward-up-block). With numeric prefix argument, do it
that many times; a negative argument means move forward, but still to a
less deep spot.
Put point at the beginning of this block, mark at the end
The block marked is the one that contains point or follows point.
Close the current block on a separate line (
An error is signaled if no block to close is found.
Insert a function skeleton, prompting for the function’s name, arguments
and return values which have to be entered without parentheses
Search the function, operator and variable indices of all info files
with documentation for Octave for entries (
octave-help). If used
interactively, the entry is prompted for with completion. If multiple
matches are found, one can cycle through them using the standard
Info-index-next) command of the Info reader.
octave-help-files is a list of files to search
through and defaults to
'("octave"). If there is also an Octave
Local Guide with corresponding info file, say, octave-LG, you can
octave-help search both files by
(setq octave-help-files '("octave" "octave-LG"))
in one of your Emacs startup files.
A common problem is that the RET key does not indent the line to where the new text should go after inserting the newline. This is because the standard Emacs convention is that RET (aka C-m) just adds a newline, whereas LFD (aka C-j) adds a newline and indents it. This is particularly inconvenient for users with keyboards which do not have a special LFD key at all; in such cases, it is typically more convenient to use RET as the LFD key (rather than typing C-j).
You can make RET do this by adding
(define-key octave-mode-map "\C-m" 'octave-reindent-then-newline-and-indent)
to one of your Emacs startup files. Another, more generally applicable solution is
(defun RET-behaves-as-LFD () (let ((x (key-binding "\C-j"))) (local-set-key "\C-m" x))) (add-hook 'octave-mode-hook 'RET-behaves-as-LFD)
(this works for all modes by adding to the startup hooks, without having
to know the particular binding of RET in that mode!). Similar
considerations apply for using M-RET as M-LFD. As Barry
A. Warsaw firstname.lastname@example.org says in the documentation for his
cc-mode, “This is a very common question.
:-) If you want
this to be the default behavior, don’t lobby me, lobby RMS!”
The following variables can be used to customize Octave mode.
nil means auto-indent the current line after a semicolon or
space. Default is
nil means auto-insert a newline and indent after semicolons
are typed. The default value is
nil means show matching begin of block when inserting a space,
newline or ‘;’ after an else or end keyword. Default is
This is an extremely useful feature for automatically verifying that the
keywords match—if they don’t, an error message is displayed.
Extra indentation applied to statements in block structures. Default is 2.
Extra indentation applied to Octave continuation lines. Default is 4.
String used for Octave continuation lines. Normally ‘\’.
t (default), a startup message is displayed when Octave mode
If Font Lock mode is enabled, Octave mode will display
There is also rudimentary support for Imenu (currently, function names can be indexed).
You can generate TAGS files for Emacs from Octave .m files using
the shell script
octave-tags that is installed alongside your copy of
Customization of Octave mode can be performed by modification of the
octave-mode-hook. If the value of this variable is
nil, turning on Octave mode calls its value.
If you discover a problem with Octave mode, you can conveniently send a
bug report using C-c C-b (
automatically sets up a mail buffer with version information already
added. You just need to add a description of the problem, including a
reproducible test case and send the message.