如何编写一个vim函数,它将从文件的前一行返回一个字符串?

时间:2016-01-15 02:37:31

标签: vim

鉴于以下5行:

describe 'trueness', ->
  it 'is really, really, really true', ->
    expect(true).to.eq true
    expect(true).to.eq true
    expect(true).to.eq true

有没有办法可以在vim脚本中编写一个函数:

  • 当在上述代码的第2,3,4或5行中的任何位置运行时,将返回字符串it is really, really, really true
  • 在第1行的任何位置运行时,会返回字符串trueness

(我希望编写一个允许我从mocha运行某些vim规范的函数,唯一的方法是通过mocha a表示要运行的规范名称的字符串。)

1 个答案:

答案 0 :(得分:1)

使用while循环的简单迭代将为您完成。您使用getline()获取当前行的内容,并使用matchstr()和正确的正则表达式提取带引号的字符串,如下所示:

function! GetQuotedAbove()
    let lnum = line('.')
    while lnum > 0
        let quoted = matchstr(getline(lnum), "'[^']*'")
        if ! empty(quoted)
            return quoted
        endif
        let lnum -= 1
    endwhile
    return ''
endfunction
相关问题