pub / www.jayvii.de

My personal website
git clone https://https://src.jayvii.de/pub/www.jayvii.de.git
Home | Log | Files | Exports | Refs | Submodules | RSS

commit 38f7e1bbc00a8db862d9a2323dfe5b0b2b6103ae
parent c35842c6a51a82bd94724a20d025a5614344f217
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Wed, 16 Oct 2024 17:18:24 +0200

feat: add post about helix-term

Diffstat:
Acontent/posts/helix-term.md | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 81 insertions(+), 0 deletions(-)

diff --git a/content/posts/helix-term.md b/content/posts/helix-term.md @@ -0,0 +1,81 @@ +--- +author: "JayVii" +title: "REPL Support for Helix" +date: "2024-10-16" +summary: "An easy process to send code from inside the Helix editor to console" +tags: ["development", "helix", "tech"] +--- + +## Motivation + +For almost a year now, I am an avid user of the "post-modern text editor" +[Helix](https://helix-editor.com/), which replaced [NeoVim](https://neovim.io/) +for me entirely. In helix I write all kinds of configs, texts and code, +primarily in [R](https://cran.r-project.org/). Helix works flawlessly with it, +especially with the built-in LSP. The only gripe I had with this was, that there +was no easy way to write R-code in helix and send it directly and interactively +to R's REPL, as you would in typical R IDEs like +[RStudio](https://posit.co/products/open-source/rstudio/). + +So earlier this year, I tried to workaround that issue by experimenting with +[TMUX](https://github.com/tmux/tmux/wiki) and its `send-keys` feature. The +results of this are available as installable script(s) +[in my git-repository](https://src.jayvii.de/pub/helix-term/). + +## Functionality + +The idea behind it is quite simple: A TMUX session with a pre-defined name is +spawned that runs the REPL we want to send our code to. This could be R, Python, +Bash, ... Next, Helix sends code in its selection to some process +[via its `pipe-to` command](https://docs.helix-editor.com/commands.html). This +process' job is to send the highlighted text to tmux line-by-line. + +In its simplest form, the REPL in tmux could be started as: + +```bash +tmux new-session -s "my_repl" /usr/bin/R +``` + +The process that send text to this tmux session, let's call it `send-to-tmux.sh` +could be as easy as: +```bash +#!/usr/bin/env bash + +# read piped input +readarray -t lines + +# send input to tmux line by line +for line in "${lines[@]}"; do + tmux send-keys -t "my_repl" "$line" "Enter" +done +``` + +We could set up a keyboard shortcut for this in Helix in selection mode (hitting +Ctrl-C twice): + +```toml +[keys.select] +C-c = { C-c = [":pipe-to ~/.local/bin/send-to-tmux"] } +``` + +## Using helix-term + +The final script(s) in my +[git-repository](https://src.jayvii.de/pub/helix-term/) are slightly more +complicated to allow a more streamlined workflow, such as sending the current +code-paragraph in insert or normal mode from helix and setting launching a +separate tmux-session for each working-directory, so you can run multiple such +processes at the same time. + +In short, you can use my `helix-term` setup by running `helix-term /usr/bin/R` +in the same working directory as the Helix process and hitting `Ctrl-C` twice +for the code chunk you would like to send to the `R`-process. + +## Noteworthy alternatives + +In a conversation +[with another Helix user](https://social.jayvii.de/objects/VHQJgjNndkY/thread), +we found that we might have different use-cases. This user came up with another, +yet similar approach that tries to turn Helix a little bit more into an IDE: +[hide](https://codeberg.org/landesfeind/hide). If `helix-editor` is too bare +bones for your use-case, maybe `hide` is a better approach for you.