Nvim :help
页面,生成 自 源代码 使用 tree-sitter-vimdoc 解析器。
jobstart(..., {'pty': v:true})
或 termopen() 打开的 PTY 的主端 PTY。on_stdout
、on_stderr
、on_stdin
或 on_data
选项键的回调函数来响应通道活动(接收到的数据)。回调应该很快:避免潜在的缓慢/昂贵的操作。['']
表示 EOF(流已关闭)。或者设置 stdout_buffered
、stderr_buffered
、stdin_buffered
或 data_buffered
选项键,仅在收集完所有输出并关闭流后调用回调。 E5210{name}
键中。如果键存在,则会发生错误。{data}
列表中的第一项和最后一项可能是部分行。空字符串完成之前的部分行。示例(不包括在 EOF 时发出的最终 ['']
)foobar
可能到达为 ['fo'], ['obar']
foo\nbar
可能到达为['foo','bar']
['foo',''], ['bar']
['foo'], ['','bar']
['fo'], ['o','bar']
let s:lines = ['']
func! s:on_event(job_id, data, event) dict
let eof = (a:data == [''])
" Complete the previous line.
let s:lines[-1] .= a:data[0]
" Append (last item may be a partial line, until EOF).
call extend(s:lines, a:data[1:])
endf
function! s:OnEvent(id, data, event) dict
let str = join(a:data, "\n")
echomsg str
endfunction
let id = jobstart(['cat'], {'on_stdout': function('s:OnEvent') } )
call chansend(id, "hello!")
function! s:OnEvent(id, data, event) dict
call nvim_buf_set_lines(2, 0, -1, v:true, a:data)
endfunction
let id = jobstart(['grep', '^[0-9]'], { 'on_stdout': function('s:OnEvent'),
\ 'stdout_buffered':v:true } )
call chansend(id, "stuff\n10 PRINT \"NVIM\"\nxx")
" no output is received, buffer is empty
call chansend(id, "xx\n20 GOTO 10\nzz\n")
call chanclose(id, 'stdin')
" now buffer has result
jobstart(..., {'pty': v:true})
打开的 PTY 通道不会预处理 ANSI 转义序列,这些序列将以原始形式发送到回调。但是,可以使用 jobresize() 将 PTY 大小更改信号发送到从属设备。另请参阅 terminal-emulator。:echo system('nvim --headless +"te stty -a" +"sleep 1" +"1,/^$/print" +q')
func! OnEvent(id, data, event)
if a:data == [""]
quit
end
call chansend(a:id, map(a:data, {i,v -> toupper(v)}))
endfunc
call stdioopen({'on_stdin': 'OnEvent'})
uppercase.vim
并运行nvim --headless --cmd "source uppercase.vim"
:startinsert
,以便用户可以开始键入一行。CTRL-W
键可用于启动窗口命令,例如 CTRL-W
w 切换到下一个窗口。这在插入模式下也能正常工作(使用 Shift-CTRL-W 删除一个单词)。离开窗口时,将停止插入模式。返回提示窗口时,将恢复插入模式。" Function handling a line of text that has been typed.
func TextEntered(text)
" Send the text to a shell with Enter appended.
call chansend(g:shell_job, [a:text, ''])
endfunc
" Function handling output from the shell: Add it above the prompt.
func GotOutput(channel, msg, name)
call append(line("$") - 1, a:msg)
endfunc
" Function handling the shell exits: close the window.
func JobExit(job, status, event)
quit!
endfunc
" Start a shell in the background.
let shell_job = jobstart(["/bin/sh"], #{
\ on_stdout: function('GotOutput'),
\ on_stderr: function('GotOutput'),
\ on_exit: function('JobExit'),
\ })
new
set buftype=prompt
let buf = bufnr('')
call prompt_setcallback(buf, function("TextEntered"))
call prompt_setprompt(buf, "shell command: ")
" start accepting shell commands
startinsert