新维姆 | E81:不在脚本上下文中使用 <SID>

NeoVIM | E81: Using <SID> not in a script context

我正在尝试将 g++ 运行 作为 vim 中的一个函数 :call > 由于某些我不理解的原因 vim 抛出一个 E81当我尝试执行 s:compile_run_cpp() 函数时出错。

function! s:compile_run_cpp() abort
  let src_path = expand('%:p:~')
  let src_noext = expand('%:p:~:r')
  " The building flags
  let _flag = '-Wall -Wextra -std=c++11 -O2'

  if executable('clang++')
    let prog = 'clang++'
  elseif executable('g++')
    let prog = 'g++'
  else
    echoerr 'No compiler found!'
  endif
  call s:create_term_buf('v', 80)
  execute printf('term %s %s %s -o %s && %s', prog, _flag, src_path, src_noext, src_noext)
  startinsert
endfunction

function s:create_term_buf(_type, size) abort
  set splitbelow
  set splitright
  if a:_type ==# 'v'
    vnew
  else
    new
  endif
  execute 'resize ' . a:size
endfunction

如果您使用自动加载而不是 .vimrc 然后将您的函数名称更改为 filename#function_name 例如:如果您的文件名是 compile.vim 并且函数名称是 compile_run_cpp 那么函数名称将是 compile#compile_run_cpp 然后你可以很容易地用 :call compile#compile_run_cpp()

调用它
function! compile#compile_run_cpp() abort
  let src_path = expand('%:p:~')
  let src_noext = expand('%:p:~:r')
  " The building flags
  let _flag = '-Wall -Wextra -std=c++11 -O2'

  if executable('clang++')
    let prog = 'clang++'
  elseif executable('g++')
    let prog = 'g++'
  else
    echoerr 'No compiler found!'
  endif
  call s:create_term_buf('v', 80)
  execute printf('term %s %s %s -o %s && %s', prog, _flag, src_path, src_noext, src_noext)
  startinsert
endfunction

function s:create_term_buf(_type, size) abort
  set splitbelow
  set splitright
  if a:_type ==# 'v'
    vnew
  else
    new
  endif
  execute 'resize ' . a:size
endfunction

如果您正在使用 .vimrc

尝试改变这个:

call s:create_term_buf('v', 80)

对此:

call <SID>create_term_buf('v', 80)

注:

  • 其他程序员使用的函数命名转换。例如:create_term_bufCreate_term_buf
  • 如果您想从 compile_run_cpp.
  • 等其他脚本调用函数,请不要限定函数的作用域