I have many python code examples in my markdown files, which should be prepended with >>> symbols.

So I write a vim function to do this job:

function AddShellPrompt() range
    let lnum = a:firstline
    while lnum <= a:lastline
        let replaced_line = substitute(getline(lnum), '^\s\+', '&>>>', '')
        call setline(lnum, replaced_line)
        let lnum += 1

The problem is, for raw text like:

pow(2, 3)

I need only add >>> before pow(2, 3), but not before 8. So I need to expand my regex.
It comes to me that if I only add >>> for a line which contains any character of +-*/%(=.
I can get a pretty good result(though not perfect).

But the problem is, how to write regex (especially in vim) which expresses Some of the characters in the given set has appeared in the current line?

  • 1
    Good question! For vi and vim, you can't also ask at the dedicated site vi.stackexchange.com . – cxw Aug 7 '15 at 10:11
  • @cxw, if you think it's a good question, vote for me, please. And I'm happy to see there is a dedicated vim site on stackoverflow! – Zen Aug 7 '15 at 10:18

vim regex engine support Bracket Expression, you can defined a class of characters as a sequence of characters enclosed by square brackets [...]:


will match any character from those characters (You need to escape -, which defined a range between characters, to match it literally)

To check current line contains any in set of characters:

if getline(".") =~ '[+\-*/%(=]'
  echo "Match"
  • Yeah, I know, this is a meta class, but how to express: In the following arbitrary characters, one of the chars in the meta class has appeared? For example, how to write a regex in vim, which can detect if a line contains any +, % or =? – Zen Aug 7 '15 at 10:08
  • @Zen: I'm not sure what do you mean. if getline(".") =~? '[+%=]' will match if line contains any +, % or =. – cuonglm Aug 7 '15 at 10:37
  • I got you know. This if getline(".") =~? '[+%=]' is a more complete expression. Especially the =~? expression, I didn't know it before. Is it able a even add a word into such kind of meta class? I mean, can I detect if the word import exists in the line within the [+%=] expression? Or must I write if getline(".") =~? '[+%=]' and getline(".") =~? "import". If I had to add a whole and block. Wouldn't the work detect if one of the multiple words in a line a tedious job? Or should I write a for loop, put all key words in a list. And check if they are in the line one by one? – Zen Aug 7 '15 at 10:43
  • [...] is set of characters, not word. Using if getline(".") =~? '[+%=], you only know if line contains any of those characters, you don't know if all matched or how many characters matched. – cuonglm Aug 7 '15 at 10:50
  • I know how meta class works. Anyway, your answer is a solution to my original question. Could you please update if getline(".") =~? '[+%=]' to your answer so that I can accept your answer? That's an important update, I think. – Zen Aug 7 '15 at 12:22

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.