简体   繁体   English

map Vim 冒号搜索和替换命令如何仅与视觉选择一起使用?

[英]How do I map Vim colon search and replace command to work just with visual selection?

Sometimes I want to replace some text in multiple lines but not in the whole line, so I toggle visual mode on and highlight the text I want to replace.有时我想替换多行而不是整行的一些文本,所以我打开可视模式并突出显示我要替换的文本。 But then, I need to add this annoying \%V that I always forget for it to replace the text just in the selection and not in the whole line但是,我需要添加这个烦人的\%V ,我总是忘记它来替换选择中的文本而不是整行中的文本

:'<, '>s/\%Vold/new/g

Is there a way to map the normal replace command有没有办法 map 正常替换命令

:'<, '>s/old/new/g

To the one shown above so that I don't need to remember that V every time?到上面显示的那个,这样我就不需要每次都记住那个 V 了吗?

Thank you谢谢

I tried using cmap command in the vimrc file in the following way:我尝试通过以下方式在 vimrc 文件中使用 cmap 命令:

cmap '<, '>s/* '<, '>s/\%V*

But this of course doesn't work, because instead of the asterisk I should input the text I want to search and replace to end the search and replace command.但这当然行不通,因为我应该输入要搜索和替换的文本而不是星号来结束搜索和替换命令。

There are three basic parts to a mapping:映射包含三个基本部分:

  • the mapping command,映射命令,
  • the "left-hand side" (LHS), “左侧”(LHS),
  • and the "right-hand side" (RHS).和“右侧”(RHS)。

The LHS is the key sequence/combination that you want to press and the RHS is a different key sequence that you want Vim to "press" instead of those in the LHS. LHS 是您要按下的键序列/组合,而 RHS 是您希望 Vim “按下”而不是 LHS 中的键序列的不同键序列。 You "map" one key sequence to another.您将一个键序列“映射”到另一个键序列。

The RHS is, fundamentally, a macro: a sequence of keystrokes, "just" like if you pressed them yourself but without any delay between them.从根本上说,RHS 是一个宏:一系列击键,“就像”您自己按下它们一样,但它们之间没有任何延迟。 So, the RHS is really just a bunch of keystrokes you are tired of making over and over again.因此,RHS 实际上只是一堆您厌倦了一遍又一遍的击键。

The main use of mappings is to reduce repetitiveness.映射的主要用途是减少重复性。 You figure that you often press the imaginary sequence sdlusdrosydtsodsd and you map it all to <F5> to be more efficient.您认为您经常按虚构序列sdlusdrosydtsodsd和 map 将其全部按<F5>以提高效率。

In this case…在这种情况下…

  • The current mode is visual mode so the mapping has to be a visual mode mapping.当前模式是可视模式,所以映射必须是可视模式映射。

    This makes :xmap or :xnoremap the proper mapping command .这使得:xmap:xnoremap成为正确的映射命令 Let's make it :xnoremap .让我们:xnoremap

  • The repetitive key sequence you want to abstract away is:您想要抽象掉的重复键序列是:

     :s/\%V

    That's your RHS .那是你的 RHS

    (Note that the range '<,'> is automatically inserted for you so it can be ignored.) (请注意,范围'<,'>会自动为您插入,因此可以忽略。)

  • The key sequence you want to press instead is, let's say… <F5> .你想按的键序列是,比方说...... <F5>

    That's your LHS .那是你的 LHS

Now that you have your mapping command, your LHS, and your RHS, building the mapping is extremely easy:现在您有了映射命令、LHS 和 RHS,构建映射非常容易:

:xnoremap <F5> :s/\%V

地图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM