I would like to insert a specific character after each instance of another character in a line. For example, inserting a '\' after each instance of another '\' on a line.

So, from this: C:folder\folder2\folder3\folder4, to this:C:folder\\folder2\\folder3\\folder4

I was thinking it may be a variation on the s command, but I haven't found anything that works yet. Or maybe using search and replace is a better way.

  • 1
    I think you answered your own question, the s command does just fine: :s/\\/\\\\/g – guillem Jun 26 '18 at 6:00

Extending the comment of @guillem

you need a simple :s/\\/\\\\/g for each line that you want to perform the substitution on. Or maybe visually select the lines and then :'<,'>s/\\/\\\\/g

Here, you need to escape each \ with another \ before it.

Edit: I'm adding a link to vim substitution command. Moreover, vim's substitute command :s/<pattern>/<change>/ is the same as the search and replace command. Look at the wiki page for using vim's substitute command.

  • 1
    Why not :s-\-\\-g to avoid all the escapes? The delimiter is pretty flexible. – D. Ben Knoble Jun 26 '18 at 23:01
  • @D.BenKnoble Um, that's because I didn't know it. I'll edit my answer after I read about it a little, ok? – klaus Jun 27 '18 at 1:51
  • 1
    Fair enough! Just a suggestion – D. Ben Knoble Jun 27 '18 at 1:51
  • 1
    I saw the deal about substitute delimiters. And you, sir, made my day. – klaus Jun 27 '18 at 12:11
  • 2
    Vimgolf: :s/\\/&&/g – Christian Brabandt Jun 27 '18 at 15:02

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.