Skip to content

Latest commit

 

History

History
166 lines (117 loc) · 6.19 KB

File metadata and controls

166 lines (117 loc) · 6.19 KB

MATLAB Language Server and Emacs

The MATLAB Language Server, matlabls, is used for code navigation, code completion, go to definition, find references, and more. To use matlabls, see the Installation section below.

matlabls is a separate special MATLAB process that Emacs communicates with. For example, when you ask Emacs to jump to the definition of a function via M-. or M-x xref-find-definitions, Emacs will ask the matlabls process where the function is located and use its response to jump to the definition.

+-----------+            +------------+
|           |            |            |
|   Emacs   |<==========>|  matlabls  |
|           |            |            |
+-----------+            +------------+

Code Navigation - Jump to Definition and Back

Below we are in lspExample.m and then type M-. to jump to the definition in func3.m. We then type M-, to jump back.

matlab-language-server-lsp-mode-files/matlab-lsp-mode-jump-to-definition.gif

Code Navigation - Find References

matlab-language-server-lsp-mode-files/matlab-and-lsp-mode-find-references.png

Rename symbols and identifiers, M-x lsp-rename

matlab-language-server-lsp-mode-files/matlab-and-lsp-mode-rename.png

Viewing Issues Without the lsp-ui Package

matlab-language-server-lsp-mode-files/matlab-and-lsp-mode-flycheck.png

Viewing Issues With the lsp-ui Package

matlab-language-server-lsp-mode-files/matlab-and-lsp-ui-sideline-mode.png

Navigation with lsp-ui-imenu

matlab-language-server-lsp-mode-files/matlab-and-lsp-ui-imenu.png

Installation

  1. Install lsp-mode from MELPA
    (require 'package)
    (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
        

    then

    M-x package-install RET lsp-mode RET
    M-x package-install RET company RET            // for TAB completions
    M-x package-install RET flycheck RET           // for diagnostics (mlint warnings).
    M-x package install RET lsp-ui RET             // See https://github.com/emacs-lsp/lsp-ui.
        
  2. Download and install the MATLAB language server
    • Download the language server (clone or unzip):
      git clone https://github.com/mathworks/MATLAB-language-server.git
              

      or download and unzip a release from https://github.com/mathworks/MATLAB-language-server/releases

    • In the cloned or unzipped directory,
      npm run project-install
      npm run compile
      npm run package  # optional JavaScript minimization
      
      # Install by copying built items into an install directory, e.g. /usr/local/apps/matlabls
      cp -r ./out/ /usr/local/apps/matlabls/out/
      cp -r ./matlab/ /usr/local/apps/matlabls/matlab/
              
      • In Emacs:
        M-: (require 'lsp-matlab)
        M-x customize-variable RET lsp-clients-matlab-server RET
                    

        and set to your installation, e.g. /usr/local/apps/matlabls/out/index.js

  3. Tell lsp-mode where MATLAB is if MATLAB not on the PATH (which matlab does not find MATLAB).

    In Emacs:

    M-: (require 'lsp-matlab)
    M-x customize-variable RET lsp-clients-matlab-install-path RET
        

    and set to your MATLAB installation e.g. /usr/local/MATLAB/R2025a

  4. Activate lsp-mode when you open *.m files.

    Add to your ~/.emacs

    • If using matlab-ts-mode
      (add-hook 'matlab-ts-mode-hook #'lsp)
              
    • If using matlab-mode
      (add-hook 'matlab-mode-hook #'lsp)
              

    It is safe to add both of the above, if you switch between matlab-ts-mode and matlab-mode.

    Replace #'lsp with #'lsp-deferred which defers the language server start to after the buffer is visible.

  5. After loading a *.m file, the MATLAB language server will run MATLAB in the “background” and use that MATLAB for language server features such as xref-find-definitions. It can take a long time for this background MATLAB to start. On some systems, this can be seconds and others minutes. After the server starts, the supported language server features will work. See https://github.com/mathworks/MATLAB-language-server for features supported.

    Some lsp features:

    M-.   Find the definition of the identifier at point (xref-find-definitions)
    M-?   Find references to the identifier at point (xref-find-references)
    M-,   Go back to the previous position in xref history (xref-go-back)
        

Windows Emacs with LSP

The examples above were created on Windows 11 using Emacs 30.1. The setup I used (May-29-2025):

  1. Install Emacs from https://www.gnu.org/software/emacs/download.html
  2. Install MSYS2 from https://www.msys2.org/
  3. Install gpg from https://www.gpg4win.org/. Note there is a version gpg.exe in MSYS2 that does not work with the Emacs package manager. Therefore, install gpg4win and place that gpg on the PATH before the gpg from MSYS2.
  4. Install the Emacs packages listed above.
  5. Install the MATLAB Language Server as shown above.