Perform a non-regex search/replace in vim

前端 未结 7 1707
暖寄归人
暖寄归人 2021-02-01 12:22

When doing search/replace in vim, I almost never need to use regex, so it\'s a pain to constantly be escaping everything, Is there a way to make it default to not using regex or

相关标签:
7条回答
  • 2021-02-01 12:35

    Use this option:

    set nomagic
    

    See :help /magic

    0 讨论(0)
  • 2021-02-01 12:38

    If you want to search literally, you can use the \V regex atom. This almost does what you want, except that you also need to escape the backslash. You could define your own search command, that would search literally. Something like this:

     :com! -nargs=1 Search :let @/='\V'.escape(<q-args>, '\/')| normal! n
    

    And then use :Search /foobar/baz

    For Substitute, you could then after a :Search command simply use

    :%s//replace/g
    

    since then Vim would implicitly pick up the last search item and use the for replacing.

    (Just want to give you some ideas)

    0 讨论(0)
  • 2021-02-01 12:42

    For the :s command there is a shortcut to disable or force magic. To turn off magic use :sno like:

    :sno/search_string/replace_string/g
    

    Found here: http://vim.wikia.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic

    0 讨论(0)
  • 2021-02-01 12:48

    Try the Edit Find and replace on the menu bar.

    0 讨论(0)
  • 2021-02-01 12:50

    Have you enabled magic?

    :set magic
    
    0 讨论(0)
  • 2021-02-01 12:55

    Here’s how to disable regular expression search/replace only in command mode:

    autocmd CmdWinEnter * set nomagic
    autocmd CmdWinLeave * set magic
    

    All plugins that depends on regular expression such as white-space remover should works as usual.

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