2

I repeatedly need to transform

word

into

\edlemvar{word}{}{}

or

\edlemvar{}{}{word}

More generally, it would be great to have a way to do this even if there are more words on the line than "word," something like <count>COMMANDSEQUENCE where <count> is however many words I would like to include in either pair of braces; the rest of the words on the line would then appear on a new line. That is, I'd like to convert (for <count> = 2)

word1 word2 word3 word4

to

\edlemvar{word1 word2}{}{} 
word3 word 4

or

\edlemvar{}{}{word1 word2} 
word3 word 4

Question: Is there some clever way in Vim/Neovim to do either this general transformation with minimal keystrokes?

I use a custom snippet that turns edlv into \edlemvar{}{}{}. But then I still need to type Iedlv<tab><esc>$miwbp to do the first operation, or Iedlv<tab><esc>$miwhP for the second --- and that's just the simple case.

Note: I use cutlass/subversive/yoink. If you don't, please replace m with d.

4
  • Maybe something like <count>mawiedlv<tab><esc>p or <count>mawiedlv<tab><tab><tab><esc>p? This almost works, but often leaves extra white space within the braces. Apr 4 at 23:05
  • 1
    "I use a custom snippet" Care to share what snippet plugin you are using? AFAIK, most snippet engines support a visual placeholder, so that v3iw<tab>edlv<tab> results in \edlemvar{word1 word2}{}{}
    – husB
    Apr 6 at 14:09
  • I use ultisnips. I tried your sequence just now and it didn't work (I just got \edlemvar{}{}{}. Do I need to add something to the snippet to tell it where to put the portion selected in visual mode? Apr 6 at 21:14
  • 1
    Yes, you need to add $VISUAL in your snippet. Let me post an answer to include the details
    – husB
    Apr 7 at 6:20

1 Answer 1

2

As per the comments, Ultisnips supports the use of a $VISUAL placeholder to include the recent visual selection in a snippet.

Instead of using <count>{motion}, use {Visual} mode to select the text to be moved inside the braces.

In the snippet definition, tell Ultisnips where to place the visually selected text (if any) with $VISUAL.

That is, instead of

snippet edlv "\edlemvar{}{}{}" 
\edlemvar{${1:one}}{${2:two}}{${3:three}}
$0
endsnippet

we can define

snippet edlv1 "\edlemvar{.}{}{}" 
\edlemvar{${1:${VISUAL:one}}}{${2:two}}{${3:three}}
$0
endsnippet

Now, with the text

word1 word2 word3 word4

and cursor on word1, type v3iw<tab>edlv1<tab> to get

\edlemvar{word1 word2}{two}{three}
 word3 word4

Addendum: there is a small annoyance — a space in the start of the second line. I don't have a robust solution right now, but as a workaround, you could select the additional space at the end of word2, and use a transformation, ie.

snippet edlv1t "\edlemvar{.}{}{}" 
\edlemvar{${1:${VISUAL/\s+$//:one}}}{${2:two}}{${3:three}}
$0
endsnippet

and use v4iw<tab>edlv1t<tab> to get the desired result (without the leading space).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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