DEV Community

Cover image for Complete Guide: Setting up VS Code for Elixir and Phoenix Development
João Paulo Abreu
João Paulo Abreu

Posted on

Complete Guide: Setting up VS Code for Elixir and Phoenix Development

In this guide, we'll explore how to configure Visual Studio Code for Elixir and Phoenix development. Having a well-configured development environment is crucial for productivity, and VS Code offers excellent support for Elixir through its extensions and customizable settings.

This article is aimed at Elixir developers who want to set up their VS Code environment or improve their current setup. We'll cover essential extensions and optimal configurations for both Elixir and Phoenix development.

Required Extensions

First, let's install the necessary extensions for Elixir and Phoenix development. Open your terminal and run the following commands:

code --install-extension jakebecker.elixir-ls
code --install-extension phoenixframework.phoenix
code --install-extension pantajoe.vscode-elixir-credo
Enter fullscreen mode Exit fullscreen mode

Let's understand what each extension provides:

ElixirLS: Elixir Language Server

  • Provides intelligent code completion
  • Inline documentation
  • Go to definition functionality
  • Real-time diagnostics
  • Format on save capability

Phoenix Framework

  • Adds support for Phoenix-specific features
  • HEEx template syntax highlighting
  • Phoenix snippets and helpers

Elixir Credo

  • Integrates Credo for code analysis
  • Helps maintain code quality
  • Provides real-time feedback on code style

VS Code Settings

Now, let's configure VS Code with optimal settings for Elixir and Phoenix development. Open your settings.json file (Press Ctrl+Shift+P or Cmd+Shift+P and type "Open Settings (JSON)") and add these configurations:

{
  "[elixir]": {
    "editor.defaultFormatter": "JakeBecker.elixir-ls"
  },
  "[phoenix-heex]": {
    "editor.defaultFormatter": "JakeBecker.elixir-ls"
  },
  "editor.formatOnSave": true,
  "editor.tabSize": 2,
  "emmet.includeLanguages": {
    "elixir": "html",
    "phoenix-heex": "html",
    "html-eex": "html"
  },
  "files.associations": {
    "*.heex": "phoenix-heex"
  },
  "elixirLS.suggestSpecs": false,
  "elixirLS.mixEnv": "dev",
  "elixirLS.fetchDeps": false,
  "elixirLS.dialyzerEnabled": true,
  "elixir.credo.configurationFile": ".credo.exs",
  "elixir.credo.credoConfiguration": "default",
  "elixir.credo.strictMode": true,
  "elixir.credo.lintEverything": true,
  "elixir.credo.enableDebug": true
}
Enter fullscreen mode Exit fullscreen mode

Let's break down these settings:

Formatter Settings

  • Sets ElixirLS as the default formatter for Elixir and Phoenix HEEx files
  • Enables format on save
  • Uses 2 spaces for indentation (Elixir community standard)

Template Settings

  • Configures proper file associations for HEEx templates
  • Enables Emmet support for HTML in Elixir files

ElixirLS Settings

  • Disables automatic spec suggestions
  • Sets development environment
  • Enables Dialyzer for static analysis
  • Configures dependency handling

Credo Settings

  • Sets up Credo configuration file path
  • Enables strict mode for better code quality
  • Configures real-time linting

Project-Specific Configuration

For each Elixir/Phoenix project, it's recommended to create a .formatter.exs file in your project root:

[
  import_deps: [:ecto, :phoenix],
  inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
  subdirectories: ["priv/*/migrations"],
  plugins: [Phoenix.LiveView.HTMLFormatter]
]
Enter fullscreen mode Exit fullscreen mode

This ensures consistent formatting across your project and team members.

Verifying Your Setup

To verify your setup is working correctly:

  1. Open a Phoenix project in VS Code
  2. Create a new Elixir file (e.g., test.ex)
  3. Type some Elixir code and verify that you get:
    • Syntax highlighting
    • Code completion suggestions
    • Inline documentation on hover
    • Error diagnostics

Example test:

defmodule Test do
  def hello(name) do
    "Hello, #{name}!"
  end
end
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues

If you encounter any issues:

  1. ElixirLS not starting:

    • Ensure Elixir and Erlang are properly installed
    • Try reloading VS Code (Ctrl+Shift+P -> "Developer: Reload Window")
  2. Formatting not working:

    • Check if mix format works from the terminal
    • Verify your .formatter.exs file is properly configured
  3. Credo not working:

    • Ensure Credo is added to your project's dependencies
    • Run mix deps.get to install dependencies

Next Steps

Now that your VS Code is configured for Elixir and Phoenix development, you might want to:

  • Configure keyboard shortcuts for common Elixir tasks
  • Set up debugging configurations
  • Explore additional VS Code extensions for improved productivity

Conclusion

A well-configured editor is essential for productive development. With these settings and extensions, you'll have a powerful environment for Elixir and Phoenix development in VS Code.

References

Top comments (0)