Configuring Emacs for Swift Development
Emacs is a highly customizable text editor that has its origins in amacro package for the Digital Equipment Corporation TECO editor.A number of Emacs-like editors and derivatives have existed over theyears, but this guide will focus onGNUEmacs.
This guide will walk you through setting up a new install of Emacs forSwift development; it does not assume any particular operating system,but where relevant may give system-specific hints to help you getstarted. It also is not intended to teach you to use Emacs; if youwant to learn Emacs, try this resource:EmacsTour.
We assume for the purposes of this guide that you have alreadyinstalled Swift; if not, pleasefollow the installation instructionsfor your platform on the Swift websitebefore continuing.
Installing Emacs
There are genericinstallation instructions on the GNU Emacswebsite. Specificinstructions are given below for some common platforms.
macOS
TheEmacs for Mac OS X website providesuniversal binaries in a standard Mac disk image. Downloading theimage from that site, opening it and dragging it to yourApplicationsfolder is the easiest way to install a native Emacs for macOSdevelopment.
Microsoft Windows
You can download GNU Emacs for Windows froma nearby GNUmirror or themain GNU FTPserver. The simplest thing islikely to run theemacs-<version>-installer.exe executable, whichwill install Emacs and set up some shortcuts for you.
Debian-based Linux (including Ubuntu)
From a prompt, enter
$sudoapt-getinstallemacsor if you are doing this on a server system or in a container whereyou have no plans to use the GUI,
$sudoapt-getinstallemacs-noxRedHat-based Linux (RHEL, Centos, Fedora)
From a prompt, enter
$sudodnfinstallemacs(On older builds you may need to useyum rather thandnf, but thelatter is the newer, better option if it’s available.)
Configuring Emacs
Emacs veterans probably remember downloading and installing Lisppackages manually, but these days it’s best to use a package manager.We’ll also configureMELPA, which is a popularpackage repository for Emacs. To do this, open Emacs, enterC-x C-f~/.emacs and hit enter. Then add the following to your.emacsfile:
;;; Add MELPA as a package source(require'package)(setqpackage-enable-at-startupnil)(add-to-list'package-archives'("melpa"."https://melpa.org/packages/"))(package-initialize)We’ll also set-upuse-package, since thatsimplifies package installation if you want to be able to just copyyour~/.emacs to a new machine and have things just work. To dothat, add
;;; Bootstrap `use-package'(unless(package-installed-p'use-package)(package-refresh-contents)(package-install'use-package))(eval-when-compile(require'use-package))In a moment we’ll want to be able to locatesourcekit-lsp; thisdepends on where Swift is installed, so let’s add a Lisp function todo that:
;;; Locate sourcekit-lsp(defunfind-sourcekit-lsp()(or(executable-find"sourcekit-lsp")(and(eqsystem-type'darwin)(string-trim(shell-command-to-string"xcrun -f sourcekit-lsp")))"/usr/local/swift/usr/bin/sourcekit-lsp"))Next, we’ll install some useful packages:
;;; Packages we want installed for Swift development;; .editorconfig file support(use-packageeditorconfig:ensuret:config(editorconfig-mode+1));; Swift editing support(use-packageswift-mode:ensuret:mode"\\.swift\\'":interpreter"swift");; Rainbow delimiters makes nested delimiters easier to understand(use-packagerainbow-delimiters:ensuret:hook((prog-mode.rainbow-delimiters-mode)));; Company mode (completion)(use-packagecompany:ensuret:config(global-company-mode+1));; Used to interface with swift-lsp.(use-packagelsp-mode:ensuret:commandslsp:hook((swift-mode.lsp)));; lsp-mode's UI modules(use-packagelsp-ui:ensuret);; sourcekit-lsp support(use-packagelsp-sourcekit:ensuret:afterlsp-mode:custom(lsp-sourcekit-executable(find-sourcekit-lsp)"Find sourcekit-lsp"))We’ll also add a couple of packages to make things look prettier andmore modern; you could install a theme or change the fonts as well ifyou wish - the possibilities for customizing Emacs are nearly endless:
;; Powerline(use-packagepowerline:ensuret:config(powerline-default-theme));; Spaceline(use-packagespaceline:ensuret:afterpowerline:config(spaceline-emacs-theme))Finally, let’s turn off the splash screen, since we don’t want to seethat whenever we start Emacs, and we’ll also disable the toolbar:
;;; Don't display the start screen(setqinhibit-startup-screent);;; Disable the toolbar(tool-bar-mode-1)EnterC-x C-s to save your new.emacs file, then restart Emacs.
Conclusion
We were able to get Emacs set-up to edit Swift code, with syntaxhighlighting and integration with SourceKit-LSP as well as support formodern best practices like the use of.editorconfig files to allowprojects to easily set their own preferences for tab widths andformatting.
Files
Here’s a complete.emacs file containing the full configuration:
;;; Add MELPA as a package source(require'package)(setqpackage-enable-at-startupnil)(add-to-list'package-archives'("melpa"."https://melpa.org/packages/"))(package-initialize);;; Bootstrap `use-package'(unless(package-installed-p'use-package)(package-refresh-contents)(package-install'use-package))(eval-when-compile(require'use-package));;; Locate sourcekit-lsp(defunfind-sourcekit-lsp()(or(executable-find"sourcekit-lsp")(and(eqsystem-type'darwin)(string-trim(shell-command-to-string"xcrun -f sourcekit-lsp")))"/usr/local/swift/usr/bin/sourcekit-lsp"));;; Packages we want installed for Swift development;; .editorconfig file support(use-packageeditorconfig:ensuret:config(editorconfig-mode+1));; Swift editing support(use-packageswift-mode:ensuret:mode"\\.swift\\'":interpreter"swift");; Rainbow delimiters makes nested delimiters easier to understand(use-packagerainbow-delimiters:ensuret:hook((prog-mode.rainbow-delimiters-mode)));; Company mode (completion)(use-packagecompany:ensuret:config(global-company-mode+1));; Used to interface with swift-lsp.(use-packagelsp-mode:ensuret:commandslsp:hook((swift-mode.lsp)));; lsp-mode's UI modules(use-packagelsp-ui:ensuret);; sourcekit-lsp support(use-packagelsp-sourcekit:ensuret:afterlsp-mode:custom(lsp-sourcekit-executable(find-sourcekit-lsp)"Find sourcekit-lsp"));; Powerline(use-packagepowerline:ensuret:config(powerline-default-theme));; Spaceline(use-packagespaceline:ensuret:afterpowerline:config(spaceline-emacs-theme));;; Don't display the start screen(setqinhibit-startup-screent);;; Disable the toolbar(tool-bar-mode-1)