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 | | | | | +-----------+ +------------+
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.
- 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. - Download and install the MATLAB language server
- Download the language server (clone or unzip):
git clone https://github.com/mathworks/MATLAB-language-server.gitor 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 RETand set to your installation, e.g.
/usr/local/apps/matlabls/out/index.js
- In Emacs:
- Download the language server (clone or unzip):
- Tell lsp-mode where MATLAB is if MATLAB not on the PATH (
which matlabdoes not find MATLAB).In Emacs:
M-: (require 'lsp-matlab) M-x customize-variable RET lsp-clients-matlab-install-path RETand set to your MATLAB installation e.g.
/usr/local/MATLAB/R2025a - Activate lsp-mode when you open
*.mfiles.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
#'lspwith#'lsp-deferredwhich defers the language server start to after the buffer is visible. - If using matlab-ts-mode
- After loading a
*.mfile, the MATLAB language server will run MATLAB in the “background” and use that MATLAB for language server features such asxref-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)
The examples above were created on Windows 11 using Emacs 30.1. The setup I used (May-29-2025):
- Install Emacs from https://www.gnu.org/software/emacs/download.html
- Install MSYS2 from https://www.msys2.org/
- 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.
- Install the Emacs packages listed above.
- Install the MATLAB Language Server as shown above.





