0

I'm using neovim for working with Elixir, with the neoterm plugin which gives me REPL functionality.

A well known challenge is that the fact that the terminal evaluates and executes code line by line, even in circumstances where you actually want newlines. See this post for more details.

My question is if it's possible to create a keybinding for a command that takes a visual mode selection as input, and temporarily modify the selection prior to executing the command.

Eg. assume I select all of the following text, and run neoterm's :TREPLSendSelection command:

[1, [2], 3]
|> List.flatten()

I want to enclose the selection in parentheses, so that text passed to TREPLSendSelection becomes:

(
[1, [2], 3]
|> List.flatten()
)

However, I don't want the selected text to get modified in my text file, the modification should just happen under the hood. Is this possible? Can anyone show me how to do this for neovim in a lua config file?

1

1 Answer 1

1

For vimscript:

xnoremap KEYS :call MultiLineSend()<CR>

function MultiLineSend() range
  " lastline first, so that we don't change where firstline is
  execute a:lastline "put =')'"
  execute a:firstline "put! ='('"
  " lines have changed, so adjust the offsets
  execute printf('%s,%s TREPLSendSelection', a:firstline, a:lastline+2)
  execute (a:lastline + 2) 'delete'
  execute a:firstline 'delete'
endfunction

This modifies the buffer, but undoes the change (I hope). If neoterm exposed an API for sending arbitrary text, this would be easier; we could grab the text with getline(), modify it, and send it over.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.