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?

A specific version of this question is:
How to detect if a line contains any +, -, *, /, %, (, =, or even a word import with a vim regex?

After the suggestion of the following answers, I got my improved function as below:

" Massively add python prompt symbol in a markdown file
function AddShellPrompt() range
    let lnum = a:firstline
    let match_pattern = '^\s\+.*[-\+_*/%=]\|.*import\|.*print\|.*def\|.*class\|.*while\|.*for\|.*if\|.*elif\|.*else\|.*pass\|.*continue\|.*break\|.*return'
    while lnum <= a:lastline
        let current_line = getline(lnum)
        if match(current_line, match_pattern) == 0
            let replaced_line = substitute(current_line, '^\s\+', '&>>>', '')
            call setline(lnum, replaced_line)
        let lnum += 1
  • 3
    Rather than use the function to do this, you could use :global with the pattern and the command norm I>>>. The whole command would be :g/[-+*/%()]\|import/norm I>>>. Aug 7, 2015 at 12:36
  • @SakariCajanus, That sounds good, but I need something that I can reuse easily.
    – Zen
    Aug 7, 2015 at 13:17
  • 1
    The first thought I had when reading your question was that all you need is a global substitution. Similar to Sakari's approach but without vim's specific norm, e.g.: :g/[-+*/%()]\|import/s/^/>>>/. (And yes, you can reuse that command as well, you don't need a complex function.)
    – Janis
    Aug 8, 2015 at 17:31

2 Answers 2


The question seems easier when you word it differently. If you are happy with the logic of Any of the characters -+*/%(=or the word import, the regex is simply


where [-+*/%()] is a set of characters that can match, \| stands for or, and import is the word itself.

Of course, if you want to use that with substitute you want to match the beginning of line of the line containing the pattern, e.g.


which adds the start-of-line, \ze to set the end of match directly after, then any characters followed by the match described before.


It should be possible by including the \| operator in your regex.

For example :

The text :

sit totam impedit+ .
adipisicing eos = nulla.
sit (veniam magnam.
elit %debitis porro!

And the search regex :


And the regex matches the 4 lines, though it's not some valid python statement, it answers you question.

Reference : https://stackoverflow.com/a/680031/2558252

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.