0

I have following yas-snippet for inserting latex equation parenthesis in selected region.

# -*- mode: snippet -*-
# name: parenthesis_region_()
# binding: M-r ( )
# --
\left(
    `yas-selected-text` 
\right)
$0

I use evil-mode.

  1. When I mark a region with v (evil-visual-char), and run above snippet with M-r (). Cursor ends up on ) character (in \right)). enter image description here

  2. When I mark a region with V (evil-visual-line), and run above snippet with M-r (). Cursor ends up on \ character (in \left().

enter image description here

In both cases, cursor doesn't end up next line of \right). The case 1 is acceptable for me (I can live with that). But the case 2 is vastly different and inconvenient.

Question:

  • Why these 2 have different outcomes?
  • How can I make them behave same?

1 Answer 1

1

Try this:

# -*- mode: snippet -*-
# name: parenthesis_region_()
# binding: M-r ( )
# --
\left(
    `yas-selected-text`
\right)
`(deactivate-mark)`$0

UPDATE:

I've noticed that V inserts a new line in the result, to remove it, you can use this for both.

# -*- mode: snippet -*-
# name: parenthesis_region_()
# binding: M-r ( )
# --
\left(
    `(replace-regexp-in-string "\n$" "" yas-selected-text)`
\right)
`(deactivate-mark)`$0

Mind you, you will lose the gv command to re-select the last selected region, but I suppose you wouldn't need it anyway.

To answer the whys: I think for V, the cursor jumps back to the beginning of the last selected region point, hence you can deactivate the mark. Not sure for v, but it would have worked if you just put a space before $0. It might be because the cursor steps back as you come out of the insert state. See evil-move-cursor-back, although, it might be down to the mark as well, or something else.

4
  • Thanks !!!, I didn't even know existence of gv command before. It's good learn Vim commands from Emacs site !.
    – Garid
    Mar 11, 2023 at 9:33
  • Please see the update above.
    – Arktik
    Mar 11, 2023 at 9:35
  • 1
    Also, have a look at cdlatex package. You might be re-inventing the wheel here.
    – Arktik
    Mar 11, 2023 at 9:36
  • 1
    Also, I am sure there are plenty of resources to learn Vim commands, but for me the "Practical Vim" by Drew Neil was the turning point.
    – Arktik
    Mar 11, 2023 at 9:39

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.