Vim incremental search during substitution

时光怂恿深爱的人放手 提交于 2019-12-30 03:16:09

问题


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:


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.




回答2:


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.)



来源:https://stackoverflow.com/questions/8329887/vim-incremental-search-during-substitution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!