Script Vim with Tcl

风行水上 @ 2014-06-14 16:32:09
标签:

    Vim可以支持扩展脚本,比如:

    • Tcl
    • Python
    • Lua
    • Ruby

    Vim 对 Tcl 的支持

    Windows打开gVim,执行ex命令:version,确认可以找到如下信息:

    • +tcl/dyn:表示已经编译了对Tcl的支持。
    • tcl*.dll:显示编译时所用的Tcl的版本,比如"tcl8.5.dll"。

    通过ex命令:tcl puts "hello"确认是否Tcl可用。如果各项配置都正常的话,应该可以看到窗口底部显示"hello"字体串。

    Tcl 安装问题

    gVim只是默认已经编译了对Tcl的支持,是通过动态链接的形式实现的。但没有自带需要的Tcl运行环境。

    所谓的Tcl运行环境,这里主要是指动态加载"tcl8.5.dll"(上述':version'命令所显示的Tcl运行库文件)文件的问题。

    可以参考 Tcl动态运行环境的配置

    Tcl in Vim 牛刀小试

    :tcl

    :tcl直接执行Tcl命令。

    :tcl puts "hello"
    
    :tcl << EOF
    puts hello
    EOF
    
    :tcl <<
    puts hello
    puts world
    .
    

    对"<<"的形式,如果省略"marker",可以用'.'表示结束。

    :tcldo

    针对当前编辑文件的每一行内容执行Tcl命令。

    • 行内容用变量line进行引用——赋值该变量可以改变行的内容。
    • 行号用变量lnum表示
    :tcldo set line "123abc456"
    # 当前文件每一行的内容都变成"123abc456"
    
    :tcldo set line [regsub -all {abc} $line "def"]
    # 替换每一行的"abc"为"def"
    

    :tclfile

    :tclfile script.tcl
    

    相当于 :tcl source script.tcl,但可以进行文件名匹配以方便选择脚本。

    Vim Tcl API

    可以通过:help tcl查看相关的Vim Tcl API文档。

    变量:

    • $::vim::current
    • ...

    命令:

    • ::vim::buffer
    • ...

    TODO

    Vim Tcl 实例

    对所有行进行排序:

    set buf $::vim::current(buffer)
    set lines [$buf get top bottom]
    set lines [lsort -dictionary $lines]
    $buf set top bottom $lines
    

    Best Practice

    " File: ~/.vimrc
    
    if has("tcl")
      tclfile ~/.vimrc.tcl
    endif
    

    TODO:

    标签:

      分享到:
      comments powered by Disqus

      25/29ms