Vim - Search and replace the results

后端 未结 4 722
刺人心
刺人心 2021-02-13 05:13

I\'m getting more and more comfortable with Vim after a few months. BUT, there is only one simple feature I can\'t get any answer from the web. That is \"Search and replace the

相关标签:
4条回答
  • 2021-02-13 05:56

    Well, since #keyword# and #new_keyword# account for most of the characters, and you need some way to differentiate between them (i.e., a character in vim, or tab between entry fields in dialog in a different editor), you're left with maybe four or five keystrokes beyond that.

    So I think you're probably overestimating number of keystrokes and also forgetting that (1) it becomes very natural, and (2) working this way allows you also to naturally modify the action performed by specifying a different range or option flag.

    But you can cut down on keystrokes. If you want you can map a key to automatically bring up the command line with '%s/' already in place. e.g.:

    nmap s :%s/
    

    The command above would remap 's' (I'm not recommending remapping to that key, but it gives the idea) and set you up to insert the keyword.

    Also, you can set the 'gdefault' option to default to substituting multiple times per line. This lets you skip the ending '/g' in your keystrokes:

    set gdefault
    

    See ':h gdefault' for help section on that option.

    In the end I would say just get used to the default way it works, because using it that way allows you to keep same basic operation when you want to specify different ranges or option flags, and creating a new special map is just another thing to remember. gdefault may be worth setting if you think you're going to want it majority of time, adding /g flag at end when gdefault is set has effect of turning /g off. . .

    0 讨论(0)
  • 2021-02-13 05:59

    Move to the first highlighted word then record a macro for replacing the word and moving to the next one, e.g:

    gg
    n
    qq
    caw new_word^[
    n
    q
    @q
    @@
    @@
    ...
    
    0 讨论(0)
  • 2021-02-13 06:02

    If you've already done a search you can do a substitution for the same pattern by simply leaving out the pattern in the substitute command. eg:

    /keyword
    

    searchs for "keyword", and then:

    :%s//new_keyword/g
    

    will replace all occurrences of "keyword" with "new_keyword".

    0 讨论(0)
  • 2021-02-13 06:12

    Searching and using the dot command (you didn't meantion you are using the dot command, that's why I highlight it) to repeat the last input action is my best bet here.

    I use s///g for search and replace.

    0 讨论(0)
提交回复
热议问题