Fork me on GitHub
#vim
<
2022-05-18
>
emilaasa07:05:41

How would I go about making a keybinding that does a "def a let expression" action? GIF in thread: 🧵

emilaasa07:05:00

I often do something like this when iterating on a piece of code:

emilaasa07:05:58

I use nvim, conjure, lsp. How would I approach automating that action?

emilaasa07:05:53

A couple of ideas would be: • a normal vim macro • maybe there's some API in Conjure?

dominicm08:05:05

I'd just use a normal vim binding, bound to a function.

Leaf Garland12:05:14

if you've got vim-sexp which has various motions and text objects for s-exprs then something like this might work. Use it with the cursor on the name of the binding. It selects the first element moves to the end of the next, yanks them then pastes them into a command line using :ConjureEval. Clunky but works mostly. nmap <leader>cdl vie<m-e>y:ConjureEval (def <c-r>")<cr>

🔥 2
🙇 1
dominicm12:05:08

That's messing with the clipboard though, but yeah.

emilaasa16:05:28

Cool I'll test something like that!

emilaasa05:05:15

@U02EP7NKPAL That worked like a charm! Thank you! Now I feel a hacker in the movies

👍 1
Dumch19:05:47

Thanks, guys, didn't even think about it, and it's really handy

peterh23:05:38

@U02EP7NKPAL Very nice, I never knew I needed this! It also works with IcedEval for vim-iced.

emilaasa10:05:52

Follow up on this - does anyone know how to port this into nvim + conjure + fennel?

lemuel20:05:47

This is slightly different but works for me

(nvim.set_keymap :n :<leader>cdl "vi]y:ConjureEval (def <c-r>\")<cr>" {:noremap true})

🙏 2
emilaasa05:05:22

Oh god it works again! Thanks :D

emilaasa05:05:51

That funky binding has become integral to how I work, pretty funny!

emilaasa05:05:02

Does not work for multi line let tho

Leaf Garland05:05:31

What’s an example of a multi line let? Vim-sexp motions should work fine across lines

emilaasa05:05:09

(let [a 1 
      b 2])

emilaasa05:05:38

I can't get the M-e binding to work in my fnl config

emilaasa05:05:51

So this works on a let with one line, like (let [a 1]) (nvim.set_keymap :n :<leader>cdl "vi]y:ConjureEval (def <c-r>\")<cr>" {:noremap true})

emilaasa05:05:56

But if I run it on the two line one, it will go until the ending ] and then get an error like this: ; eval (command): (def a :b b :c) ; (err) Syntax error compiling def at (src/analysis/core.clj:1:1). ; (err) Too many arguments to def

emilaasa06:05:20

If I naively try and bind the same binding that worked great in vimscript, translated to this: (nvim.set_keymap :n :<leader>cdl "vie<m-e>y:ConjureEval (def <c-r>\")<cr>" {:noremap true})

emilaasa06:05:43

I end up in visual mode - so I don't know if it's able to interpret the <m-e> string correctly

emilaasa06:05:09

It still works correctly if I manually run the motions vie<m-e>y

lemuel07:05:23

I got the same issue, will have a play with it later

Leaf Garland07:05:43

That vi] will select everything in square brackets, so every binding in the let which won't work if there is more than one.

lemuel07:05:05

Looks like it works if you set noremap to false (not really sure what difference that makes!)

(nvim.set_keymap :n :<leader>cdl "vie<m-e>y:ConjureEval (def <c-r>\")<cr>" {:noremap false})

Leaf Garland08:05:50

Ah yes, of course. noremap means interpret the mapping as using default mappings (not any mapping that other plugins or you might have set). Usually that's what you want but in this case we do want to use a plugin's mappings so we need noremap=false. That's why the original mapping I posted used nmap, not nnoremap.

👍 4
😍 2
Leaf Garland08:05:43

btw - an easy way to use vim script mappings in lua is to use vim.cmd to run vimscript, eg. vim.cmd[[ nmap <leader>cdl vie<m-e>y:ConjureEval (def <c-r>")<cr> ]]

👍 2
emilaasa10:05:52

Follow up on this - does anyone know how to port this into nvim + conjure + fennel?