使用 for 循环插入带有索引的单词 vim

Using a for loop to insert words with index appended in vim

不止一次,我不得不在 python 的列表中插入某个单词的变体,例如:

intf = ["eno1", "eno2", "eno3", "eno4", "eno5", "eno6", "eno7", "eno8", "eno9", "eno10"]

是否有一种简单的方法可以使用 for 循环插入这些单词? 我找到了这个: https://renenyffenegger.ch/notes/development/vim/commands/put 这非常接近我想做的。 例如,我可以这样做:

:for i in range(1,10) | put ='\"eno'.i.'\", ' | endfor

这给了我:

"eno1",
"eno2",
...

唯一的区别是它在一个新行上插入每个单词。 是否可能有将所有单词插入同一行的变体?

  1. 首先将所有值附加到一个变量,然后调用 put
:let a="" | for i in range(1,10) | let a.='"eof'.i.'", ' | endfor | put =a
  1. 要删除结尾', ',我们还可以add the values to a list, and then call join
:let a=[] | for i in range(1,10) | call add(a,'"eof'.i.'"') | endfor | put =join(a, ', ')

为什么不直接在代码中生成该列表?

intf = []
for i in range(1, 11):
    intf.append('eno' + str(i))

如果你坚持在 Vim 中使用 Vim 功能:

intf = <C-r>=range(1,10)->map('"eno" . v:val')->string()<CR>

参见 :help i_ctrl-r:help "=:help range():help map():help string():help method