Emacs is a highly extensible, customizable text editor known for its powerful editing capabilities and a vast ecosystem of packages. It has been around since the 1970s and is still widely used by developers today. Emacs can be customized using Emacs Lisp, a dialect of the Lisp programming language, allowing users to tailor the editor to their specific needs.
TypeScript is a superset of JavaScript that adds static typing to the language. It helps catch errors early in the development process and provides better tooling support. TypeScript code is transpiled to JavaScript, which can then be run in any JavaScript environment.
The Language Server Protocol is an open standard that enables communication between an IDE or editor and a language server. A language server is a program that provides language-specific features such as code completion, code navigation, and diagnostics. By using LSP, different editors can leverage the same language server, reducing the need for language-specific integrations in each editor.
Emacs TypeScript LSP refers to the integration of the TypeScript language server with Emacs using the LSP protocol. This integration allows Emacs users to enjoy features such as code completion, code navigation, and diagnostics when working with TypeScript code.
To use Emacs TypeScript LSP, you need to install the following components:
npm install -g typescript-language-server typescript
use-package
to manage your Emacs packages. Here is an example configuration:(use-package lsp-mode
:ensure t
:init
(setq lsp-keymap-prefix "C-c l")
:hook (typescript-mode . lsp)
:commands lsp)
(use-package lsp-ui
:ensure t
:commands lsp-ui-mode)
(use-package company-lsp
:ensure t
:commands company-lsp)
Once you have installed the necessary packages, you need to configure Emacs to use the TypeScript language server. Add the following configuration to your Emacs configuration file (usually ~/.emacs.d/init.el
):
(require 'lsp-mode)
(add-hook 'typescript-mode-hook #'lsp)
C-x C-f
command.M-x lsp
command.M-/
to trigger code completion. Emacs will show a list of suggestions based on the context.M-.
to jump to its definition. You can use M-,
to jump back to the previous location.M-x lsp-find-references
to find all references to that symbol in the project.M-/
to trigger code completion. You can also set up automatic completion by adding the following configuration to your Emacs configuration file:(setq company-idle-delay 0.2)
RET
to select a suggestion.M-x lsp-ui-flycheck-list
command to view a list of all errors and warnings in the project.Emacs allows you to customize keybindings to suit your preferences. For example, you can change the keybinding for jumping to definitions from M-.
to something else. Here is an example of how to customize the keybinding:
(define-key lsp-mode-map (kbd "C-c d") 'lsp-find-definition)
prettier
package globally using npm:npm install -g prettier
Then, add the following configuration to your Emacs configuration file:
(use-package prettier-js
:ensure t
:hook (typescript-mode . prettier-js-mode))
Emacs TypeScript LSP provides a powerful and efficient development environment for TypeScript developers. By leveraging the Language Server Protocol, Emacs users can enjoy features such as code completion, code navigation, and diagnostics. By following the usage methods, common practices, and best practices outlined in this blog, you can enhance your TypeScript development experience in Emacs.