Vim脚本:如何在Vim函数中执行命令

15
为什么以下代码会:
let s:colorschemes = ['synic', 'ir_black']
let s:colorscheme_idx = 0

function! RotateColorscheme()
  let s:colorscheme_idx += 1
  let s:name = s:colorschemes[s:colorscheme_idx]
  echo s:name
  colorscheme s:name
endfunction

无法执行colorscheme?Vim 报错 'cannot find colorschem s:name'。如何告诉Vim对该变量进行解引用而不是将其文字字面应用于 :colorscheme 命令?

1个回答

16

看看这段来自vim.wikia.com的脚本,基本上就是你所要求的。

关键代码似乎是这一行:

let nowcolors = 'elflord morning desert evening pablo'
  execute 'colorscheme '.split(nowcolors)[i]

1
谢谢,但是你知道我为什么需要使用execute吗?我在上面的echo行中不需要使用execute吗?是什么使:echo与:colorscheme不同? - sashang
请参考:help exe:help echo - Fredrik Pihl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接