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 is there an alternative command to accomplish this?
在vim中进行搜索/替换时,我几乎不需要使用regex,所以不断地转义所有内容是很痛苦的,是否有一种方法可以使它默认不使用regex,或者是否有其他命令来实现这一点?
As an example, if I want to replace <
with <
, I'd like to just be able to type s/</</g
instead of s/\</\<\;/g
作为一个例子,如果我想要替换< with <我想要能够输入s/
6 个解决方案
#1
13
The problem is primarily caused by confusion about the role of the &
in the replacement string. The replacement string is not a reg-ex, although it has some special characters, like &
. You can read about role of &
in replacement string here: :h sub-replace-special
.
问题主要是由于对替换字符串中&的角色的混淆造成的。替换字符串不是正则表达式,尽管它有一些特殊的字符,比如&。你可以在这里读到&在替换字符串中的角色:h子替换-特殊。
I suspect the main problem for OP is not necessarily typing the extra backslashes, but rather remembering when a backslash is needed and when not. One workaround may be to start making use of "replacement expressions" when unsure. ( See :h sub-replace-expression
.) This requires putting a `\=' in replacement string but for some people it may give you more natural control over what's being substituted, since putting a string literal in single quotes will give you the replacement string you want. For example, this substitute does what OP wants:
我怀疑OP的主要问题并不一定是输入额外的反斜杠,而是记住什么时候需要反斜杠,什么时候不需要反斜杠。解决方法之一可能是在不确定的时候开始使用“替换表达式”。(参见:h sub-replace-expression。)这需要在替换字符串中放置一个“\=”,但是对于某些人来说,它可能会让你更自然地控制被替换的字符串,因为在单引号中添加字符串字面量会给你想要的替换字符串。例如,这个替代品做OP想要的:
:s/</\='<'/g
#2
67
For the :s command there is a shortcut to disable or force magic. To turn off magic use %sno
like:
对于:s命令有一个禁用或强制魔法的快捷方式。关闭魔法使用%sno:
:%sno/search_string/replace_string/g
Found here: http://vim.wikia.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic
在这里找到:http://vim.wikia.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic。
#3
12
Use this option:
使用这个选项:
set nomagic
See :help /magic
看到:帮助/魔法
#4
3
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:
如果您想按字面搜索,可以使用\V regex原子。这几乎是您想要的,除了您还需要转义反斜杠。你可以定义你自己的搜索命令,按字面意思搜索。是这样的:
:com! -nargs=1 Search :let @/='\V'.escape(<q-args>, '\/')| normal! n
And then use :Search /foobar/baz
然后使用:Search /foobar/baz
For Substitute, you could then after a :Search command simply use
作为替代,您可以在a:Search命令之后使用
:%s//replace/g
since then Vim would implicitly pick up the last search item and use the for replacing.
从那时起,Vim将隐式地获取最后一个搜索项并使用for替换。
(Just want to give you some ideas)
(只是想给大家一些建议)
#5
0
Have you enabled magic?
你启用了魔法吗?
:set magic
#6
-3
Try the Edit Find and replace on the menu bar.
尝试在菜单栏上编辑查找和替换。
#1
13
The problem is primarily caused by confusion about the role of the &
in the replacement string. The replacement string is not a reg-ex, although it has some special characters, like &
. You can read about role of &
in replacement string here: :h sub-replace-special
.
问题主要是由于对替换字符串中&的角色的混淆造成的。替换字符串不是正则表达式,尽管它有一些特殊的字符,比如&。你可以在这里读到&在替换字符串中的角色:h子替换-特殊。
I suspect the main problem for OP is not necessarily typing the extra backslashes, but rather remembering when a backslash is needed and when not. One workaround may be to start making use of "replacement expressions" when unsure. ( See :h sub-replace-expression
.) This requires putting a `\=' in replacement string but for some people it may give you more natural control over what's being substituted, since putting a string literal in single quotes will give you the replacement string you want. For example, this substitute does what OP wants:
我怀疑OP的主要问题并不一定是输入额外的反斜杠,而是记住什么时候需要反斜杠,什么时候不需要反斜杠。解决方法之一可能是在不确定的时候开始使用“替换表达式”。(参见:h sub-replace-expression。)这需要在替换字符串中放置一个“\=”,但是对于某些人来说,它可能会让你更自然地控制被替换的字符串,因为在单引号中添加字符串字面量会给你想要的替换字符串。例如,这个替代品做OP想要的:
:s/</\='<'/g
#2
67
For the :s command there is a shortcut to disable or force magic. To turn off magic use %sno
like:
对于:s命令有一个禁用或强制魔法的快捷方式。关闭魔法使用%sno:
:%sno/search_string/replace_string/g
Found here: http://vim.wikia.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic
在这里找到:http://vim.wikia.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic。
#3
12
Use this option:
使用这个选项:
set nomagic
See :help /magic
看到:帮助/魔法
#4
3
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:
如果您想按字面搜索,可以使用\V regex原子。这几乎是您想要的,除了您还需要转义反斜杠。你可以定义你自己的搜索命令,按字面意思搜索。是这样的:
:com! -nargs=1 Search :let @/='\V'.escape(<q-args>, '\/')| normal! n
And then use :Search /foobar/baz
然后使用:Search /foobar/baz
For Substitute, you could then after a :Search command simply use
作为替代,您可以在a:Search命令之后使用
:%s//replace/g
since then Vim would implicitly pick up the last search item and use the for replacing.
从那时起,Vim将隐式地获取最后一个搜索项并使用for替换。
(Just want to give you some ideas)
(只是想给大家一些建议)
#5
0
Have you enabled magic?
你启用了魔法吗?
:set magic
#6
-3
Try the Edit Find and replace on the menu bar.
尝试在菜单栏上编辑查找和替换。