How I run code in Helix editor?
2023-07-14
Categories: Development Environment
In my previous post, I mentioned that I’ve made the switch to WezTerm and Helix, and today I want to share how I run my code in this setup.
Previously, I would split Wezterm into two panes and run the code in a new pane.
However, I found it cumbersome to type different commands for each programming language (Go, C, Racket, and so on).
While auto-completion helped to some extent, or using control-p to run the previous command, it still took up a little time.
That’s when I started exploring the possibility of running my code directly from Helix based on the file extension.
Unfortunately, Helix didn’t have a variable for the current filename, I have to compile a specific PR to use %val{filename}.
To streamline the process, I created a script that executes different commands based on the file extension:
#!/bin/sh
pane_id=$(wezterm cli get-pane-direction down)
if [ -z "${pane_id}"]; then
pane_id=$(wezterm cli split-pane)
fi
filename="$1"
basedir=$(dirname "$filename")
basename=$(basename "$filename")
basename_without_extension="${basename%.*}"
extension="${filename##*.}"
case "$extension" in
"c")
run_command="clang -Wall -g -O3 $filename -o $basedir/$basename_without_extension && $basedir/$basename_without_extension"
;;
"go")
run_command="go run $filename"
;;
"rkt")
run_command="racket $filename"
;;
esac
echo "${run_command}" | wezterm cli send-text --pane-id $pane_id --no-paste
Then I made the following addition to the Helix’s config file:
[keys.normal.";"]
r = ":sh run.sh %val{filename}"
With this setup, you can now simply press ;, followed by r to run your code directly from Helix.
Additionally, I enabled auto-save by adding the following lines to ~/.config/helix/config.toml
[editor]
auto-save=true
Related Posts:
- Turning Helix into an IDE with the help of WezTerm and CLI tools
- Debug Rust in Helix using lldb-vscode: display the contents of local string variables
- How to debug Rust in Helix?
- WezTerm: quickly select a command and open it in a new pane
- File tree workaround for Helix
Quan Tong