vim - 将缓冲区发送到Vim 8中正在运行的终端窗口

标签 vim terminal

是否可以将缓冲区的内容发送到正在运行的终端窗口。该窗口可以正在运行,例如python代码的REPL。

我的意思是VIM的新终端功能,而不是外部插件或以前的版本。

最佳答案

您可以使用term_sendkeys()将数据发送到终端缓冲区。但是,有一些注意事项:

  • 通常需要通过拉动文本
  • 来捕获数据以使用term_sendkeys()
  • 需要知道要发送到
  • 的哪个终端缓冲区

    这是一些简化并自动执行“发送到终端缓冲区”工作流的代码。放入vimrc文件或制作一个小插件。
    augroup send_to_term
      autocmd!
      autocmd TerminalOpen * if &buftype ==# 'terminal' |
            \   let t:send_to_term = +expand('<abuf>') |
            \ endif
    augroup END
    
    
    function! s:op(type, ...)
      let [sel, rv, rt] = [&selection, @@, getregtype('"')]
      let &selection = "inclusive"
    
      if a:0 
        silent exe "normal! `<" . a:type . "`>y"
      elseif a:type == 'line'
        silent exe "normal! '[V']y"
      elseif a:type == 'block'
        silent exe "normal! `[\<C-V>`]y"
      else
        silent exe "normal! `[v`]y"
      endif
    
      call s:send_to_term(@@)
    
      let &selection = sel
      call setreg('"', rv, rt)
    endfunction
    
    function! s:send_to_term(keys)
      let bufnr = get(t:, 'send_to_term', 0)
      if bufnr > 0 && bufexists(bufnr) && getbufvar(bufnr, '&buftype') ==# 'terminal'
        let keys = substitute(a:keys, '\n$', '', '')
        call term_sendkeys(bufnr, keys . "\<cr>")
        echo "Sent " . len(keys) . " chars -> " . bufname(bufnr)
      else
        echom "Error: No terminal"
      endif
    endfunction
    
    command! -range -bar SendToTerm call s:send_to_term(join(getline(<line1>, <line2>), "\n"))
    nmap <script> <Plug>(send-to-term-line) :<c-u>SendToTerm<cr>
    nmap <script> <Plug>(send-to-term) :<c-u>set opfunc=<SID>op<cr>g@
    xmap <script> <Plug>(send-to-term) :<c-u>call <SID>op(visualmode(), 1)<cr>
    

    您可以设置自己的映射。例子:
    nmap yrr <Plug>(send-to-term-line)
    nmap yr <Plug>(send-to-term)
    xmap R <Plug>(send-to-term)
    

    现在,您可以使用:[range]SendToTerm将行的[range]发送到选项卡页中最后使用的终端缓冲区。您还可以使用yrr发送行,yr{motion}发送{motion}文本,或使用R将可视选择的文本发送到终端缓冲区。注意:您必须在当前选项卡页面中事先打开终端缓冲区。

    关于vim - 将缓冲区发送到Vim 8中正在运行的终端窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49318522/

    相关文章:

    java - 从java执行终端命令

    vim - Vim-NERDTree-如何在NERDTree插件中打开隐藏的文件夹(.name)?

    Vim:箭头键在插入模式下在一行内移动

    emacs - M-RET 和 M-UP/DOWN 在控制台/终端的 Emacs 组织模式下不起作用

    java - 在linux中使用psql配合java导入sql文件

    MySQL 在终端中不工作,但可以连接到数据库并使用查询

    php - 使用 Vim 正确缩进 HTML 和 PHP

    vim - 如何使用Ctags列出vim中某个符号(标签)的所有引用?

    unix - VIM textwidth 没有效果

    linux - Vim:映射 ctrl+pgup 和 ctrl+pgdn (CTRL+Page Up/Down) 组合键