从以下方面
# industrials
# * airlines: AAL
aal_score = run("AAL")
# information technology
# * MSFT
# * AAPL
msft_score = run("MSFT")
aapl_score = run("AAPL")
# materials
# * Agnico Eagle Mines AEM
aem_score = run("AEM")
# telecommunication services
# * ATT
att_score = run("ATT")
# utilities
# * AEP
aep_score = run("AEP")我想收集变量名并打印它们的值,如
print "AAL score {%2.2f}".format(aal_score)
print "MSFT score {%2.2f}".format(msft_score)
print "AAPL score {%2.2f}".format(aapl_score)
...如何在Vim中有效地做到这一点?
发布于 2016-07-06 00:32:32
下面是一个流水线命令,它执行您想要的操作,您将在一个名为Output的文件中找到结果
:g/^\s*\(\w*_score\)\s*=\s*run("\(\w*\)").*$/ s//&\rprint "\2 score {2.2f}".format(\1)/ | :. w! >> Output | normal! dd上面的命令实际上分为三个命令:
:g/^\s*\(\w*_score\)\s*=\s*run("\(\w*\)").*$/ s//&\rprint "\2 score {2.2f}".format(\1)/第一件事是全局搜索,它使用模式^\s*\(\w*_score\)\s*=\s*run("\(\w*\)").*$ (即这些行)搜索行。
aal_score = run("AAL") msft_score = run("MSFT") aapl_score = run("AAPL") aem_score = run("AEM") att_score = run("ATT") aep_score = run("AEP")
在每一行中,我们将所需的文本包围到\(...\) (反向引用和分组)中,然后使用替换:
s//&\rprint "\2 score {2.2f}".format(\1)/生成每次(例如:)
aep_score = run("AEP") 打印"AEP评分{2.2f}".format(aep_score)
:. w! >> Output对于第二个命令,我们将在文件输出的末尾写入打印行。
normal! dd或:.d从当前文件中删除打印行
https://stackoverflow.com/questions/38214710
复制相似问题