This guide assumes you have installed the server by following instructions in the README.md if that is applicable to your client (i.e. if the client doesn't download the server itself).
The following filetypes are supported by the Terraform Language Server:
terraform
- standard*.tf
config filesterraform-vars
- variable files (*.tfvars
)terraform-stack
- standard*.tfstack.hcl
filesterraform-deploy
- standard*.tfdeploy.hcl
files
NOTE: Clients should be configured to follow the above language ID conventions
and do not send *.tf.json
, *.tfvars.json
nor Packer HCL config
nor any other HCL config files as the server is not equipped to handle these file types.
Most clients with a dedicated Terraform extension/plugin already have the default configuration, so you should not need to worry about it.
Instructions for popular IDEs are below and pull requests for updates or addition of more IDEs are welcomed.
See also settings to understand how you may configure the settings.
Most text editors allow you to open files to edit a single file, or a folder to edit many files at once. When opening a folder, this is commonly referred to as "workspace" or "root folder".
Opening folders is always preferred over individual files as it allows the language server to index the whole folder and keep track of changes more easily. We do however support "single-file mode" which provides limited IntelliSense.
Indexing enables IntelliSense related to module
blocks, such as
go-to-definition, completion of module.*
references, or workspace-wide
symbol lookup.
The server will not index any folders or files above the workspace root initially opened in the editor.
- Install the Terraform VS Code Extension
>=2.24.0
- The latest compatible version of terraform-ls is bundled with the extension
- See VS Code Configuration in case you need to tweak anything. Default settings should work for majority of users.
- Install the LSP package
- Install the LSP-terraform package
- Install the coc.nvim plugin
- Add the following snippet to the
coc-setting.json
file (editable via:CocConfig
in NeoVim)
{
"languageserver": {
"terraform": {
"command": "terraform-ls",
"args": ["serve"],
"filetypes": ["terraform", "tf"],
"initializationOptions": {},
"settings": {}
}
}
}
Make sure to read through the example vim configuration of the plugin, especially key remapping, which is required for completion to work correctly:
" Use <c-space> to trigger completion.
inoremap <silent><expr> <c-space> coc#refresh()
- Install the following plugins:
- Add the following to your
.vimrc
:
if executable('terraform-ls')
au User lsp_setup call lsp#register_server({
\ 'name': 'terraform-ls',
\ 'cmd': {server_info->['terraform-ls', 'serve']},
\ 'whitelist': ['terraform'],
\ })
endif
- Install the following plugins:
- Add the following to your
.vimrc
:
" Remove this line if additional custom language servers are set elsewhere
let g:ycm_language_server = []
if executable('terraform-ls')
let g:ycm_language_server += [
\ {
\ 'name': 'terraform',
\ 'cmdline': [ 'terraform-ls', 'serve' ],
\ 'filetypes': [ 'terraform' ],
\ 'project_root_files': [ '*.tf', '*.tfvars' ],
\ },
\ ]
endif
- Install the LanguageClient-neovim plugin
- Add the following to your
.vimrc
:
let g:LanguageClient_serverCommands = {
\ 'terraform': ['terraform-ls', 'serve'],
\ }
- Install the nvim-lspconfig plugin
- Add the following to your
.vimrc
orinit.vim
:
lua <<EOF
require'lspconfig'.terraformls.setup{}
EOF
autocmd BufWritePre *.tfvars lua vim.lsp.buf.formatting_sync()
autocmd BufWritePre *.tf lua vim.lsp.buf.formatting_sync()
- If you are using
init.lua
:
require'lspconfig'.terraformls.setup{}
vim.api.nvim_create_autocmd({"BufWritePre"}, {
pattern = {"*.tf", "*.tfvars"},
callback = vim.lsp.buf.formatting_sync(),
})
- Install the nvim-lspconfig plugin
- Add the following to your
.vimrc
orinit.vim
:
lua <<EOF
require'lspconfig'.terraformls.setup{}
EOF
autocmd BufWritePre *.tfvars lua vim.lsp.buf.format()
autocmd BufWritePre *.tf lua vim.lsp.buf.format()
- If you are using
init.lua
:
require'lspconfig'.terraformls.setup{}
vim.api.nvim_create_autocmd({"BufWritePre"}, {
pattern = {"*.tf", "*.tfvars"},
callback = function()
vim.lsp.buf.format()
end,
})
Make sure to read through to server_configurations.md#terraformls if you need more detailed settings.
If you are using use-package
, you can put this in the init.el
file to install lsp-mode
:
(use-package lsp-mode
:ensure t
:hook ((terraform-mode . lsp-deferred)))
There are various other ways to install lsp-mode
and they are
documented here.
The lsp-mode
language client for Terraform supports various features
like semantic tokens, code lens for references etc. There is more
detailed documentation here.
- Install LSP Support plugin
- Open Settings
- Go to
Languages & Frameworks → Language Server Protocol → Server Definitions
- Pick
Executable
- set
Extension
totf
- set
Path
toterraform-ls
- set
Args
toserve
- Pick
- Confirm by clicking
Apply
Please note that the Terraform plugin provides overlapping functionality (and more features at the time of writing). As a result having both enabled at the same time may result in suboptimal UX, such as duplicate completion candidates.
BBEdit 14 added support for the Language Server Protocol so you'll need to upgrade to version 14 to use; this won't work for older versions of BBEdit.
- Open Preferences > Languages
- In Language-specific settings section, add an entry for Terraform
- In the Server tab, Set Command to
terraform-ls
and Arguments toserve
- Once you've correctly installed
terraform-ls
and configured BBEdit, the status indicator on this settings panel will flip to green - If you'd like to pass any settings to the server you can do so via the Arguments field.
KDE Kate editor supports LSP and is user configurable.
- Install the
terraform-ls
package (or the equivalent package name appropriate to your distro) - Open Kate configuration (Settings Menu ->
Configure
Kate or Kate ->Preferences
on macOS) - Select LSP Client in the left pane
- Select User Server Settings tab
- Paste the following JSON and Save:
{
"servers": {
"terraform": {
"command": ["terraform-ls", "serve"],
"url": "https://github.com/hashicorp/terraform-ls",
"highlightingModeRegex": "^Terraform$",
"rootIndicationFileNames": ["*.tf", "*.tfvars"]
}
}
}
- Restart of the editor should not be necessary.