1

[Edit: after reading comments I realize that the Surround plugin is adequate for my needs after all, so I'll leave this question for purely academic purposes to gain a better understanding of vimscript's inner workings]

I'd like to make adding/deleting tags, quotes, braces, and other symmetrical text structures easier to do in Vim, and I find the surround.vim plugin a little too quirky and specialized for my needs.

What I really need is more generally a "mirrored" input mode and "mirrored" deletion mode, whereby I could visually select a block of text, then type onto or delete from both ends of the selection at once. As an example workflow, I'd like to:

  1. select the word hello
  2. hit some keystroke combo to enter "mirror mode"
  3. type "
  4. my text now says "hello"

In this example I only typed one character at each end, but it's important that in step three I could have typed many characters, not just one, for instance I should be able to type <b> to produce <b>hello<b> (I still would need to manually add the / in the closing tag, which I'm OK doing).

So is this even possible in Vim? Could someone provide a broad outline of functions that would be involved in the solution? Specifically, I don't know how to intercept text as it's being inserted and then alter the location where it appears so that it's tacked onto the beginning and ending of the selection block instead of the cursor location. And ditto for deletion.

1 Answer 1

2

Well, the behavior you describe is exactly what Surround does:

  1. select the word hello
  2. hit S
  3. type "
  4. my text now says "hello"

The difference with what you ask is the "live updating" or "live mirroring" which I have no idea how to do. You could probably take a look at SnipMate or UltiSnips for that part.

Sign up to request clarification or add additional context in comments.

3 Comments

Surround is great for that specific case. The problem is when I want more than one character, like a tag, then it's a new keystroke combo. And the commands are inconsistent depending what I want to do, such as surrounding an arbitrary selection not just a word.
No, it's exactly the same: 1. select hello with viw, 2. S, 3. <p>, 4. get <p>hello</p>. Doing the same without visual selection: ysiw<p>.
Hmm you're right. And for deletion/change I just do dst/cst for tags or ds"/cs" eg for quotes. OK so I guess Surround is adequate after all, it just needs better documentation! Thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.