Function vim函数外部调用

Function vim函数外部调用,function,rspec,cucumber,vim,Function,Rspec,Cucumber,Vim,目前,我的vimrc具有以下关键映射: map <leader>m :w\|!clear && rspec --drb %<cr> map <leader>k :w\|!clear && rspec --drb %:<C-r>=line('.')<CR><cr> map <leader>c :w\|:!clear && cucumber --drb -r ./fea

目前,我的vimrc具有以下关键映射:

map <leader>m :w\|!clear && rspec --drb %<cr>
map <leader>k :w\|!clear && rspec --drb %:<C-r>=line('.')<CR><cr>
map <leader>c :w\|:!clear && cucumber --drb -r ./features %<cr>
map <leader>x :w\|!clear && cucumber --drb -r ./features %:<C-r>=line('.')<CR><cr>
地图m:w\|!清除&rspec--drb% 地图k:w\|!清除&rspec--drb%:=行('.')) 地图c:w\|::!清除(&C)——drb-r./features% 地图x:w\|!清除(&&cumber--drb-r./features%:=行('.')) 然而,我想将它们合并为(两)个函数,它们具有相同的line vs file键映射,我尝试了以下方法,但Vim抱怨缺少括号:

function! TestCurrentLine()
  let spec = '*_spec\.rb'
  if !(expand("%") =~ spec)
    :!clear && cucumber --drb -r ./features %:<C-r>=line('.')<CR>
  else
    :!clear && rspec --drb %:<C-r>=line('.')<CR>
  end
endfunction

function! TestCurrentFile()
  let spec = '*_spec\.rb'
  if !(expand("%") =~ spec)
    :!clear && cucumber --drb -r ./features %
  else
    :!clear && rspec --drb %
  end
endfunction

map <leader>m :w\|call TestCurrentFile<cr>
map <leader>k :w\|call TestCurrentLine<cr>
函数!TestCurrentLine()
让spec='*_spec\.rb'
如果!(展开(“%”=~spec)
:!清除(&&cumber--drb-r./features%:=行('.'))
其他的
:!清除&rspec--drb%:=行('.'))
结束
端功能
功能!TestCurrentFile()
让spec='*_spec\.rb'
如果!(展开(“%”=~spec)
:!清除(&C)——drb-r./features%
其他的
:!清除&rspec--drb%
结束
端功能
映射m:w\|调用TestCurrentFile
映射k:w\|调用TestCurrentLine

有什么想法吗?

在函数调用中添加括号:

map <leader>m :w\|call TestCurrentFile()<cr>
map <leader>k :w\|call TestCurrentLine()<cr>
map m:w\|调用TestCurrentFile()
映射k:w\|调用TestCurrentLine()

在函数调用中添加括号:

map <leader>m :w\|call TestCurrentFile()<cr>
map <leader>k :w\|call TestCurrentLine()<cr>
map m:w\|调用TestCurrentFile()
映射k:w\|调用TestCurrentLine()

除了map命令中缺少的
()
。您的函数也有一些问题:

  • !清除
    您不需要前导
  • “*\u spec\.rb”
    应该是正则表达式。但是领先的
    *
    没有任何意义。您想拥有
    *\u spe…
    ?使用
    *\u spec\.rb$”也更好
  • %
    直接传递到shell命令不是100%安全的。(如果缓冲区名称有特殊字符或空格)。您可以使用内置的
    shellescape()
    函数。例如,
    shellescape(@%,1)
    (然后需要
    “execute”
    来执行命令)
  • 考虑只在某个缓冲区中创建某些映射(通过AutoCMD)在某个缓冲区(<代码> <代码>),也在创建映射时,考虑“NORE”。
除了map命令中遗漏的
()
。您的函数也有一些问题:

  • !清除
    您不需要前导
  • “*\u spec\.rb”
    应该是正则表达式。但是领先的
    *
    没有任何意义。您想拥有
    *\u spe…
    ?使用
    *\u spec\.rb$”也更好
  • %
    直接传递到shell命令不是100%安全的。(如果缓冲区名称有特殊字符或空格)。您可以使用内置的
    shellescape()
    函数。例如,
    shellescape(@%,1)
    (然后需要
    “execute”
    来执行命令)
  • 考虑只在某个缓冲区中创建某些映射(通过AutoCMD)在某个缓冲区(<代码> <代码>),也在创建映射时,考虑“NORE”。
+1用于
nnoremap
。我还建议如果
文件类型
未正确设置为
cucumber
rspec
则使用如下自动命令
autocmd BufNewFile,BufRead*_spec.rb set filetype=cucumber
。通过
设置文件类型?
检查文件类型。正确设置文件类型将提供的不仅仅是语法高亮显示。它还将允许您访问
:make
。如果需要,可以通过
:compiler cumber
:compiler rspec
直接设置编译器。我建议学习使用
quickfix
列表浏览
:生成
结果。我已经完整地重新编写了命令,但我肯定会查看特定于文件类型的加载,谢谢。
nnoremap
的+1。我还建议如果
文件类型
未正确设置为
cucumber
rspec
则使用如下自动命令
autocmd BufNewFile,BufRead*_spec.rb set filetype=cucumber
。通过
设置文件类型?
检查文件类型。正确设置文件类型将提供的不仅仅是语法高亮显示。它还将允许您访问
:make
。如果需要,可以通过
:compiler cumber
:compiler rspec
直接设置编译器。我建议学习使用
quickfix
列表浏览
:生成
结果。我已经完全重新编写了该命令,但我肯定会查看特定于文件类型的加载,谢谢。