My problem is rather simple : I use a macro to wrap my current visual selection with some text (\verb; and ;, in fact). The current macro I use is :s#\%V.*\%V#\\verb;\0;. It works, but suffers from one problem : subsitutions always jump at the beginning of the line the cursor is on.

I would like to keep my cursor where it is to continue editing right where I did the subsitution or, alternatively, jump at the beginning/end of the substituted text. Is such a thing possible ? I've had a quick look at the substitute help, but there doesn't seem to be such a flag. I am not sure where to look for a global toggle of this behavior, either.

If possible, I would like to be able to keep this behavior for this macro only, however it is not too important and having a global toggle for this behavior would work, too.

Many thanks for your help !

  • I just realized I can probably do it another way, without using a substitute. But I think the question is still interesting, if only to share the solution I found : `>^[a;^[gv`<^[i\verb;^[ does the trick but needs some refinement (^[ means escape). May 21, 2019 at 9:25

2 Answers 2


There's a much simpler solution to your specific problem of surrounding the current selection with \view; and ; delimiters. Type:


  • c — change the visual selection: this deletes the text and enters insert mode. Crucially, it also stores the deleted text in the unnamed register,
  • \view;;Esc — enter your delimiters and exit insert mode,
  • P — paste the previously deleted text just before the ending ; delimiter.
  • Yeah I know I can do that, the whole point being that I don't want to type \verb; then ; everytime I want to surround a word because it's annoying. I guess I can add it to a macro though. May 21, 2019 at 13:23
  • @FlatBartender You have to type \verb; ; in the :substitute command in your question. This answer tells you how to make the change without the cursor moving to the start of the line, and is simpler than the normal mode commands you mentioned in a comment.
    – Rich
    May 21, 2019 at 13:48
  • Yes that was what I was trying to say ^^ Thanks for the answer, I'll mark it as correct. May 21, 2019 at 14:02

Custom operator would be a much better choice than a map, as you can use it with motion, it's very flexible.

Lots of people use surround operator provided by tpope/vim-surround, it's bound to ys in normal mode and S in visual mode. If you want to surround something with asymmetric text, you can customize it like this:

let g:surround_{char2nr('v')} = "\\verb;\r;"

\r is used to separate left and right text. change 'v' to whatever key you like. ys{motion}v will surround target with \vert; and ;. Your cursor will be placed at 1st character of left text.

  • :h curly-braces-names

Your Answer

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

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