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

helix-term.md (3052B)


      1 ---
      2 author: "JayVii"
      3 title: "REPL Support for Helix"
      4 date: "2024-10-16"
      5 summary: "An easy process to send code from inside the Helix editor to console"
      6 tags: ["development", "helix", "tech"]
      7 ---
      8 
      9 ## Motivation
     10 
     11 For almost a year now, I am an avid user of the "post-modern text editor"
     12 [Helix](https://helix-editor.com/), which replaced [NeoVim](https://neovim.io/)
     13 for me entirely. In Helix I write all kinds of configs, texts and code,
     14 primarily in [R](https://cran.r-project.org/). Helix works flawlessly with it,
     15 especially with the built-in LSP. The only gripe I had with this was, that there
     16 was no easy way to write R-code in Helix and send it directly and interactively
     17 to R's REPL, as you would in typical R IDEs like
     18 [RStudio](https://posit.co/products/open-source/rstudio/).
     19 
     20 So earlier this year, I tried to workaround that issue by experimenting with
     21 [TMUX](https://github.com/tmux/tmux/wiki) and its `send-keys` feature. The
     22 results of this are available as installable script(s)
     23 [in my git-repository](https://src.jayvii.de/pub/helix-term/).
     24 
     25 ## Functionality
     26 
     27 The idea behind it is quite simple: A TMUX session with a pre-defined name is
     28 spawned that runs the REPL we want to send our code to. This could be R, Python,
     29 Bash, ... Next, Helix sends code in its selection to some process
     30 [via its `pipe-to` command](https://docs.helix-editor.com/commands.html). This
     31 process' job is to send the highlighted text to tmux line-by-line.
     32 
     33 In its simplest form, the REPL in tmux could be started as:
     34 
     35 ```bash
     36 tmux new-session -s "my_repl" /usr/bin/R
     37 ```
     38 
     39 The process that send text to this tmux session, let's call it `send-to-tmux.sh`
     40 could be as easy as:
     41 ```bash
     42 #!/usr/bin/env bash
     43 
     44 # read piped input
     45 readarray -t lines
     46 
     47 # send input to tmux line by line
     48 for line in "${lines[@]}"; do
     49   tmux send-keys -t "my_repl" "$line" "Enter"
     50 done
     51 ```
     52 
     53 We could set up a keyboard shortcut for this in Helix in selection mode (hitting
     54 Ctrl-C twice):
     55 
     56 ```toml
     57 [keys.select]
     58 C-c = { C-c = [":pipe-to send-to-tmux.sh"] }
     59 ```
     60 
     61 ## Using helix-term
     62 
     63 The final script(s) in my
     64 [git-repository](https://src.jayvii.de/pub/helix-term/) are slightly more
     65 complicated to allow a more streamlined workflow, such as sending the current
     66 code-paragraph in insert or normal mode from Helix and setting launching a
     67 separate tmux-session for each working-directory, so you can run multiple such
     68 processes at the same time.
     69 
     70 In short, you can use my `helix-term` setup by running `helix-term /usr/bin/R`
     71 in the same working directory as the Helix process and hitting `Ctrl-C` twice
     72 for the code chunk you would like to send to the `R`-process.
     73 
     74 ## Noteworthy alternatives
     75 
     76 In a conversation
     77 [with another Helix user](https://social.jayvii.de/objects/VHQJgjNndkY/thread),
     78 we found that we might have different use-cases. This user came up with another,
     79 yet similar approach that tries to turn Helix a little bit more into an IDE:
     80 [hide](https://codeberg.org/landesfeind/hide). If `helix-editor` is too bare
     81 bones for your use-case, maybe `hide` is a better approach for you.