请给我一些有关Vimgolf问题的建议

发布于 2025-01-19 13:50:22 字数 615 浏览 1 评论 0原文

https://www.vimgolf.com/challenges/9v006233d72d000000000219

启动文件

#!/bin/bash
a = 5
b = 10
sum = $a + $b
echo $sum

mul = $a * $b
echo $mul

<强>结束文件

#!/bin/bash
a=5
b=10
sum=$((a + b))
echo $sum

mul=$((a * b))
echo $mul

===================================

这个问题中的击键是 26 但我只得到 41。

我使用的方式 我不

:%s/ = /=/g

:%s/$a/$((a/g

:%s/$b/b))/g

知道如何进一步减少击键。请给我一些建议。

https://www.vimgolf.com/challenges/9v006233d72d000000000219

Start file

#!/bin/bash
a = 5
b = 10
sum = $a + $b
echo $sum

mul = $a * $b
echo $mul

End file

#!/bin/bash
a=5
b=10
sum=$((a + b))
echo $sum

mul=$((a * b))
echo $mul

=================================

The keystroke in this problem was 26 but I only get 41.

The way I used it

:%s/ = /=/g

:%s/$a/$((a/g

:%s/$b/b))/g

I don't know how to reduce keystrokes more. Please give me some advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2025-01-26 13:50:22
  • /g意思是“在行中的每场比赛中进行替换”。每种模式只有一个匹配项,因此不需要/g s:

     :%s / = / =&lt; cr&gt;
    :%s/$ a/$((a&lt; cr&gt;
    :%s/$ b/b))&lt; cr&gt;
     

    您降至36个击键。

    参见:help:s_g

  • 在这种特定情况下,$ a + $ b可以与单个模式匹配,$。*b,因此您可以将最后两个替换融合到一个单个:

     :%s / = / =&lt; cr&gt;
    :%s/$。b/$((&amp;))
     

    ,您降至26个击键。

    参见:帮助S/\&amp;

  • /g means "do the substitution on every match in the line". There is only one match for each pattern so the /gs are not necessary:

    :%s/ = /=<CR>
    :%s/$a/$((a<CR>
    :%s/$b/b))<CR>
    

    You are down to 36 keystrokes.

    See :help :s_g.

  • In this specific case, $a + $b can be matched with a single pattern, $.*b, so you could fuse the two last substitutions into a single one:

    :%s/ = /=<CR>
    :%s/$.*b/$((&))<CR>
    

    And you are down to 26 keystrokes.

    See :help s/\&.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文