vim - 在 Snipmate.vim 片段中更改大小写?

标签 vim snipmate

是否可以更改 Snipmate 片段中变量值的大小写?

例如:

snippet dc
  def create
    @${1} = $1.new
  end

应该输出:
def create
  @product = Product.new
end

我尝试使用反引号来调用自定义函数:
snippet dc
  def create
    @${1} = `ToUpperCase('$1')`.new
  end

并在 Vim 中定义了这个函数:
function! ToUpperCase(str)
    let result = substitute(a:str, '\(\w\)', '\u\1', '')
    return result
endfunction

这不起作用,因为 Snipmate 在执行反引号后似乎会扩展其 $n 变量。

最佳答案

免责声明:我是 UltiSnips 的主要作者。

为了您的兴趣和敲鼓的目的,我提供了 UltiSnips 的两个片段定义之前已经在这里提到过。两者都做OP想要的。第一个使用转换(TextMate 语法):

snippet dc "create" b
def create
   @$1 = ${1/.*/\u$0/}.new
end
endsnippet

第二种使用python代码插值。根据我的口味,这更容易阅读,但有点冗长。
snippet dc "create" b
def create
   @$1 = `!p snip.rv = t[1].title()`.new
end
endsnippet

由于版本 1.3 UltiSnips 带有一个可以转换 snipMate 片段的脚本,因此切换应该很容易。

关于vim - 在 Snipmate.vim 片段中更改大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5064430/

相关文章:

linux - Vim,拆分窗口中断长度突出显示

vim - 允许 vim snipMate 片段处理不同的文件类型

ruby-on-rails - Vim html.erb 片段? snipMate 需要一个 vim 提示

vim - SnipMate与病原体

vim - 使用 snipMate 在代码片段中插入 $1

vim - 如何阻止 c)hange 复制到我的粘贴缓冲区?

r - 使用 Vim + latex-suite + knitr

python - python : cannot instantiate more than one or two variables (SyntaxError raised)

vim - 我怎样才能禁用 vim 插件 snipMate 到某种文件?

Vim:浏览 Latex 文档结构