1

我有这段代码:

fill_in "Name",with: "John"
fill_in "Email",with: "user@example.com"
fill_in "Password",with: "supersafe"

我想用 替换后面写:的所有内容,因此user.我只需在.nameemailpassworduser.

fill_in "Name",with: user.name
fill_in "Email",with: user.email
fill_in "Password",with: user.password

此外,一种从所选可视块的一行导航到另一行的方式(可能是 vim 插件?)当然是最佳的,尽管不是这里的重点。

一个更简单的选择是不用替换它,而是用;user.删除它。d$但是选择问题仍然存在。

我尝试了正常Ctrl-V选择,但由于:字符放置在每行的不同列中,我没有所需的行为。

谢谢

4

3 回答 3

5

或者,如果(像我一样)您更喜欢使用手动编辑的命令,而不是输入疯狂的正则表达式:

:g/with:/norm yi"$"_ca"␣user.C-r"Escv`.u`

  1. g:/with:/(适用于所有包含“with:”的行(如果缓冲区不包含其他行,这实际上是可选的)
  2. norm正常模式下的以下“键”:

    • yi" (拉出第一个引用的值)
    • $ (移动到行尾...)
    • "_ca" (更改引用的文本) ␣user. (以 开头user. C-r" (以及最初被抽出的文本) Esc (完成)
    • v``.u` (小写的粘贴位)

这可能是口味问题,但对我来说效果很好。

复杂?这是一个技巧:我实际上所做的是在一行上执行编辑(用qq...录制宏q),然后

:%norm @q

将其应用于所有行

演示:(注意它仍然使用gu`` instead of.v \``.u

在此处输入图像描述

于 2013-09-10T19:07:31.463 回答
3

只是为了好玩,另一个宏:

qq     start recording in register q
0      go to the first column
yi"    yank the content of the first pair of quotes
f";    jump to the next pair of quotes
"_c$   change until the end of line, sending the previous text to the black hole register
user.  insert user.
<C-r>" insert the content of the default register
<Esc>  exit insert mode
guiw   change the case of the word under the cursor
q      stop recording

我确信有几十种方法可以获得相同的结果。

于 2013-09-10T19:34:17.380 回答
3

这只会突出显示您希望它运行的行。

非魔法正则表达式

s/.\{-}"\(.\{-}\)".\{-}:\zs.*/ user.\L\1/g

非常神奇的正则表达式

s/\v.{-}"(.{-})".{-}:\zs.*/ user.\L\1/g

这捕获了引号中的内容。由于这使用\zs冒号后的匹配开始。因此,冒号之后的所有内容都被替换user.为引号中的任何内容的小写版本。\L将字符串 after 转换为它的小写版本。

于 2013-09-10T18:20:53.600 回答