How to setup the Helix editor for Rust
2024-11-11
I am new to helix and Rust, but I just came across a low quality PAID Medium article “teaching” how to setup rust-analyzer for the helix editor. I am sharing the info here because yes.
Install helix
Just use a package manager. In my case, macos:
brew install helix
Configure rust-analyzer
Assuming you have a working rust installation, use rustup to install the language server:
rustup component add rust-analyzer
And this will provide us with the binary we want.
Create a languages.toml in Helix config
Now, we need to explicity configure the editor to use rust-analyzer when in a Rust project. Create a ~/.config/helix/languages.toml
file and add this to it:
[[language]]
name = "rust"
auto-format = true
roots = [
"Cargo.toml",
"Cargo.lock"
]
[language.auto-pairs]
'(' = ')'
'{' = '}'
'[' = ']'
'"' = '"'
'`' = '`'
[language-server.rust-analyzer]
command = "rust-analyzer"
[language-server.rust-analyzer.config]
inlayHints.bindingModeHints.enable = false
inlayHints.closingBraceHints.minLines = 10
inlayHints.closureReturnTypeHints.enable = "with_block"
inlayHints.discriminantHints.enable = "fieldless"
inlayHints.lifetimeElisionHints.enable = "skip_trivial"
inlayHints.typeHints.hideClosureInitialization = false
Restart helix and test if it works!
By now, the syntax highlighting and code suggestions should be working!
Extra configuration
Apart from the language support, you can also configure Helix’s theme, behavior and keybinds For example you can create a ~/.config/helix/config.toml:
theme = "onedark"
[editor]
line-number = "relative"
mouse = false
auto-completion=true
auto-save=true
auto-format=true
text-width=80
gutters = ["diff", "diagnostics", "line-numbers", "spacer"]
soft-wrap.enable = true
soft-wrap.max-indent-retain=80
[editor.file-picker]
hidden = false
[editor.statusline]
left = ["mode", "spinner", "file-modification-indicator", "read-only-indicator"]
center = ["file-name"]
right = ["diagnostics", "register", "selections", "position", "file-encoding", "file-line-ending", "file-type"]
separator = "│"
mode.normal = "LOCKED"
mode.insert = "WORKING"
mode.select = "VISUAL SEL"
[editor.lsp]
enable=true
auto-signature-help=true
display-messages = true
[editor.indent-guides]
render = true
character = "┊"
skip-levels = 1
# At most one section each of 'keys.normal', 'keys.insert' and 'keys.select'
[keys.normal]
C-s = ":w" # Maps Ctrl-s to the typable command :w which is an alias for :write (save file)
C-o = ":open ~/.config/helix/config.toml" # Maps Ctrl-o to opening of the helix config file
a = "move_char_left" # Maps the 'a' key to the move_char_left command
w = "move_line_up" # Maps the 'w' key move_line_up
"C-S-esc" = "extend_line" # Maps Ctrl-Shift-Escape to extend_line
g = { a = "code_action" } # Maps `ga` to show possible code actions
"ret" = ["open_below", "normal_mode"] # Maps the enter key to open_below then re-enter normal mode
[keys.insert]
"A-x" = "normal_mode" # Maps Alt-X to enter normal mode
j = { k = "normal_mode" } # Maps `jk` to exit insert mode
Additional Resources
Some extra links here for more details on how to configure helix
- Helix Keymap: https://docs.helix-editor.com/keymap.html
- Helix commands: https://docs.helix-editor.com/commands.html
- Helix Language support: https://docs.helix-editor.com/lang-support.html
- Configuration: https://docs.helix-editor.com/configuration.html
- Language configuration: https://docs.helix-editor.com/languages.html