Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VIM中收集具有相同名称模式的变量_Vim - Fatal编程技术网

在VIM中收集具有相同名称模式的变量

在VIM中收集具有相同名称模式的变量,vim,Vim,从下面 # 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 # *

从下面

# 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中高效地执行此操作?

这里有一个流水线命令,它可以执行您想要的操作,您可以在名为
输出的文件中找到结果:

:g/^\s*\(\w*_score\)\s*=\s*run("\(\w*\)").*$/ s//&\rprint "\2 score {2.2f}".format(\1)/ | :. w! >> Output | normal! dd

  • 说明:

上述命令实际上分为3个命令:

  • :g/^\s*\(\w*\u score\)\s*=\s*运行(\(\w*\).*$/s/&\rprint“\2 score{2.2f}”。格式(\1)/
  • 首先是全局搜索,它搜索具有以下模式的行:
    ^\s*\(\w*\u score\)\s*=\s*运行(“\(\w*\)”).$
    (即这些行)

    aal_分数=跑步(“aal”)

    msft_分数=跑步(“msft”)

    aapl_分数=运行(“aapl”)

    aem_分数=跑步(“aem”)

    att_得分=跑步(“att”)

    aep_分数=跑步(“aep”)

    --[-----\1-----------------------------------------[\2]

    在每一行中,我们通过将所需文本包围在
    \(…\)
    (反向引用和分组)中来捕获所需文本,然后使用替换:

    s//&\rprint "\2 score {2.2f}".format(\1)/
    
    每次都会生成(例如:)

    aep_分数=跑步(“aep”)

    打印“AEP分数{2.2f}”。格式(AEP_分数)

  • :。w!>>输出
  • 对于第二个命令,我们将在文件输出的末尾写入打印行

  • 正常!dd
    :.d

  • 从当前文件中删除打印行

    这是一个流水线命令,它可以执行您想要的操作,您将在名为
    输出
    的文件中找到结果:

    :g/^\s*\(\w*_score\)\s*=\s*run("\(\w*\)").*$/ s//&\rprint "\2 score {2.2f}".format(\1)/ | :. w! >> Output | normal! dd
    

    • 说明:

    上述命令实际上分为3个命令:

  • :g/^\s*\(\w*\u score\)\s*=\s*运行(\(\w*\).*$/s/&\rprint“\2 score{2.2f}”。格式(\1)/
  • 首先是全局搜索,它搜索具有以下模式的行:
    ^\s*\(\w*\u score\)\s*=\s*运行(“\(\w*\)”).$
    (即这些行)

    aal_分数=跑步(“aal”)

    msft_分数=跑步(“msft”)

    aapl_分数=运行(“aapl”)

    aem_分数=跑步(“aem”)

    att_得分=跑步(“att”)

    aep_分数=跑步(“aep”)

    --[-----\1-----------------------------------------[\2]

    在每一行中,我们通过将所需文本包围在
    \(…\)
    (反向引用和分组)中来捕获所需文本,然后使用替换:

    s//&\rprint "\2 score {2.2f}".format(\1)/
    
    每次都会生成(例如:)

    aep_分数=跑步(“aep”)

    打印“AEP分数{2.2f}”。格式(AEP_分数)

  • :。w!>>输出
  • 对于第二个命令,我们将在文件输出的末尾写入打印行

  • 正常!dd
    :.d

  • 从当前文件中删除打印行

    这看起来像python。为什么不使用例如带有股票符号的dict作为键?这样就没有那么多样板文件可以尝试使用vim实现自动化,这看起来像python。为什么不使用例如带有股票符号的dict作为键?那么,使用vim尝试和自动化的样板文件就更少了。您介意分解一下命令并解释组件的功能吗?我是Vim的新手,因此我非常感谢。@Menix非常感谢:)您介意将命令分解并解释组件的功能吗?我是Vim的新手,所以我非常感谢。@Menix非常感谢:)