10

I am terrible at writing vim regular expressions. Whenever I write one to do a substition, it never works the first time because I inevitably end up writing something Perl instead of vim. I fare a lot better when doing a simple search because I have incsearch turned on and I can see in real-time whether my pattern matches.

Is there a way I can have the s command act like / (performing an incremental search) while I am attempting to write a proper pattern?

1
  • 4
    Vim 8.1 has added support for incsearch on :s, :g, and :v via patch 8.1.0271
    – 1110101001
    Dec 31, 2018 at 0:48

2 Answers 2

20

I'm not sure but I think there is not a way to do it. By the way, I use a little trick to speed up my substitutions. If you do something like:

:%s//bar

on the command line Vim will use your latest search. So, it's not exactly what you need but still a way to increase a bit your speed doing substitutions.

3
  • 1
    This is good tip. You can just type your search out first, then run that shortcut
    – Matt
    Nov 30, 2011 at 17:15
  • 1
    Wow, that tip will probably save me many hours over the course of my career. Thanks!
    – notlesh
    Nov 30, 2011 at 19:22
  • I knew I could search again with /, but it never occured to me that it would carry over to substitute as well!
    – djs
    Nov 30, 2011 at 21:51
3

You could try this little trick to compose your search pattern using incsearch and then copy pattern into command line substitution:

  1. Compose pattern using normal mode /... You can see your patterns are matching. The last pattern will be stored in the @/ register.

  2. Go to command line mode and enter this partial line: :%s/

  3. Now press these keys: <c-r>=@/ This will copy last search pattern into the substitute command you're writing. ( <c-r> is pressing control-r key, not typing in the characters.)

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.