Nvim 的 :help
頁面,由 生成,其原始碼使用 tree-sitter-vimdoc 解析器。
jobstart(..., {'pty': v:true})
或 termopen() 開啟的 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